Populate Cells from ListBox

Nigel B

New Member
Joined
Oct 14, 2022
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi

I'm having difficulty in populating cells from a ListBox.

Rich (BB code):
Private Sub CommandButton1_Click()
Dim dashboard As Worksheet, ws As Worksheet
Dim RowNum As Long, wbk As Workbook, RowNum1 As Long, ColNum As Long, counter As Integer

Set wbk = ThisWorkbook
Set dashboard = wbk.Sheets("Dashboard")
Set ws = wbk.Sheets("25-07-22")
RowNum = 7
RowNum1 = 31
ColNum = 4
counter = 0

With dashboard

ActionList.Clear
ActionList.Height = 100
ActionList.Width = 1000
ActionList.ColumnCount = 4
ActionList.ColumnWidths = "200;200;200;200"

Do Until ws.Cells(RowNum, 3).Value = ""
If InStr(1, ws.Cells(RowNum, 21).Value, "Yes", vbTextCompare) > 0 Then
On Error GoTo next1

ActionList.AddItem ws.Cells(RowNum, 3).Text
dashboard.Cells(RowNum1, ColNum) = ActionList.List(counter, 0)
ActionList.List(ActionList.ListCount - 1, 1) = RowNum
ActionList.List(ActionList.ListCount - 1, 2) = RowNum1
ActionList.List(ActionList.ListCount - 1, 3) = "Test"

End If

next1:

RowNum = RowNum + 1
RowNum1 = RowNum1 + 1
counter = counter + 1

Loop

End With

End Sub

The code line in red seems to be the offender, I get Run-time error '381' Could not get the List property. Invalid property array index.

I know the solution is going to be glaringly obvious, but i'm having a senior and/or blonde moment.

Any help gladly appeciated
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi

I'm having difficulty in populating cells from a ListBox.

VBA Code:
Private Sub CommandButton1_Click()
Dim dashboard As Worksheet, ws As Worksheet
Dim RowNum As Long, wbk As Workbook, RowNum1 As Long, ColNum As Long, counter As Integer

Set wbk = ThisWorkbook
Set dashboard = wbk.Sheets("Dashboard")
Set ws = wbk.Sheets("25-07-22")
RowNum = 7
RowNum1 = 31
ColNum = 4
counter = 0

With dashboard

ActionList.Clear
ActionList.Height = 100
ActionList.Width = 1000
ActionList.ColumnCount = 4
ActionList.ColumnWidths = "200;200;200;200"

Do Until ws.Cells(RowNum, 3).Value = ""
If InStr(1, ws.Cells(RowNum, 21).Value, "Yes", vbTextCompare) > 0 Then
On Error GoTo next1

ActionList.AddItem ws.Cells(RowNum, 3).Text
[COLOR=rgb(184, 49, 47)]dashboard.Cells(RowNum1, ColNum) = ActionList.List(counter, 0)[/COLOR]
ActionList.List(ActionList.ListCount - 1, 1) = RowNum
ActionList.List(ActionList.ListCount - 1, 2) = RowNum1
ActionList.List(ActionList.ListCount - 1, 3) = "Test"

End If

next1:

RowNum = RowNum + 1
RowNum1 = RowNum1 + 1
counter = counter + 1

Loop

End With

End Sub

The code line in red seems to be the offender, I get Run-time error '381' Could not get the List property. Invalid property array index.

I know the solution is going to be glaringly obvious, but i'm having a senior and/or blonde moment.

Any help gladly appeciated
If i change the line:

dashboard.Cells(RowNum1, ColNum) = ActionList.List(counter, 0)
to
dashboard.Cells(RowNum1, ColNum) = ActionList.List(0, 0)

That populates the column of cells, but with with the first listbox value multiple times, and blank cells i the column
 
Upvote 0
You should only increment the counter variable if you add an item. Currently you do it at every iteration of the loop.

I'm also not sure why you use a mix of counter and ActionList.ListCount - 1
 
Upvote 0
Solution
You should only increment the counter variable if you add an item. Currently you do it at every iteration of the loop.

I'm also not sure why you use a mix of counter and ActionList.ListCount - 1
Thank you Rory

I couldn't see the forest for the trees

That made all the difference.
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,252
Members
452,623
Latest member
Techenthusiast

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
Back
Top