UserForm and ListCount problem

DonAndress

Active Member
Joined
Sep 25, 2011
Messages
362
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hello.

I'm trying to prepare a UserForm with a list of cells - easy.

Now, I need to add a button to select all non-empty cells from the ListBox1.

I know that the list always starts from cell A4, I don't know how long the list is (each macro run may generate different amount of values), there are no empty cells between cells with values.

Right now I have it defined in the ListBox1 properties: RowSource = Sheet1!A4:A100
Then a default code for a button:

Code:
For i = 0 to ListBox1.ListCount - 1
ListBox1.Selected(i) = True
Next i

But it selects all 96 cells defined above.

How do I limit only to non-empty cells?
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
No really.
I wanted to make it possible for the user to see all values and choose between selecting all of them or just the values of his choice.
Considering this, it's not very comfortable to see only a dropdown list from a combobox instead of a list of all values at the same time in a listbox.
 
Upvote 0
Rcel = sheets(1).cells(rows.count,1).end(xlup).row
UserForm1.ListBox1.RowSource = "Sheet1!A4:A" & Rcel.Row
 
Upvote 0
Thanks a lot, but could you please also advise where to put this code?
 
Upvote 0
I could be proven wrong on this as I have not done it before.

Place it in the listbox click form control.

If you have changed the name of your listbox then change the ListBox1 to match the click name,

also drop the row from UserForm1.ListBox1.RowSource = "Sheet1!A4:A" & Rcel.Row
 
Upvote 0
But should I remove my defined range from the properties pane?
If I leave it there your code doesn't work.
If I remove it from the properties pane there is no list in the listbox.
 
Upvote 0
Try this.
Code:
Private Sub UserForm_Initialize

   Listbox1.RowSource = ""

   With Sheets(1)
       Listbox1.List = .Range("A4", .Range("A" & Rows.Count).End(xlUp))
   End With

End Sub
 
Upvote 0
I think UserForm_Initialize returns error.
It's hard to determine as the debugger can't get inside the code of the ListBox and just stops in ListBox1.Show.
 
Upvote 0
Just a typo, should be this.
Code:
Option Explicit

Private Sub UserForm_Initialize()

   ListBox1.RowSource = ""

   With Sheets(1)
       ListBox1.List = .Range("A4", .Range("A" & Rows.Count).End(xlUp)).Value
   End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,203,640
Messages
6,056,491
Members
444,869
Latest member
tulo spont

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