AddItem to two columns in a listbox within a For each loop - add to last row in listbox(?)

arg123

New Member
Joined
Jun 8, 2016
Messages
26
Hello all and thanks for the view. Hopefully it is a fairly easy thing I'm missing.

I'm running a loop where for each cell in a column on a sheet, if conditions are met, cell.value and cell.offset(0, 1).value would be added to a list with two columns (row i, column1) and (row i , column 2) where i is the next available row in the list.

The scripting works when i is replaced with a row number but only adds to a single "row" in the List. My thought was to do something like - Range("A" & Rows.Count).End(xlUp).Offset(1, 0) - but the list range for columns and rows doesn't seem to act the same as the range in the worksheet. But I'm surly missing something.

'thought about using this as a reference range for the listbox (since this is equal to cell.value and cell.Offset(0, 1).Value) but it decouples the For Each loop respective to the range in the loop
Dim samtar As Range
Set samtar = Sheets(1).Range("D23:E3094")

Any thoughts or assistance is greatly appreciated!

VBA Code:
Private Sub PopulateReviewList_Click()

Dim i As Long
Dim samname As Range
Set samname = Sheets(1).Range("D23:D3094")

For Each cell In samname
    If cell.Offset(0, 3).Value = "Review" Or cell.Offset(0, 4).Value = "Review" Or cell.Offset(0, 5).Value = "Review" Then
        With UserForm1.ReviewSampleList
            .AddItem
                                  'when i is replaced with 1, the list only populates the first row, regardless of how many entries qualify to be added, which makes sense
                .List(i, 0) = cell.Value
                .List(i, 1) = cell.Offset(0, 1).Value
        End With
    Else
    End If
Next

End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,660
Office Version
  1. 365
Platform
  1. Windows
Try
VBA Code:
i = UserForm1.ReviewSampleList.ListCount - 1
For Each cell In samname
    If cell.Offset(0, 3).Value = "Review" Or cell.Offset(0, 4).Value = "Review" Or cell.Offset(0, 5).Value = "Review" Then
        With UserForm1.ReviewSampleList
            .AddItem
                                  'when i is replaced with 1, the list only populates the first row, regardless of how many entries qualify to be added, which makes sense
                .List(i, 0) = cell.Value
                .List(i, 1) = cell.Offset(0, 1).Value
                i = i + 1
        End With
    Else
    End If
Next
 
Solution

arg123

New Member
Joined
Jun 8, 2016
Messages
26
That worked out perfectly with one adjustment.

i = i + 1 needed to be one line above the first .List otherwise it throws an array error for the list. Again, makes sense. But with defining i inside of the loop before using i worked out great.

Quote below is adjusted for the i = i +1 location

Thank you so much!

Try
VBA Code:
i = UserForm1.ReviewSampleList.ListCount - 1
For Each cell In samname
    If cell.Offset(0, 3).Value = "Review" Or cell.Offset(0, 4).Value = "Review" Or cell.Offset(0, 5).Value = "Review" Then
        With UserForm1.ReviewSampleList
            .AddItem             [COLOR=rgb(251, 160, 38)] [/COLOR][COLOR=rgb(0, 0, 0)] [/COLOR]
                 i = i + 1
                .List(i, 0) = cell.Value
                .List(i, 1) = cell.Offset(0, 1).Value
        End With
    Else
    End If
Next
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,660
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

Forum statistics

Threads
1,141,858
Messages
5,709,039
Members
421,608
Latest member
jking1

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top