Cell reference as variable?

Magriza

Well-known Member
Joined
Nov 16, 2005
Messages
509
Office Version
  1. 365
Platform
  1. Windows
Does anyone know if it's possible to set the active cell on one sheet as a variable so that it can be used to select the same cell on another sheet? So that if I select, say, C5 on Sheet1 and then run the macro it will automatically select Sheet2 and cell C5? Both cells would contain no data. I know I can do it if I have the cell reference typed into the cell:
Code:
Dim i As String
i = ActiveCell

Sheets("Sheet2").Activate
Range(i).Select

However, I am unable to see how I can get it to work if the cell on Sheet1 is blank.

I'm sure it's simple but so am I so please help! :confused:
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hi Magriza,

Welcome to the Board. With my limited knowladge i used this in my code to do the trick:

Code:
Sub activate()
Dim i
Dim R
Dim C
i = ActiveCell
R = ActiveCell.Row
C = ActiveCell.Column
Sheets("Sheet2").Select
Cells(R, C).Select
End Sub

I am sure there is a better way to do it but I couldn't find it at the time

Cheers,
Matt
 
Upvote 0
How about this?
Code:
Dim i As String 
i = ActiveCell.Address

Sheets("Sheet2").Range(i).Select
 
Upvote 0
Thank you to both of you! Worked perfectly. Had to select sheet2 before the range in the second solution before it worked...

Code:
Dim i As String
i = ActiveCell.Address

Sheets("Sheet2").Select
Range(i).Select

I'm going to sacrifice a floppy disc on my mouse mat altar in thanks! :biggrin:
 
Upvote 0
Magriza said:
Had to select sheet2 before the range in the second solution before it worked...
What are you actually trying to do?

You shouldn't need to select anything to work with a range/cell.
 
Upvote 0
Sorry, didn't really explain myself well. The second part of your code:

Code:
Sheets("Sheet2").Range(i).Select

Returned the following error:

Run-time error '1004':

Select method of Range class failed.

However, as soon as I instructed the code to select sheet2 first it worked fine!
Code:
Sheets("Sheet2").Select
Range(i).Select

Thanks once again for your help.
 
Upvote 0
Magriza said:
Sorry, didn't really explain myself well. The second part of your code:

Code:
Sheets("Sheet2").Range(i).Select

Returned the following error:

Run-time error '1004':

Select method of Range class failed.

However, as soon as I instructed the code to select sheet2 first it worked fine!
Code:
Sheets("Sheet2").Select
Range(i).Select

Thanks once again for your help.

You cannot select both the sheet and range in the same Select statement. But you can probably avoid selects altogether, as has been mentioned...
 
Upvote 0

Forum statistics

Threads
1,214,516
Messages
6,119,979
Members
448,934
Latest member
audette89

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