Listbox to select range on sheet

Jimmypop

Well-known Member
Joined
Sep 12, 2013
Messages
753
Office Version
  1. 365
Platform
  1. Windows
Hi All

Me again...

I have searched but nothing seems to work or the code is so complex I cannot seem to adapt to my needs.

I have a list box on a userform which is populated from a range on the worksheet.

Now when I click on an item in the listbox I just want to select a certain range on the worksheet. Each listbox entry will have a different range that gets selected...
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
If the ListBox data is in the form of a range address:
VBA Code:
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    With Me.ListBox1
        With ActiveSheet.Range(.List(.ListIndex))
            .Select
        End With
    End With
End Sub
 
Upvote 0
If the ListBox data is in the form of a range address:
VBA Code:
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    With Me.ListBox1
        With ActiveSheet.Range(.List(.ListIndex))
            .Select
        End With
    End With
End Sub

Getting a Runtime error 1004 on line

VBA Code:
Private Sub ListBox1_Click()
    With Me.ListBox1
        With ActiveSheet.Range(.List(.ListIndex)) 'getting error here'
            .Select
        End With
    End With
End Sub

So basically if I select an item in listbox e.g. "Head Office" then the code needs to select the range "A14:A28" and if I select "Training Centre" then it needs to select range "A30:A37" etc. the listbox is located on a userform and gets populated from range "O13:O21"... ranges to select does not change so I am guessing would need to work with "Select Case" or similar... and I realise I use Click and not Double click but even on double click I get same error...
 
Upvote 0
As I noted in my reply, the code assumes that the list entry is just the range address, i.e. "A1" or "A14:A28". It makes that assumption because you did not mention in your original post that it did something else. If double clicking on a list item produces a result "Head Office" , then you need to have a way to relate "Head Office" to a range address that can then be selected.
 
Upvote 0
Solution
Got it working as wanted. Thanks for assistance. I eventually had to use Case and Select Case.
 
Upvote 0

Forum statistics

Threads
1,214,990
Messages
6,122,625
Members
449,093
Latest member
catterz66

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