Search code examples
vbaexcellistboxcommandbutton

Dosent load the empty cells from listbox VBA excel


I have a CommandButton to load the results from listbox(lsbWarenausgang) to the sheet (Tabelle5) but the button loads the results and all the empty results in excel table.

enter image description here

 Private Sub CommandButton3_Click()
 Dim lRw As Long
     Dim iX As Integer, iY As Integer
     For iX = 0 To lsbWarenausgang.ListCount - 1
         If Me.lsbWarenausgang.Selected(iX) = True Then
             With Tabelle5
                 lRw = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
                For iY = 0 To Me.lsbWarenausgang.ColumnCount - 1
                     .Cells(lRw, iY + 1).Value = Me.lsbWarenausgang.List(iX, iY)
            Next iY
        End With

    End If
Next iX
End Sub

Solution

  • You mean like this (Untested)? I am assuming that the first column will always be filled if there is data in that listbox row.

    Private Sub CommandButton3_Click()
        Dim lRw As Long
        Dim iX As Integer, iY As Integer
        Dim col As Long
    
        For iX = 0 To lsbWarenausgang.ListCount - 1
            col = 1
            If Me.lsbWarenausgang.Selected(iX) = True Then
                With Tabelle5
                    If Len(Trim(Me.lsbWarenausgang.List(iX, 0))) <> 0 Then
                        lRw = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
    
                        For iY = 0 To Me.lsbWarenausgang.ColumnCount - 1
                            If Len(Trim(Me.lsbWarenausgang.List(iX, iY))) <> 0 Then
                                .Cells(lRw, col).Value = Me.lsbWarenausgang.List(iX, iY)
                                col = col + 1
                            End If
                        Next iY
                    End If
                End With
            End If
        Next iX
    End Sub