Moving to a previous sheet macro

Balrajss0121

New Member
Joined
May 6, 2020
Messages
11
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi, I have a macro that enables me to move to the previous sheet. However when back at the start I would want it to loop back to the last sheet in the workbook. The current code is displayed below and it checks for any hidden sheets and it misses out the first sheet which is the home sheet. Any help would be appreciated in solving this :)

Sub PreviousSheet()

Dim i As Integer
i = ActiveSheet.Index - 1
If i > Sheets.Count Then i = 2
Sheets(i).Activate

End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
That code neither misses the first sheet, nor checks for hidden sheets.
This will do what you have asked for
VBA Code:
Sub PreviousSheet()

Dim i As Integer
i = ActiveSheet.Index - 1
If i = 1 Then i = Sheets.Count
Sheets(i).Activate

End Sub
 
Upvote 0
Thank you for your response. I tried your code but unfortunately, it does cycle back through the sheets, as it stops when it gets to the first sheet. I would want it to carry on iterating through the sheets backwards without stopping, thanks.
 
Upvote 0
I think you were asking to have the macro check if the sheet was not hidden and you wanted to step backward through the sheets and ignore the first sheet

VBA Code:
Sub PreviousSheet()

  Dim i As Long
  Dim X As Long
  
  i = ActiveSheet.Index - 1
  
  For X = i To 1 Step -1
    If X = 1 Then X = Sheets.Count
    If Sheets(X).Visible = True Then
      Sheets(X).Activate
      Exit For
    End If
  Next X

End Sub
 
Upvote 0
No it doesn't stop when it gets to the first sheet, in fact it doesn't go to the first sheet.
 
Upvote 0
To ignore hidden sheets use.
VBA Code:
Sub PreviousSheet()

Dim i As Integer
i = ActiveSheet.Index - 1
Do Until Sheets(i).Visible
   i = i - 1
Loop
If i = 1 Then i = Sheets.Count
Sheets(i).Activate

End Sub
 
Upvote 0
I think you were asking to have the macro check if the sheet was not hidden and you wanted to step backward through the sheets and ignore the first sheet

VBA Code:
Sub PreviousSheet()

  Dim i As Long
  Dim X As Long

  i = ActiveSheet.Index - 1

  For X = i To 1 Step -1
    If X = 1 Then X = Sheets.Count
    If Sheets(X).Visible = True Then
      Sheets(X).Activate
      Exit For
    End If
  Next X

End Sub
I think you were asking to have the macro check if the sheet was not hidden and you wanted to step backward through the sheets and ignore the first sheet

VBA Code:
Sub PreviousSheet()

  Dim i As Long
  Dim X As Long
 
  i = ActiveSheet.Index - 1
 
  For X = i To 1 Step -1
    If X = 1 Then X = Sheets.Count
    If Sheets(X).Visible = True Then
      Sheets(X).Activate
      Exit For
    End If
  Next X

End Sub
Hi Jeffrey, I appreciate your response. Yes that is what I meant, I don't think I worded it very well. Your solution worked a treat. Thank you very much.
 
Upvote 0
To ignore hidden sheets use.
VBA Code:
Sub PreviousSheet()

Dim i As Integer
i = ActiveSheet.Index - 1
Do Until Sheets(i).Visible
   i = i - 1
Loop
If i = 1 Then i = Sheets.Count
Sheets(i).Activate

End Sub
Hi Fluff, thanks for the response, your second solution worked great. I most likely haven't worded it right and trust your judgment on the first solution working. Thank you :)
 
Upvote 0
One problem with my code is if the first sheet is hidden, use this instead.
VBA Code:
Sub PreviousSheet()

Dim i As Integer
i = ActiveSheet.Index - 1
Do Until Sheets(i).Visible
  i = IIf(i = 1, Sheets.Count, i - 1)
Loop
Sheets(i).Activate

End Sub
 
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