Application on key as home button - vba

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.

Related

Run a Macro in Excel 2010 using Enter Key, Whether Worksheet has Changed or Not, when Pressed in Particular Range

The short version of the question: How can I make a macro run in Excel 2010 when the user hits the numeric [Enter] key within a given range of cells, and whether or not they've changed the spreadsheet since the last running of the macro?
LONGER VERSION: I have a macro in Excel 2010, and I currently have a button on the sheet that the user presses to run it. The user enters 4 cells of data (in A2:D2) and runs the macro to populate several tables on several sheets with the data. The user may enter several hundred sets of data in a single sitting, and currently leaves the mouse hover over the button, enters the data, and clicks the mouse without moving it to hit the button. I'd like to simplify it by having a press of the numeric [Enter] key run the macro so they can leave their hand on the numeric key area.
I've found this question, but the answer was to use Worksheet_Change and the user didn't need the Enter Key. I also found this question, which used on the [Enter] key, but also required the worksheet to change to be of use.
My user may use the same data multiple times, so I'd like them to be able to press the [Enter] key multiple times. Currently the macro finishes by selecting A2, the first cell with data, so the user can enter new data or press it again to run it again. I want it to run from anywhere in the range of A2:D2, selecting A2 when complete (as it does now), and if [Enter] is pressed while any other cell is selected, or a cell on any other sheet is selected, I want it to move down a cell as it currently does, without running any macro.
Useful names/numbers/things:
Data range will always be in Sheet 1, cells A2:D2.
Macro to be run is named "InsertIntoTables()" and takes no parameters.
Currently no other application-wide macros or events are being used.
The macro already handles empty or improperly-filed cells in my range.
If there is anything else necessary to provide an answer, I'm happy to supply it.
This is just to get you started. Say clicking the button with the mouse runs InsertIntoTables(). I am assuming that InsertIntoTables() is in a standard module. (this makes it easy to call from another sub)
First run:
Sub NumericEnter()
Application.OnKey "{ENTER}", "InsertIntoTables"
End Sub
After this has been run:
touching the numeric keypad ENTER key will have the same effect as mouse-clickng the button
the normal ENTER key will not be affected
To restore the numeric keyboard ENTER key, run this:
Sub ClearEnter()
Application.OnKey "{ENTER}", ""
End Sub
What you must complete:
The numeric keypad ENTER key will call your sub no matter which worksheet is active. YOU must add the logic to detect which sheet is active and take appropriate actions. You must select which ever cell you want when your sub completes. You must remember to run the restore sub before quitting.
EDIT#1:
I have two worksheets; Sheet1 and Sheet2.Sheet1 is the sheet in which I want to use the "special" ENTER key.
I placed the following in a standard module:
Sub NumericEnter()
Application.OnKey "{ENTER}", "InsertIntoTables"
End Sub
Sub ClearEnter()
Application.OnKey "{ENTER}", ""
End Sub
Sub InsertIntoTables()
MsgBox "Inserting"
End Sub
I placed the following in the Sheet1 code area:
Private Sub Worksheet_Activate()
Call NumericEnter
End Sub
Private Sub Worksheet_Deactivate()
Call ClearEnter
End Sub
I placed the following in the workbook code area:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call ClearEnter
End Sub
Private Sub Workbook_Open()
Sheets("Sheet2").Activate
Sheets("Sheet1").Activate
End Sub
The purpose of the Workbook_Open() macro is to insure we start with Sheet1 active and the "special" ENTER key active.
EDIT#2:
Use this instead:
Sub ClearEnter()
Application.OnKey "{ENTER}"
End Sub
Reference:
Tims answer

Running a Macro after refreshing data

I need to run a macro after some data in a table/pivot table is refreshed. My table is reading data from a database, so I want the macro to run after the refresh. How would I do this using VBA. I've tried the following.
Private Sub Worksheet_PivotTableUpdate(ByVal target As PivotTable)
Application.Run "overrideManagers" 'Is this right?
End Sub
Sub overrideManagers() 'Macro to be run upon refresh of data
MsgBox "Hello"
End Sub
I don't get what was expected. I just get the refresh, but without the MsgBox. Even if I move the MsgBox to this.
Private Sub Worksheet_PivotTableUpdate(ByVal target As PivotTable)
MsgBox "Hello"
End Sub
I get the same results. This leads me to think that my original method for catching the update is wrong. Any ideas?
This may be a little lengthy answer. I'll try to explain clearly.
Below are the steps I followed
Created a new excel workbook with a simple table and data ( Sheet1)
Created a pivot table for that data ( Pivot table in another sheet - Sheet2)
3. Added the following code in Macro editor under Sheet2
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
MsgBox "update"
End Sub
4. Selected Sheet2, right click pivot table and refresh. Message box displayed
Hope this helps.

Programming vba excel to move cursor

I am trying to figure out how to move a cursor from one excel cell to another with a specific shortcut using vba for excel.
say from any cell if I press Alt+e it bring me to A5 for exemple. Alt+d would bring me to E3 for example etc... Any help?
First, create a VBA macro like shown below
Sub MoveToA5()
Range("A5").Select
End Sub
Then, press ALT+F8, select "Options" and assign the shortcut combination, for example CTRL+Shift+a.
In order to use ALT+ HotKey combination (for example, ALT+F5), place the Macro shown above into code Module (you have to add it to your Workbook VBAProject using Modules->Insert->Module) and also add the following code snippet to ThisWorkbook code module:
Private Sub Workbook_Open()
Application.OnKey "%{F5}", "MoveToA5"
End Sub
Hope this will help.

VBA code to go to previous page

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.

How do i show a certain excel worksheet from Userform button?

I need help with showing a specific Excel spreadsheet when the user clicks on a button called "Show in Excel".
When the user clicks i want the specific Excel worksheet to pop infront and show itself.
I have been looking for at long time now, and i need help :/
Sincerly,
Peyko.
This is what i have so far:
Private Sub CmdBtnExcel_Click()
Sheets("Sheet1").Show
End Sub
Private Sub CmdBtnExcel_Click()
Sheets("Sheet1").visible = true
Sheets("Sheet1").activate
End Sub
This will bring it 'infront'