Jed
Jed

Reputation: 15

Using addresses (assigned as separate variables) into a formula

I keep getting a run-time error '13' when I try to run this code:

FirstAddress = Range("P12").Address
SecondAddress = Range("C13").Address

MyVar = FirstAddress - SecondAddress

Would anyone happen to know what I can do to fix this? I know that the run-time error code means that I'm doing something that is not allowed, but I want to save addresses (not values) to a variable that I can easily call upon later.

Thanks in advance!

Upvotes: 1

Views: 41

Answers (1)

David G
David G

Reputation: 2347

The addresses you are storing are in fact strings. That means that they are not range objects, and you cannot use them as if they were. The following statement will go to the range your address specifies:

MyVar = Range(FirstAddress).Value - Range(SecondAddress).Value

I highly recommend you dimension your variables before using them.

Dim MyVar as double

for example.

Upvotes: 1

Related Questions