Curious: One works, the other doesn't ---- why????

spydey

Active Member
Joined
Sep 19, 2017
Messages
314
Office Version
  1. 2013
Platform
  1. Windows
I have a workbook with multiple sheets.

I have some simple code that I thought would work, but for some reason it doesn't.

Here is the code in a module:

Code:
Sub GoHome()

Dim wb As Workbook
Dim ws As Worksheet

Set wb = ThisWorkbook

For Each ws In wb.Worksheets
     ws.Range("A1").Select

Next ws

End Sub

I run it and it doesn't work.

However, if I add:

ws.Activate

Prior to ws.Range("A1").Select, then it works perfectly.

Why do I need to activate each worksheet prior to selecting that worksheet's A1 cell?

Thanks for the clarification!!

-Spydey
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Open up a workbook with say 2 sheets. From 'Sheet1' select A1 of 'Sheet2'. You cant unless you activate 'Sheet2' first. Hence why you cant in code either. You rarely need to select anyway.
 
Upvote 0
You can only select a cell on the active sheet.
That said it's very rare that you need to select things.
 
Upvote 0
Open up a workbook with say 2 sheets. From 'Sheet1' select A1 of 'Sheet2'. You cant unless you activate 'Sheet2' first. Hence why you cant in code either. You rarely need to select anyway.


Thank you @steve the fish

I figured that was the case but I could have sworn I have many examples of code where I am on one sheet and doing things to another sheet in the same workbook, but never activated the 2nd sheet.

I could be wrong .....

Thanks again!!

-Spydey
 
Upvote 0
You can only select a cell on the active sheet.
That said it's very rare that you need to select things.
@Fluff

So my ultimate goal is to bring the view up to Cell A1 on each sheet, rather than some random cell.

Would it be more appropriate to Activate cell A1 rather than select cell A1?

-Spydey
 
Upvote 0
To move A1 to the top of the screen you will need to select the sheet & activate the cell
 
Upvote 0
I often use goto

Code:
Dim wb As Workbook
Dim ws As Worksheet
Dim rng As Range

Set wb = ThisWorkbook

For Each ws In wb.Worksheets
    Set rng = ws.Range("A1")
    Application.Goto rng, True
Next ws
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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