switching to worksheet exit/enter code instead of change sheet code.

omairhe

Well-known Member
Joined
Mar 26, 2009
Messages
2,040
Office Version
  1. 2019
Platform
  1. Windows
Hey everyone,

The following code helps in arranging the columns across multiple sheets to be same as active sheet when enter is pressed on a cell. Code being perfect however is tedious due to repetitive pressing of the Enter Key. Instead I could think of whenever the active sheet is exited and a new sheet is selected then run the code only for the new sheet. hence, the new sheet will now have the same column selected as the last sheet with even same positioning of the scrolling aswell. for mroe reference please refer to the link of my OP

https://www.mrexcel.com/forum/excel-questions/1083556-select-same-column-across-multiple-sheets.html


Code:
[COLOR=#333333][I]Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Source As Range)[/I][/COLOR]    Dim ws As Worksheet, c As Long
    c = ActiveWindow.VisibleRange.Column
    Application.ScreenUpdating = False
    For Each ws In Sheets
        ws.Select
        Cells(ActiveCell.Row, Source.Column).Select
        ActiveWindow.SmallScroll ToLeft:=Columns.Count
        ActiveWindow.SmallScroll ToRight:=c - 1
    Next
    Sh.Select
    Application.ScreenUpdating = True [COLOR=#333333][I]End Sub[/I][/COLOR]

Would it be possible?

Thank you.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
I suppose you could use the SheetDeactivate event.

Something like this perhaps.
Code:
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
Dim ws As Worksheet
Dim cl As Range

    Set cl = ActiveCell
    
    Application.EnableEvents = False

    For Each ws In ThisWorkbook.Sheets
        Application.Goto ws.Range(cl.Address), True
    Next ws

    Application.Goto sh.Range(cl.Address), True

    Application.EnableEvents = True

End Sub
 
Upvote 0
I suppose you could use the SheetDeactivate event.

Something like this perhaps.
Code:
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
Dim ws As Worksheet
Dim cl As Range

    Set cl = ActiveCell
    
    Application.EnableEvents = False

    For Each ws In ThisWorkbook.Sheets
        Application.Goto ws.Range(cl.Address), True
    Next ws

    Application.Goto sh.Range(cl.Address), True

    Application.EnableEvents = True

End Sub

When I tried the code it would bring me back to my previous sheet and cell A1 gets selected everytime. Thus I am unable to select a new sheet. Do you think MS office version has to do something with this behavior? I am using version 2019..
 
Last edited:
Upvote 0
No it's my fault.

Try removing this line.
Code:
  Application.Goto sh.Range(cl.Address), True
 
Upvote 0
No it's my fault.

Try removing this line.
Code:
  Application.Goto sh.Range(cl.Address), True

Now, once the code is run it will still select cell A1. But this time around it takes me to the last sheet of my workbook. From there on I am unable to select other sheets in my workbook.
 
Last edited:
Upvote 0
Well I'm afraid I can't think of another way to do this - Sh is the sheet that you are deactivating, there's nothing to reference the sheet you are activating.

Vice versa, if you tried the SheetActivate event Sh would refer to the sheet being activated and there would be no reference to the sheet you are deactivating.
 
Upvote 0
Well I'm afraid I can't think of another way to do this - Sh is the sheet that you are deactivating, there's nothing to reference the sheet you are activating.

Vice versa, if you tried the SheetActivate event Sh would refer to the sheet being activated and there would be no reference to the sheet you are deactivating.


That is at least good to know that it can't be done otherwise so I will have to stop looking else where and stick with my previous code.
Thank you for explaining it to me.
 
Last edited:
Upvote 0
I'm not saying it can't be done, only that right now I can't think of a way to do it.:)
 
Upvote 0
I'm not saying it can't be done, only that right now I can't think of a way to do it.:)

What if we create a hidden helper sheet by name = Column Ref
Now when enter is pressed on active sheet the column reference is copied to the "Column Ref" sheet inside cell A1.

Now when we exit the active sheet . The column reference will be in A1 of "Column Ref"

Will that work?
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,935
Members
449,094
Latest member
teemeren

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