Maintaining row position when switching sheets

jimpappas

New Member
Joined
Jan 19, 2016
Messages
2
Hello. I'm new to visual basic and have a question I hope somebody can answer. I have an excel workbook with 2 sheets. There are a ton of rows in each sheet. I wish to be able to go from one sheet to another and automatically maintain row position. For example, if I am in sheet A, row 100 and go to sheet B, it will start me in the A column of row 100. Any help is much appreciated! Jim
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
It is all in how the code is written. The main thing to remember is that the computer is dumb as a rock. It is super fast, but it has to be told what to do. It also has a great memomory, so once it is told something it won't forget until something causes that memory to be erased or replaced with a new value. So, one way to have that same row (eg, row = 10) value when changing sheets is to give the row a variable value:
Code:
Dim myRow As Long
myRow = 10
Sheets(1).Rows(myRow).Copy
Sheets(2).Cells(myRow, 1).PasteSpecial xlPasteValues
Application.CutCopyMode = False

There are other examples, but basically, the variable is the means to control row number values between sheets. You can also find rows by relative proximity to something in a known location from one sheet to another. In the simplest form, the code writer translates to code the same logic a human uses to do it manually.

I suggest you browse some of the postings on this site to pick up some of the techniques used by different responders.
 
Last edited:
Upvote 0
Well, you could create a little macro to do what you want. So if you use the Macro, which you could attach to a button, icon, keyboard shortcut, etc, you could just use that to move to the same address on the other sheet. That code will look something like this:
Code:
Sub MySheetMove()

    Dim mySheet As String
    Dim myAddress As String
    
'   Capture sheet index
    mySheet = ActiveSheet.Index

'   Capture current address
    myAddress = ActiveCell.Address
    
'   Go to other sheet
    If mySheet = 1 Then
        Sheets(2).Activate
    Else
        Sheets(1).Activate
    End If

'   Go to address
    Range(myAddress).Select
        
End Sub
 
Last edited:
Upvote 0
Try this...

1) Press ALT+F11 to go into the VB editor

2) Find the "ThisWorkbook" item in the list inside the "Project-VBAProject" window and double click it.

3) Copy/paste the following code into the code window that just opened up...
Code:
Public CurrentlySelectedRow As Long

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
  If CurrentlySelectedRow = 0 Then CurrentlySelectedRow = ActiveCell.Row
  Application.Goto Rows(CurrentlySelectedRow), True
End Sub

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
  CurrentlySelectedRow = Target(1).Row
End Sub

4) That's it... each page you visit will automatically start with the last active row from the sheet you just left at the top of the worksheet.

5) If you are using XL2007 or above and want this functionality to remain with the workbook the next time you open it, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
 
Last edited:
Upvote 0
Pretty slick, Rick!
I was wondering if you could use events to do it, but wasn't quite sure how to pull it off. I haven't used those particular Workbook events very much.
 
Upvote 0
Pretty slick, Rick!
Thanks Joe!


I was wondering if you could use events to do it, but wasn't quite sure how to pull it off.
The key is the public variable so that conditions that exist during one event can be used in the next event. I made extensive use of this technique back in my volunteering days for the compiled version of Visual Basic (in the early to mid-2000s).
 
Upvote 0
Rick
I really appreciate that. It is super slick. I would like to follow-up on one more thing as this has one glitch (that is my fault, as I oversimplified things slightly). I actually have 5 worksheets (call them A,B,C,D,E) with the first 3 essentially being instructions. That said, I would like worksheet D and worksheet E to maintain row position but not worksheets A-C, as this would take the user way below the instructions. I would imagine that this can be incorporated somehow into the IF statement, and can definitely investigate that. However, if you have some simple guidance or helpful hints on that I would again appreciate the assistance. This is an incredible forum! Jim
 
Upvote 0

Forum statistics

Threads
1,223,247
Messages
6,171,004
Members
452,374
Latest member
keccles

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