VBA Populating Userform ListBox

M0n5ta09

New Member
Joined
Jan 21, 2016
Messages
21
Hi,

I found a post with something similar and modified it to suit my need but I am getting an '1004' error, any suggestions?

<font face=Courier New><SPAN style="color:#00007F">Dim</SPAN> ws <SPAN style="color:#00007F">As</SPAN> Worksheet<br><SPAN style="color:#00007F">Dim</SPAN> TargetRange <SPAN style="color:#00007F">As</SPAN> Range<br><SPAN style="color:#00007F">Dim</SPAN> Rng <SPAN style="color:#00007F">As</SPAN> Range<br><SPAN style="color:#00007F">Dim</SPAN> TargetCell <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> ActRow <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> c <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> Ac <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br><br><SPAN style="color:#00007F">Set</SPAN> ws = Worksheets("Data")<br><SPAN style="color:#00007F">Set</SPAN> TargetRange = ws.Range("NamedTable")<br><br>ActRow = ActiveCell.Row<br><br>TargetCell = ActiveSheet.Range("B" & ActRow).Value<br>UserForm1.LblSerial.Caption = TargetCell<br>        <br>        <SPAN style="color:#00007F">With</SPAN> UserForm1.ListBox1<br>            .ColumnCount = 7<br>            .ColumnWidths = "80,80,80,80,80,80,80"<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>        <br>        <SPAN style="color:#00007F">ReDim</SPAN> Ray(1 <SPAN style="color:#00007F">To</SPAN> TargetRange.Count, 1 <SPAN style="color:#00007F">To</SPAN> 7)<br><br>        <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> Rng <SPAN style="color:#00007F">In</SPAN> TargetRange<br>            <SPAN style="color:#00007F">If</SPAN> Rng.Value = TargetCell <SPAN style="color:#00007F">Then</SPAN><br>                c = c + 1<br>                <SPAN style="color:#00007F">For</SPAN> Ac = 1 <SPAN style="color:#00007F">To</SPAN> 7<br>                    Ray(c, Ac) = Rng.Offset(, -(7 - Ac))<br>                <SPAN style="color:#00007F">Next</SPAN> Ac<br>             <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>        <SPAN style="color:#00007F">Next</SPAN> Rng<br>        <SPAN style="color:#00007F">ReDim</SPAN> <SPAN style="color:#00007F">Preserve</SPAN> Ray(1 <SPAN style="color:#00007F">To</SPAN> 7, 1 <SPAN style="color:#00007F">To</SPAN> c)<br>        UserForm1.ListBox1.List = Application.Transpose(Ray)<br><br>UserForm1.Show</FONT>
 
Why are you using lrw in the Cells property here?

myArray(lrw, lcol) = TargetRange.Cells(lrw, lcol)

Shouldn't it be the row of cell?

myArray(lrw, lcol) = cell.Offset(0, lcol - 1).Value
 
Upvote 0

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Andrew you are a gent, Thank you.

How would I sort the dates in order of newest to old and its formatting back to dd/mm/yyyy?
 
Upvote 0
Can't you sort the data before populating the ListBox? To have the ListBox entries in the same format as the source data try:

myArray(lrw, lcol) = cell.Offset(0, lcol - 1).Text
 
Last edited:
Upvote 0
Thank you again Andrew.

I'm thinking of having either a temporary sheet to do the sorting or using a bubblesort. do you have any preference in this?
 
Upvote 0

Forum statistics

Threads
1,214,987
Messages
6,122,613
Members
449,090
Latest member
vivek chauhan

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