PDA

View Full Version : listbox value transfer to new excel worksheet



jay6939
07-15-2020, 12:59 PM
i am writing a code to transfer list box data to new excel workbook. it is working by populating new worksheet from row 2 and particular column. what i want is, list should be populating from row 23 in particular column. i have tried to search but unable to make it work. my code is as below,


Private Sub cmdprint_Click()

Dim xl As New Excel.Application
Dim xlwbook As Excel.Workbook
Dim xlsheet As Excel.Worksheet

xl.DisplayAlerts = False


Set xlwbook = xl.Workbooks.Open("C:\Users\filename.xlsm")
Set xlsheet = xlwbook.Sheets.Item("output")

Dim i As Long, j As Long

j = 2


With UserForm1.lstdatabase1
For i = 0 To UserForm1.lstdatabase1.ListCount - 1

With xlsheet

.Cells(j, 7).End(xlUp).Offset(1).Value = UserForm1.lstdatabase1.List(i, 1) 'column 1
.Cells(j, 8).End(xlUp).Offset(1).Value = UserForm1.lstdatabase1.List(i, 2) 'column 2
j = j + 1
End With


xlwbook.SaveAs ("C:\Users\File name")



xl.DisplayAlerts = True



Next i

End With

End Sub

Also, i would like to save file save as Textbox value from form, instead of fix name.
can you pl help me with that.
thanks