Keeping the same cell when switching worksheets?

SethTibo

New Member
Joined
Mar 30, 2007
Messages
2
Hello all,

I would like my cursor to stay in the same cell when I switch worksheets. If I'm in B40 on worksheet one, and switch to worksheet two I would like the cursor to be in B40.

I'm using very basic excel functions with no scripting at the moment.

Any help is appreciated, Danke
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Welcome to the Board!

I believe that this is courtesy Tom Urtis from a few years ago.

In a new workbook, right-click on the Excel icon (next to "File") and select View-->Code.

Paste the following in the new window that opens on the right:

<font face=Tahoma><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Workbook_SheetSelectionChange(<SPAN style="color:#00007F">ByVal</SPAN> Sh <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> Target <SPAN style="color:#00007F">As</SPAN> Range)
    Sheet1.Range("IV65536").Value = ActiveCell.Address
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Workbook_SheetActivate(<SPAN style="color:#00007F">ByVal</SPAN> Sh <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>)
    Range(Sheet1.Range("IV65536").Value).Activate
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

ALT+Q to exit back to Excel and try it.

HTH,

Smitty
 
Upvote 0
Smitty,

I was trying to do something like that but using a global variable instead of a Cell holding the variable.

Any Idea how to do that?


Edited ---

NM, I figured it out

Code:
Dim mycell 'This goes at the top of the Module

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Range(mycell).Select
End Sub

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
mycell = ActiveCell.Address
End Sub
 
Upvote 0
Nice.

I think you'll want to add an error trap for if mycell doesn't become defined, like opening a sheet and immediately moving to another one without creating a new active cell.

Smitty
 
Upvote 0
Uh, duuhhhhh, dooohhhhh @$#%$#!!!! Smitty and jonmo1 are the same cartoon character! Yowsa!

Perhaps do mycell = ActiveCell.Address on Workbook open as well.

[Edited because smitty's two names overflowed my memory banks causing an array content overflow and mental GPF]
 
Upvote 0
ok, back from lunch...

Good idea on the error trap... I would think defining it upon opening the workbook would work..

Code:
Dim mycell 'This goes at the top of the Module

Private Sub Workbook_Open()
mycell = "$A$1"
End Sub

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Range(mycell).Select
End Sub

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
mycell = ActiveCell.Address
End Sub
 
Upvote 0
Don't you mean pennysaver and jonmo1 are using the same character?
Okay, you're right, I fixed that now. The color vs. B/W really is a leap to connect for my little mind. Had the 2 posts not happened consecutively, and on a subject that I bothered to read, I would never have made that sick connection!

Man, the pages load slowly today.
 
Upvote 0
Changing the code to something like this will let you select more than one cell and keep that range on all sheets:

'Define "myCurCell" at the top of the "ThisWorkbook" code module code only!
Public myCurCell$

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
'ThisWorkbook code module code only!

On Error GoTo myErr
Set Sh = ActiveSheet

Sh.Range(myCurCell).Select
GoTo myEnd

myErr:
MsgBox "Lost the current cell address!" & vbLf & vbLf & _
"ReSetting the active cell to the default ""A1""", vbCritical + vbOKOnly, "Address Error!"

Sh.Range("A1").Select

myEnd:
End Sub

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
'ThisWorkbook code module code only!

On Error GoTo myErr
Set Sh = ActiveSheet

myCurCell = Target.Address
GoTo myEnd

myErr:
Sh.Range("A1").Select

myEnd:
End Sub
 
Upvote 0
actually, no need to use the open event...just use an If

Code:
Dim mycell 'This goes at the top of the Module

Private Sub Workbook_SheetActivate(ByVal Sh As Object) 
If mycell = "" Then mycell = "$A$1"
Range(mycell).Select 
End Sub 

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range) 
mycell = ActiveCell.Address 
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,666
Messages
6,120,806
Members
448,990
Latest member
rohitsomani

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