I'm having troubles writing data to a specific worksheet using VB.Net. I want my program to be able to select a worksheet and write to it. If there are 5 worksheets in a workbook, I want to be able to loop through and write data to each specific worksheet. The code below works for "sheet1", but if I change "shXL = wbXl.Worksheets(1)" to "shXL = wbXl.Worksheets(2)", thinking that this would use "sheet2", it does not work. Any help would be much appreciated.
Dim appXL As excel.Application
Dim wbXl As excel.Workbook
Dim shXL As excel.Worksheet
Dim raXL As excel.Range
Dim xlsFile As String = "\\ssohome\home\ftp\Debbie\test1.xlsx"
' Start Excel and get Application object.
appXL = CreateObject("Excel.Application")
appXL.Visible = True
' Add a new workbook.
'wbXl = appXL.Workbooks.Add
appXL = New excel.ApplicationClass
wbXl = appXL.Workbooks.Open(xlsFile)
'shXL = wbXl.Worksheets("sheet1")
shXL = wbXl.ActiveSheet()
' Add table headers going cell by cell.
With wbXl
shXL = wbXl.Worksheets(1)
shXL.Cells(1, 1).Value = "First Name"
shXL.Cells(1, 2).Value = "Last Name"
shXL.Cells(1, 3).Value = "Full Name"
shXL.Cells(1, 4).Value = "Phone"
End With
wbXl.Save()
Dim appXL As excel.Application
Dim wbXl As excel.Workbook
Dim shXL As excel.Worksheet
Dim raXL As excel.Range
Dim xlsFile As String = "\\ssohome\home\ftp\Debbie\test1.xlsx"
' Start Excel and get Application object.
appXL = CreateObject("Excel.Application")
appXL.Visible = True
' Add a new workbook.
'wbXl = appXL.Workbooks.Add
appXL = New excel.ApplicationClass
wbXl = appXL.Workbooks.Open(xlsFile)
'shXL = wbXl.Worksheets("sheet1")
shXL = wbXl.ActiveSheet()
' Add table headers going cell by cell.
With wbXl
shXL = wbXl.Worksheets(1)
shXL.Cells(1, 1).Value = "First Name"
shXL.Cells(1, 2).Value = "Last Name"
shXL.Cells(1, 3).Value = "Full Name"
shXL.Cells(1, 4).Value = "Phone"
End With
wbXl.Save()