Convert to contents

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
2,110
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
If I have a string e.g. "Sheet1!$B$2", is there a Excel function that will give me
the contents of that cell? Or must it be a UDF, in which case , no problem.
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Gee, that's be so close to perfect, but you can't use it in UDF can you ?
 
Upvote 0
Maybe it would help if you explained what your ultimate goal is. In a UDF, you'd use something like:

Code:
x = Sheets("Sheet1").Range("B2")
 
Upvote 0
You could do this:

Code:
x = Evaluate("INDIRECT(""Sheet1!$B$2"")")

or you could write some code to parse the string into its components and use the code from post 4. But you haven't answered the question. What is your ultimate goal? There are a lot of ways to reference cells, from the worksheet or from VBA. Each way has advantages and disadvantages. Which one to use depends on what you're doing. The Evaluate method here really isn't a great way to do it.
 
Upvote 0
You could do this:

Code:
x = Evaluate("INDIRECT(""Sheet1!$B$2"")")
You do not need the INDIRECT function inside the Evaluate function (nor is the absolute reference needed either); this should return the same thing...

x = Evaluate("Sheet1!B2")

And if Sheet1!B2 is a fixed, never changing reference with in the procedure, this can be shortened to this...

x = [Sheet1!$B$2]
 
Last edited:
Upvote 0
I wanted to use this in various Functions to get the value of that location, and that single argument format seemed practical . I do have a routine
that changes "Sheet1!$B$2" to Sheets("Sheet1").Range("B2") , but maybe it wasn't needed if something already exists.
Didn't know about Evaluate, so about to try it out. Perhaps see what the disadvantages are.
Thanks for the replies :)
 
Upvote 0
How is the string generated? Typed in, the result of a formula, a selection object? Generally speaking, it's better to pass a range object instead of a string with the address of your range. With a range object you can use Target.Value, or Target.Offset(1,0), or any number of dozens of built-in methods, saving you the trouble of coding them yourself.

In any case, it looks like you have something to work with, glad we could help.
 
Upvote 0
You don't need Evaluate or Indirect in code, you can just use Range:

Code:
application.range("Sheet1!B2")
 
Upvote 0

Forum statistics

Threads
1,224,927
Messages
6,181,799
Members
453,067
Latest member
cernytomas

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