I currently have a listbox where selected entries are exported to a brand-NEW Excel worksheet (each time) after the user clicks a command button to run the following VBA code.
However, I'd like it to create just ONE new Excel worksheet initially and then append later selections onto that same spreadsheet. What changes would I need to make to the code for this to happen?
Private Sub shootoutexcel_Click()
On Error GoTo err_handler
Dim x As Integer, y As Integer
Dim itm As Variant
Dim xlNew As Excel.Application
Dim sht
Set xlNew = GetObject(, "Excel.Application")
If TypeName(xlNew) = "Nothing" Then
Set xlNew = CreateObject("Excel.Application")
End If
'Set xlNew = CreateObject("Excel.application")
With xlNew
.Workbooks.Add
.Sheets.Add
For Each itm In Me.lstCustInfo.ItemsSelected
x = x + 1
For y = 1 To Me.lstCustInfo.ColumnCount - 1
.Sheets(1).Cells(x, y) = Me.lstCustInfo.Column(y, itm)
Next
Next
End With
xlNew.Visible = True
Exit Sub
err_handler:
If Err.Number = 429 Then
Resume Next
Else
MsgBox Err.Number & " " & Err.Description, vbOKOnly, "Error"
End If
End Sub
However, I'd like it to create just ONE new Excel worksheet initially and then append later selections onto that same spreadsheet. What changes would I need to make to the code for this to happen?
Private Sub shootoutexcel_Click()
On Error GoTo err_handler
Dim x As Integer, y As Integer
Dim itm As Variant
Dim xlNew As Excel.Application
Dim sht
Set xlNew = GetObject(, "Excel.Application")
If TypeName(xlNew) = "Nothing" Then
Set xlNew = CreateObject("Excel.Application")
End If
'Set xlNew = CreateObject("Excel.application")
With xlNew
.Workbooks.Add
.Sheets.Add
For Each itm In Me.lstCustInfo.ItemsSelected
x = x + 1
For y = 1 To Me.lstCustInfo.ColumnCount - 1
.Sheets(1).Cells(x, y) = Me.lstCustInfo.Column(y, itm)
Next
Next
End With
xlNew.Visible = True
Exit Sub
err_handler:
If Err.Number = 429 Then
Resume Next
Else
MsgBox Err.Number & " " & Err.Description, vbOKOnly, "Error"
End If
End Sub