Excel

Code to move to the same cell in either the next or previous worksheets

Ease of Use

Easy

Version tested with

2003 

Submitted by:

brettdj

Description:

Excel moves to the same cell in the next or previous sheets. If Excel is already on the first or last sheet then a message box flags this to the user. 

Discussion:

I attached these two subs to a custom toolbar. I find them very handy for moving between sheets that use a common template layout as Excel normally takes you to the used cell activated on that sheet. 

Code:

instructions for use

			

Option Explicit Sub down_one_sheet_same_cell() 'Look for error that will result if the user is on the first sheet 'and the previous sheet is non-existent On Error Resume Next Application.Goto Reference:=ActiveWorkbook.Sheets(ActiveSheet.Index - 1).Range(ActiveCell.Address) 'If there is no error then exit sub If Err.Number = 0 Then Exit Sub 'User was on first sheet MsgBox ("Action not possible, you are on the first sheet") End Sub Sub up_one_sheet_same_cell() 'Look for error that will result if the user is on the last sheet 'and the next sheet is non-existent On Error Resume Next Application.Goto Reference:=ActiveWorkbook.Sheets(ActiveSheet.Index + 1).Range(ActiveCell.Address) 'If there is no error then exit sub If Err.Number = 0 Then Exit Sub 'User was on last sheet MsgBox ("Action not possible, you are on the last sheet") End Sub

How to use:

  1. Copy the code above.
  2. Open your workbook.
  3. Hit Alt+F11 to open the Visual Basic Editor (VBE).
  4. From the menu, choose Insert-Module.
  5. Paste the code into the code window at right.
  6. Close the VBE, and save the file if desired.
 

Test the code:

  1. To add the code to custom buttons:
  2. View....Toolbars.....Customise
  3. tab to Commands
  4. Categories......Macros.....Custom Buttom
  5. drag two custom buttons to the toolbar, and then assign the two macros by clicking on the custom buttons and picking a macro
 

Sample File:

CellMove(KB18).zip 14.31KB 

Approved by mdmackillop


This entry has been viewed 154 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express