Relative vs absolute Addressing - Sheet Names

MLang

New Member
Joined
Dec 15, 2009
Messages
11
I am often confused by the behavior of my Excel marocs when it comes to the degree to which I need to specify sheet names.

It seems that at times I can Select or Activate a sheet, and then everything I want to do happens on that sheet without error. At other times, it seems that I am getting errors that I can make "go away" by re-specifying a sheet name. Sometimes one line works without explicilty specifying the sheet, but the next line fails. (I am sure that it has something to do with what I am doing in the line of code, but if I knew what that was, I wouldn't have the question. I have no immediate example... I am about to write a new procedure and I jsut want to use good programming conventions to get started.)

My question is, can someone give me a short primer on the basic convention for referencing sheets in macros? Is it good convention to repeatedly call out Sheets(mysht), or something else?

OPTION 1: Start everything with:
Sheets(shtname).Range(...

OPTION 2: Activate a sheet, and then refer to Active Sheet.
Sheets(shtname).Activate
Activesheet.Range(...

OPTION 3 (which seems to get me into trouble...) Select a sheet, and then hope that Excel stays focused on that sheet.
Sheets(shtname).Select
Range(...

Thanks!
Mike
 

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.
OK, I will use that method. As for a concrete example, here is a variant...

This works:
Sheets(SHEET_Temp_Space).Select
ActiveSheet.Range("C" & PasteRow).Select

This fails:
Sheets(SHEET_Temp_Space).Range("c", PasteRow).Select

This also fails:
Sheets(SHEET_Temp_Space).Select
Range("c", PasteRow).Select
 
Upvote 0
You can only select something on the active sheet, but there is almost never a need to select something to operate on it or with it.
 
Upvote 0
You can only select something on the active sheet, but there is almost never a need to select something to operate on it or with it.

Eureka! I have read so much regarding activate and select, but either you stated it clearer than most, or I finally really listened...

"You can only select something on the active sheet."

Thank you!
Mike
 
Upvote 0
Well ... you're welcome, but the broader point is to stop selecting stuff at all.
 
Upvote 0
But now I know why, too!
That fact that you can't select something other than on the active sheet is not the reason you shouldn't; it's that it is much slower, and the screen flashes dizzyingly, then you have to add more code to stop that, then ...

Where it's helpful to select worksheets and cells is when you're debugging, so you see where the code is operating. I do that in separate lines of code, and then delete them when things are stable.
 
Upvote 0
This also fails:
Sheets(SHEET_Temp_Space).Select
Range("c", PasteRow).Select

This should only fail if the code is in the code module of a different sheet, in which case an unqualified reference to a Range refers to the sheet containing the code; in a normal module, an unqualified reference to a Range refers to the active sheet.
 
Upvote 0
Code:
Range("c" [COLOR=red]&[/COLOR] PasteRow).Select
or
Code:
Cells(PasteRow, "c").Select
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,280
Members
452,902
Latest member
Knuddeluff

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