Convert string variable to range variable

jerrykern

Board Regular
Joined
Jun 25, 2004
Messages
75
Why doesn't this work? I've got a string variable populated with "$A$1", and need to convert that to a range object:

VBA Code:
Dim Rng As Range
Dim RngString As String

RngString = "$A$1"

Set Rng = Range(RngString)

Rng.AddComment("Hello World")

If I debug.print RngString and Rng, I get "$A$1" and " ", respectively.

EDIT: This is in a UDF, if that affects your response at all.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
It works for me.

Showing Rng in the Debug window as "" is correct.

You would need to show a property of the object to see something such as the Name property.

1683862010720.png
 
Upvote 0
I'm not sure what I did wrong if it was working for you guys, but when I declared RngString as a Variant instead of a String, it started working.
 
Upvote 0
Perhaps it was related to the rest of the UDF code that you didn't show us.
In any case, thanks for letting us know that you have it sorted.
 
Upvote 0
Solution
Unfortunately, the entire UDF is very long and embarrassingly ugly, so I didn't post it. No doubt the code you didn't see was interfering with the code you did see somehow.
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,217
Members
453,024
Latest member
Wingit77

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