Programming vba excel to move cursor - vba

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.

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.

Assign keyboard shortcut to macro inside the code

I wrote a macro that change data in my excel, and I wrote other macro that
insert the first macro (I made) inside 1000+ different excels that don't have this macro. In regular situation I know how to assign keyboard shortcut to this macro: via "option" in Macro windows.
but my goal is that the keyboard shortcut for Sub Single in Macro1 will remains also in all other excels, I think the best way is to do it in my code.
I read the following articles but cant figure out where they want me to put the following code, so it will actually work?
Thanks in advance for all helprs ..
https://msdn.microsoft.com/en-us/library/office/ff197461.aspx
and that one:
Excel VBA: Assign keyboard shortcut to run procedure
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.OnKey "^b", "Macro1"
End Sub

How to run automatically Excel macros?

I have created a cricket sheet in excel. I have also created a macro which is used to display a message when overs bowled are equal to 20 (A T-20 Match). My problem is that when the over get to 20 no message is shown unless I press the shortcut key. Is there anything which I can do so that whenever overs reach to 20 a message automatically displays.
My code is:
Sub Innings()
If Range("H43") = 20# Then
MsgBox "Innings Completed"
End If
End Sub
Include the following macros in the worksheet code area:
Private Sub Worksheet_Calculate()
Call Innings
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("H43")) Is Nothing Then
Call Innings
End If
End Sub
Because they are worksheet code macros, they are very easy to install and automatic to use:
right-click the tab name near the bottom of the Excel window
select View Code - this brings up a VBE window
paste the stuff in and close the VBE window
If you have any concerns, first try them on a trial worksheet.
If you save the workbook, the macros will be saved with it.
If you are using a version of Excel later then 2003, you must save
the file as .xlsm rather than .xlsx
To remove the macros:
bring up the VBE windows as above
clear the code out
close the VBE window
To learn more about macros in general, see:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
and
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
To learn more about Event Macros (worksheet code), see:
http://www.mvps.org/dmcritchie/excel/event.htm
Macros must be enabled for this to work!
Gary's Student's code is working well. You just need to write Worksheet_Change and Worksheet_Calculate to the List1 not to the Module1. I add a picture.

Add-in with predefined shortcut key?

Firstly, this is all in Excel 2007, FYI.
I'm trying to set up an Add-in that will use a shortcut key to run a simple macro. I want to define that shortcut key so that I can simply tell my users to press "ctl+alt+l" to run the macro, hopefully keeping explanations to a minimum on my team since the user interface would be unified.
My code to set up the shortcut key is right here:
Sub LiteralizeShortcutKey()
Application.OnKey "%^l", "Literalize"
End Sub
When I run the macro it works just fine.
I then create a Workbook macro to autoload when the excel file opened.
Private Sub Workbook_Open()
LiteralizeShortcutKey
End Sub
This works fine when I open the workbook. But it all falls apart when I export the workbook to a .xlam and then load it into another workbook. My little LiteralizeShortcutKey() Sub won't load, so I can't use my predefined shortcut key.
Thoughts?

How can I run a Macro when the value in a specific cell changes?

Let me preface by saying that I am very new to VB...
I am trying to run a macro whenever the value in a certain cell changes. I've read up on how to do this, but can't seem to get it to work. I have entered the following code into the private module of the worksheet object:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("$C$5")) Is Nothing Then
Application.Run _
"'Amex Payments_Experiment.xlsm'!SelectCells"
End If
End Sub
C5 is the cell I am trying to monitor for change.
"SelectCells" is the macro I want to run.
"Amex Payments_Experiment.xlsm" is the name of the file.
When I change the value in C5 nothing happens. Some help would be great. Thanks!
UPDATE:
Cyberkiwi - No, that is not exactly how I did it, but when I follow you're instructions I do find the code where you say it should be. To get to the private module of the worksheet object I right clicked the sheet tab at the bottom, selected "view code", then selected "worksheet" from the dropdown in the top center of the page.
User587834 - Yes. Macro's are enabled.
Any other suggestions?
if you use Excel2007 be sure that macros are enabled, by default Excel 2007 deactivate macro execution for new workbook.
for that try to execute any other macro to be sure that macros are enabled.
I have entered the following code into the private module of the worksheet object:
How exactly have you done that? As below?
Alt-F11 to switch to code view
On the left, double-click on the target sheet
On the right, do you see the code you entered? if not, proceed to next step
Paste the code block
This code works Ok for me
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("$C$5")) Is Nothing Then
MsgBox "hello"
End If
End Sub
Maybe the problem is with your Application.Run:
- select the line containing the intersect and click F9 to switch on Debug, then try changing a cell to see if you get to the sub.
If you never reach that line then you have not got the code in the worksheet clkass module, or you have switched off events, or macros are disabled or ...
Check the Application.EnableEvents property, and set it to True if required.