Set variable to active cell address

S

shelfish

I just need to set the value of a variable to the "A1" address of the
active cell. I feel like I've tried everything, but shouldn't this
work....

Dim rng as Range

Set rng = activecell [results in active cell's
value]
Set rng = activecell.address [throws error]

Thanks all,
Shelton.
 
G

Guest

rng = ActiveCell gives value
rng = ActiveCell.Address gives cell reference in A1 format
Set rng = ActiveCell is used to set the active cell as an object variable
range
then you cans do: x = rng.address to get a cell string in A1 format.

I get screwed around on these also. The best way to do it is to use a Range
or Cells reference if you can. If you have to have a variable, then don't
forget that it is a cell reference and you will need to use it like
Range(rng) when setting a range reference.
 
G

Guest

Hi,


Dim rngAddr As String
Dim rng As Range

Set rng = ActiveCell

rngAddr = rng.Address 'returns $A$1
rngAddr = rng.Address(0, 0) 'returns A1
rngAddr = rng.Address(0, 1) 'returns $A1
rngAddr = rng.Address(1, 0) 'returns A$1

'Use address like this

Range (rngAddr)
 
D

Dave Peterson

dim myAddr as string
myAddr = activecell.address(0,0)


I just need to set the value of a variable to the "A1" address of the
active cell. I feel like I've tried everything, but shouldn't this
work....

Dim rng as Range

Set rng = activecell [results in active cell's
value]
Set rng = activecell.address [throws error]

Thanks all,
Shelton.
 
S

shelfish

Hi,

Dim rngAddr As String
Dim rng As Range

Set rng = ActiveCell

rngAddr = rng.Address 'returns $A$1
rngAddr = rng.Address(0, 0) 'returns A1
rngAddr = rng.Address(0, 1) 'returns $A1
rngAddr = rng.Address(1, 0) 'returns A$1

'Use address like this

Range (rngAddr)

--
Regards,

OssieMac

shelfish said:
I just need to set the value of a variable to the "A1" address of the
active cell. I feel like I've tried everything, but shouldn't this
work....
Dim rng as Range
Set rng = activecell [results in active cell's
value]
Set rng = activecell.address [throws error]
Thanks all,
Shelton.

Brilliant! Thanks for the help.
 
S

shelfish

Very nice that you did this in one step. I knew it couldn't be a two-
step process. Thanks.
 
Joined
Jul 15, 2019
Messages
2
Reaction score
0
In attempting to execute the below lines of code (which I got from shellfish's posting above of Nov 21 2007), I get an error saying:


"Microsoft Visual Basic

Compile error:
Method or data member not found "


Dim rngAddr As String
Dim rng As Range

Set rng = ActiveCell
rngAddr = rng.Address

I am at a total loss to understand what I am doing wrong.
 
Joined
Jul 15, 2019
Messages
2
Reaction score
0
I managed to get the above to work by declaring:

DIM rng as Object

I suppose that between 2007 and 2019, the Excel VBA variable ActiveCell has become an Object.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top