Where/how do I register a dynamic range

davidmyers

Board Regular
Joined
Jan 29, 2017
Messages
88
Office Version
  1. 2016
Platform
  1. Windows
Hi I want to populate a userform combo/listbox - RowSource from a dynamic range that I created in a Sub according to which cell is clicked on (using activecell.offset). What do I specify for RowSource? Do I need to register the range name - how?
Thanks for any help
David
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Something like
Code:
ComboBox1.RowSource = MyRange.Address
 
Upvote 0
Hi Fluff, Thanks very much for this.

Here's my code:
Code:
Private Sub CreateRowSource()
Dim frstRow As Long, lstRow As Long, cnt As Long
Dim rng As Range
   cnt = 0
    Do While (ActiveCell.Offset(cnt, 0).Value = ActiveCell.Offset(cnt - 1, 0).Value)
       cnt = cnt - 1
    Loop
    frstRow = ActiveCell.Row + cnt
    cnt = 0
    Do While (ActiveCell.Offset(cnt, 0).Value = ActiveCell.Offset(cnt + 1, 0).Value)
       cnt = cnt + 1
    Loop
    lstRow = ActiveCell.Row + cnt
    Set rng = Application.Range("Complaints!A" & frstRow & ":B" & lstRow)
    MsgBox "rng = " & "Complaints!A" & frstRow & ":B" & lstRow & " address: " & rng.Address
    UserForm8.ComboBox1.RowSource = rng.Address
    UserForm8.Show
End Sub

This is what I see in the message box:

"rng = Complaints!A459:B462 address: $A$459:$B$462"


My Combobox appears empty, also nothing appears in the RowSource property

There is a UserForm8 and a ComboBox1

What am I doing wrong?

Thanks
David
 
Upvote 0
Try putting that code into the Userform_Initialise event
Code:
Private Sub UserForm_Initialize()
Dim frstRow As Long, lstRow As Long, cnt As Long
Dim rng As Range
   cnt = 0
    Do While (ActiveCell.Offset(cnt, 0).Value = ActiveCell.Offset(cnt - 1, 0).Value)
       cnt = cnt - 1
    Loop
    frstRow = ActiveCell.Row + cnt
    cnt = 0
    Do While (ActiveCell.Offset(cnt, 0).Value = ActiveCell.Offset(cnt + 1, 0).Value)
       cnt = cnt + 1
    Loop
    lstRow = ActiveCell.Row + cnt
    Set rng = Application.Range("A" & frstRow & ":B" & lstRow)
    MsgBox "rng = " & "Complaints!A" & frstRow & ":B" & lstRow & " address: " & rng.Address
    Me.ComboBox1.RowSource = rng.Address

End Sub
 
Upvote 0
Hi Fluff,
Forgot that combobox is a dropdown and list box displays the list. I've changed to a listbox and it works.
Apologies for wasting your time
Thanks for your help
David
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,620
Messages
6,120,554
Members
448,970
Latest member
kennimack

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