Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I dont know what to put after the bracket of the list item class, so their is an error saying

expression expected

This is the code:

C#
xlRange = sheet.Range("A3", "I3")
xlRange.Font.Bold = True
xlRange.ColumnWidth = 13
xlRange.Value = {"Asset Id", "Barcode", "Asset Name", "Employee Name", "Location", "Sl.No", "Computer Name", "Model No", "allocation date"}



error displays in xlrange.value line near to the opening bracket.
Posted

Curly braces don't work in VBA. Try this instead:
xlRange.Value = Array("Asset Id", "Barcode", "Asset Name", "Employee Name", "Location", "Sl.No", "Computer Name", "Model No", "allocation date")


Good luck!
 
Share this answer
 
Comments
Maciej Los 16-Oct-14 10:59am    
+5
jmavrix 18-Oct-14 2:53am    
Hi
Thanks for your reply

but i still have the problem, my code works fine in vb.net 2010 only having issue running in .net 2008.
any other ways ?
Maciej Los 18-Oct-14 10:58am    
Yes, please see my answer.
If you want to add values into cell A3 to I3, try this:
VB
Dim headers As String = "Asset Id,Barcode,Asset Name,Employee Name,Location,Sl.No,Computer Name,Model No,allocation date"

'further
xlRange = sheet.Range("A3:I3")
xlRange.Font.Bold = True
xlRange.ColumnWidth = 13
xlRange = headers.Split(",")
 
Share this answer
 
v3
Comments
jmavrix 22-Oct-14 2:15am    
It still shows me Array type can not be used as an expression
and i am using vb.net 2008
Maciej Los 22-Oct-14 2:31am    
See updated answer ;)
jmavrix 22-Oct-14 2:50am    
it gives me an error like xlApp is not declared

any other suggestions please
Maciej Los 22-Oct-14 3:07am    
xlApp is an Excel application.
Where are you using above code? Is it VB.NET or VBA?
jmavrix 22-Oct-14 3:49am    
<pre lang="vb">Private Sub Export_Excel()
Dim xlApp As Excel.Application

SaveFileDialog1.Title = "Save Excel File"
SaveFileDialog1.Filter = "Excel files (*.xls)|*.xls|Excel Files (*.xlsx)|*.xslx"
SaveFileDialog1.ShowDialog()
'exit if no file selected
If SaveFileDialog1.FileName = "" Then
Exit Sub
End If

'create objects to interface to Excel
Dim xls As New Excel.Application
Dim book As Excel.Workbook
Dim sheet As Excel.Worksheet
Dim xlRange As Excel.Range
'create a workbook and get reference to first worksheet
xls.Workbooks.Add()
book = xls.ActiveWorkbook
sheet = book.ActiveSheet

''Dim valuee(1, 3) As String
''valuee(1, 1) = frmMain.pubH1
''valuee(1, 2) = ""
''valuee(1, 3) = ""

xlRange = sheet.Range("A1", "C1")
xlRange.Font.Bold = True
xlRange.ColumnWidth = 15
'' Error in below line
xlRange.Value = (frmMain.pubH1, "", "")
xlRange.Borders(Excel.XlBordersIndex.xlEdgeBottom).LineStyle = Excel.XlLineStyle.xlContinuous
xlRange.Borders(Excel.XlBordersIndex.xlEdgeBottom).Weight = 2


'sheet.Cells(1, 1) = frmMain.pubH1



xlRange = sheet.Range("A3", "I3")
xlRange.Font.Bold = True
xlRange.ColumnWidth = 13
'' Error in below line
xlRange.Value = {"Asset Id", "Barcode", "Asset Name", "Employee Name", "Location", "Sl.No", "Computer Name", "Model No", "allocation date"}
xlRange.Borders(Excel.XlBordersIndex.xlEdgeBottom).LineStyle = Excel.XlLineStyle.xlContinuous
xlRange.Borders(Excel.XlBordersIndex.xlEdgeBottom).Weight = 2

Dim row As Integer = 5
Dim col As Integer = 1
For Each item As ListViewItem In LstAstList.Items
For i As Integer = 0 To item.SubItems.Count - 1
sheet.Cells(row, col) = item.SubItems(i).Text
col = col + 1
Next
row += 1
col = 1
Next

'save the workbook and clean up
book.SaveAs(SaveFileDialog1.FileName)
xls.Workbooks.Close()
xls.Quit()
releaseObject(sheet)
releaseObject(book)
releaseObject(xls)
End Sub</pre>

this is my complete code. In a button click i call this function

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900