VBA code to go to previous page - vba

I want to write a VBA code to go to previous sheet. A code with I will be able to add a button to every sheet and by pushing it I will go back to the last (previous) sheet from which I got to the present sheet.
Example: I go to sheet1 from sheet7. Now in sheet1 there is back button through which i can go back to sheet 7. Something "similar" to the "BACK" button in Internet Explorer.
Can anyone help me with this

Put this in a regulare code module (Module1 in my case):
Public PreviousSheetNum As Integer
Sub GoBack()
Worksheets(PreviousSheetNum).Activate
End Sub
And in the ThisWorkbook module, put this code:
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
Module1.PreviousSheetNum = Sh.Index
End Sub
So basically whenever the user changes sheet, the index of the previously selected sheet is remebered, and you can go back to that sheet any time you wish.
Be warned though that changing sheet order or deleting sheets might produce unexpected behaviour. You can change from remembering the sheet index to remembering it's name, but it has some possible issues too.
Hope this helps.

Related

Application on key as home button

I'm collecting measurements in an Excel workbook.
All the measurements are displayed in separate sheets.
The average and max of every sheets' measurement is displayed in the home sheet (with the name "1")
I added a line of code to jump from the home sheet to a certain sheet by entering the sheet name in cell J23 and clicking on a button.
Now i'd like to add a physical key/button in order to go back to the home sheet.
for example with ctrl+b
unfortunately my code does not work yet.
I've pasted my code below, hope someone has a solution.
Thanks in advance
Sub Macro1()
i = Sheets("1").Range("J23").Value
Worksheets(i).Select
End Sub
Sub Home()
Application.OnKey "^b", Worksheets("1").Select
End Sub
You need to pass Application.OnKey the name of a procedure, not a VBA command as you are trying to do.
Something along these lines:
Sub Home()
Sheets(1).Select
End Sub
Sub SetUp()
Application.OnKey "^b", "Home"
End Sub
The above subs are in a standard code module. When I run SetUp, I am afterwards able to get back to Sheet1 from any other sheet by entering Ctrl+b.

Excel VBA DoubleClick

I'm working on a work project where I have an excel sheet with values that turn red when they are out of spec. What I'd like to do is be able to double click on a cell and have the sheet in my workbook pop up that has trending data on it. I have already created the sheet with the graph on it. Long story short, I'd like to be able to double click on a specific cell and have it bring up the corresponding sheet.
I have tried this code, and it will not work. Is anyone able to maybe write code from scratch or alter the code so I could use it? The cell I'm trying to click on is N9, and the sheet I want it to open is called "Alpha Final Rinse"
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, cancel As Boolean)
Sheets("Alpha Final Rinse").Select
End Sub
I'm doing this in Excel 2013. Thank you!
If you only want N9 to be able to switch focus to another worksheet, isolate Target with the Intersect method.
In the data worksheet's code sheet:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, cancel As Boolean)
If Not Intersect(Target, Range("N9")) Is Nothing Then
cancel = True
Worksheets("Alpha Final Rinse").Activate
End If
End Sub
Note that cancel = True is necessary to stop the user entering in-cell edit mode (assuming that has been enabled in Options).
Your code will work if:
it is installed in the worksheet code area of your data sheet (the sheet whose cells you are double-clicking)
macros are enabled
the file type is .xlsm rather than .xlsx

Stop Excel from changing active sheet on Sheets.Visible = True command

I have a large workbook with many sheets used for background calculation that I hide when they are not in use. The workbook is locked down (no ribbon, sheet tabs, formula bar, etc) so it cannot be tampered with when it is in use.
In Excel 2010, everything went smoothly and the active sheet was not changed, but now in Excel 2016 the following Sub changes the active sheet to the "CompCalc" sheet which leaves the user with no way to return to the sheet they need to be on to use the workbook.
Sub MakeSheetsVisible(Status As Boolean)
Dim VarSubName As String
VarSubName = "MakeSheetsVisible"
'***********************************************************
ProtectSheets (False)
Sheets("DATA_INPUT").Visible = Status
Sheets("RAW_DATA").Visible = Status
Sheets("MASTERHISTORY").Visible = Status
Sheets("CompCalc").Visible = Status
'Sheets("Event_Enter").Visible = Status
Sheets("Raw_Chart_Data").Visible = Status
End Sub
This Sub is called at the end of a Sub that is called from another Sub which can be triggered 1 of 2 ways, on a button press or ListView double click. The active sheet is only changed in the above routine when the button is used to call the initial Sub and when the routine is ran continuously (not stepped through with F8).
I can work around this issue by checking the original active sheet at the beginning of the routine and setting it back to the original at the end, but I would like to know why this is happening and if there is a way to stop it without a workaround. Any help is appreciated
It's an annoying bug in Excel 2013/2016 and as far as I know, there is no fix. A workaround I use is:
Set CurrentSheet = ActiveSheet
'Instructions here
CurrentSheet.Activate
I only ever use active sheet if it's needed for something specific. (as per MatthewD)
But surely when you've set all your sheets to visible, you can add another line to make the sheet you want to be active?
Sheets("The one you want").Activate
would do the job? (or maybe .select?)

VBA Select a sheet when the workbook is opened

The following program doesn't work when I open my workbook. What are the possible reasons?
' Select the first sheet when the workbook is opened.
Private Sub Workbook_Open()
Sheet4.Select
Sheet4.Range("B1").Select
End Sub
If you hit alt+F11 to go to the VBA code editor. On the left side, under the file name you will see the different sheets, and whatever modules you might have in there. If you go under the ThisWorkbook module
and place your code there, it will automatically run when you start up the Excel File.
you are using the "Select" Method without an Activating a Sheet First!
Yes, when you have closed your workbook last time, the current sheet will remain in the memory index and when you will again open the same workbook, the pointer search for the most recent sheet used based on the index no.
'Here is the code
Private Sub Workbook_Open()
Sheet4.Activate
Sheet4.Select
Sheet4.Range("B1").Select
End Sub
using "Select Method" without Activating the Parent Object is a Crime. lol
Hope this will help you.

Clear Contents of excell sheet from command button in Windows form (VSTO)

I have a Windows Form on a worksheet called Position. However, I would like to put a command button that clears the values of cell H45, H46 and I49 in sheet Position and also clears the values of I49 and H49 in sheet Calculations.
I am new to VSTO so I am having some trouble getting this done.
Can someone help on this? I am not sure how to reference the Calculation sheet from my button.
I'm assuming this is a document level Excel project? You can use the following:
Private Sub btnClearCells_Click(sender As Object, e As EventArgs) Handles btnClearCells.Click
With Globals.Sheet1
.Range("H45:H46").ClearContents()
.Range("I49").ClearContents()
End With
With Globals.Sheet2
.Range("H49").ClearContents()
.Range("I49").ClearContents()
End With
End Sub
Where Sheet1 would be the Positions sheet and Sheet2 would be the Calculations sheet. Change the numbers to suit your project.