Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Copy Listview items to new row in excel sheet Hi, I am trying to copy items in l

ID: 3570879 • Letter: C

Question

Copy Listview items to new row in excel sheet

Hi,

   I am trying to copy items in listview to new row in excel sheet without disturbing the earlier data in excel sheet. With following code it is not possible, can anybody help.

Private Sub CommandButton1_Click()
Dim i As Long
     Dim r As Long
     Dim c As Long
    
     Sheet14.Activate
    
     'Clear any existing data
     Range("A2:J100").CurrentRegion.ClearContents
    
     'List the column headers in the Second row, starting at A1
     With Me.ListView1
         For i = 1 To .ColumnHeaders.count
             Cells(2, i).Value = .ColumnHeaders(i)
         Next i
     End With
    
    
     'List the selected items and subitems starting at Row 2
     r = 3
     With Me.ListView1
         For i = 1 To .ListItems.count..
             If .ListItems(i).Checked Then
                             Cells("r, "A:").Value = .ListItems(i).Text
                 For c = 1 To .ListItems(i).ListSubItems.count
                     Cells(r, c + 1).Value = .ListItems(i).ListSubItems(c).Text
                 Next c
                 r = r + 1
             End If
         Next i
     End With.
    
    
     Unload Me.
     UserForm26.Show
End Sub

Explanation / Answer

Hi..

Try replacing...

r = 3

with

r = Cells(Rows.Count, "A").End(xlUp).Row + 1;

Hope this helps!!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote