VBA Help - Assigning Variable to a Selected Range

Lidsavr

Active Member
Joined
Jan 10, 2008
Messages
330
I am running Excel 2007 and writing code that some Excel 2003 users will run later.

I am trying out how to assign a variable to a selected range. Here is what I have so far. I keep getting and error on the line in red.

Code:
    Dim SRng As Range

    Range("A2").Select
    Range(Selection, Selection.End(xlDown)).Select
[COLOR="red"]    SRng = Selection[/COLOR]

I will later be assigning the SRng variable to a listbox using the following:

Code:
    UserForm1.ListBox1.RowSource = SRng

I do not know if the above will work as I have not worked my way through debugging the code to the last example.

What I need help with at this time is assigning the variable.

Can someone please help me with the correct code?

Thank you,

Charles
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Try

Set SRng = Selection


Also, I'm pretty sure the RowSource requires a string like A1:A10 or whatever...

so the next line would be

UserForm1.ListBox1.RowSource = SRng.Address


Hope that helps.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,275
Members
452,902
Latest member
Knuddeluff

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