Userform ListBox populated from range error if only 1 value

RobbieC

Active Member
Joined
Dec 14, 2016
Messages
376
Office Version
  1. 2010
Platform
  1. Windows
Hi there, I have a userform with a listbox which is populated from a range on another sheet.

I can get it all to work using:

Code:
Private Sub UserForm_Initialize()
    
  Dim ListRange As Range
  Set ListRange = Worksheets("Sheet7").Range("A1:A100")
[I][B]  If ListRange.Count = 1 Then[/B][/I]
[I][B]    ListBox1.AddItem ListRange.Value[/B][/I]
  Else
    ListBox1.List = Worksheets("Sheet7").Range("A1").CurrentRegion.Value
  End If


End Sub

but if there is only 1 value in my range (which is likely to happen), it give me a 381 error...

I can't see where I'm going wrong

If you can point me in the right direction, that'd be brilliant. Thanks
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
What happens if you change this :

Code:
Private Sub UserForm_Initialize()
    
  Dim ListRange As Range
  Set ListRange = Worksheets("Sheet7").Range("A1:A100")
  If ListRange.Count = 1 Then
    ListBox1.AddItem ListRange.Value
  Else
    ListBox1.List = Worksheets("Sheet7").Range("A1").CurrentRegion.Value
  End If




End Sub


To this :

Code:
Private Sub UserForm_Initialize()
    
  Dim ListRange As Range
  Set ListRange = Worksheets("Sheet7").Range("A1:A100")
 
  ListBox1.List = Worksheets("Sheet7").Range("A1").CurrentRegion.Value

  'or not the above but just this :   ListBox1.AddItem ListRange.Value






End Sub
 
Last edited:
Upvote 0
This is one possible way:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG26Feb15
[COLOR="Navy"]Dim[/COLOR] ListRange [COLOR="Navy"]As[/COLOR] Range
  ListBox1.Clear
  [COLOR="Navy"]Set[/COLOR] ListRange = Worksheets("Sheet7").Range("A1:A100")
    [COLOR="Navy"]If[/COLOR] Application.CountA(ListRange) = 1 [COLOR="Navy"]Then[/COLOR]
        ListBox1.AddItem ListRange.SpecialCells(xlCellTypeConstants).Value
    [COLOR="Navy"]Else[/COLOR]
        ListBox1.List = Worksheets("Sheet7").Range("A1").CurrentRegion.Value
   [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Thanks Logit. Unfortunately, that gives me a whole new error:

Run-time error'-2147352571 (80020005)': Type mismatch

It also has stopped working if there are more than one value in the column (which was working before) - with the same error...
 
Upvote 0
Brilliant Mick - that worked a treat!

Although I have no idea what ListRange.SpecialCells(xlCellTypeConstants).Value actually does, but it works!

Thanks very much (again)
 
Upvote 0
If there is only one item in the range, will it always be in a specific cell ? If so, can you reference that one cell instead of the whole range ?
 
Upvote 0
Thanks Logit, Micks solution here worked:

Code:
[COLOR=Navy]Sub[/COLOR] MG26Feb15
[COLOR=Navy]Dim[/COLOR] ListRange [COLOR=Navy]As[/COLOR] Range
  ListBox1.Clear
  [COLOR=Navy]Set[/COLOR] ListRange = Worksheets("Sheet7").Range("A1:A100")
    [COLOR=Navy]If[/COLOR] Application.CountA(ListRange) = 1 [COLOR=Navy]Then[/COLOR]
        ListBox1.AddItem ListRange.SpecialCells(xlCellTypeConstants).Value
    [COLOR=Navy]Else[/COLOR]
        ListBox1.List = Worksheets("Sheet7").Range("A1").CurrentRegion.Value
   [COLOR=Navy]End[/COLOR] If
[COLOR=Navy]End[/COLOR] [COLOR=Navy]Sub[/COLOR]
It now loads in if there is 1 or multiple values :)
 
Upvote 0

Forum statistics

Threads
1,223,247
Messages
6,171,007
Members
452,374
Latest member
keccles

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