Going back to previous worksheet

G

Guest

Hi,

I have several worksheets in a workbook, each with a commandbutton which has
the same function (to take the user back to the previously active worksheet).
What I'm trying to achieve is when the user clicks the commandbutton, the
user is taken back to the previously active worksheet i.e. if user is
currently on sheet 3 and was previously working on sheet 2, when the
commandbutton is clicked on sheet 3, the user is automatically transfered to
sheet 2 and so on etc etc.

Can anyone help?

Kind regards
Martin
 
G

Guest

Firstly capture the name of the sheet you just left and write it away. In
this case to A1 on sheet 1.

Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
Worksheets("Sheet1").Cells(1, 1) = Sh.Name
End Sub

The for your button use this code

Sub Button2_Click()
Name = Worksheets("Sheet1").Cells(1, 1).Value
Worksheets(Name).Select
End Sub


Mike
 
G

Guest

In the ThisWorkbook code module, paste this:

Private oldSht As Worksheet

Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
Set oldSht = Sh
End Sub

Then, assign this macro to your buttons:

Public Sub GoBack()
oldSht.Activate
End Sub
 
D

Don Guillett

Does this idea help?
Sub lastsht()
x = ActiveSheet.Index - 1
'MsgBox x
Sheets(x).Select
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top