VBA. What is incorrect in this code? - vba

Searching found code like this
Sub Workbook_Activate()
Application.OnKey "+^{RIGHT}", "YourMacroName"
End Sub
However, when I tried, I got
How to create procedure?
I did this
Sub YourMacroName()
Selection.Copy
Sheets("V").Select
End Sub
Sub Workbook_Activate()
Application.OnKey "+^{RIGHT}", "YourMacroName"
End Sub
Got the same error
What would be correct code? Or where would be tutorial for dummies? Found some examples, but they does not work
I see my tags were modified to excel and excel-vba. But I do not use excel. Use Kingsoft Office
Changed Application.OnKey "+^{RIGHT}", "YourMacroName" to .OnKey Key:="^+M", Procedure:="YourMacroName"
and got
Then changed to OnKey Key:="^+M", Procedure:="YourMacroName" (removed .) and got error Named argument not found. And get selected Key:=

In "ThisWorkbook", you will run code that is triggered by an event. I suggest you also put it in Workbook_Open instead of Workbook_Activate as you only need to store the shortcut once.
So, in the VB Editor, open the "Project Explorer" if it is not (CTRL+R) and find "ThisWorkbook" in the folder "Microsoft Excel Objects".
Here the code should look like this
Private Sub Workbook_Open()
' CTRL + SHIFT + RIGHT
Application.OnKey "+^{RIGHT}", "YourMacroName"
End Sub
Since, inside a Module (In the Project Explorer, right click on the folder "Modules" and select Insert > Module), put the macro "YourMacroName"
Sub YourMacroName()
Selection.Copy
Sheets("V").Select
End Sub
Oh, and you should probably rename your procedure "YourMacroName" for something more obvious...

Related

How to assign keyboard shortcuts to multiple VBA macros

I've currently got a list of about 10 macros that each do something different. I basically would like to include VBA code in each of them (or as a separate list) which has the keyboard shortcut to activate each.
I know you can assign a shortcut when you create a macro in the options, but once you've saved the macro as an add-in and closed the file, you can no longer change the shortcut.
I've also tried the application.onkey function, but that only seems to work for 1 macro and I don't see how to add to the list.
Hopefully this makes sense but please ask for clarification if needed
Thanks,
Thomas
This is the code I'm using for the only Application.OnKey that works:
Sub Workbook_Open()
Application.OnKey "^+{\}", "IF_Error_Wrap"
End Sub
Sub Workbook_BeforeClose()
??????
End Sub
You need to repeat the OnKey line for each assignment, and be sure to clear the shortcuts when the file closes using BeforeClose like this:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.OnKey "^+{\}"
End Sub
If it's an add-in, you could alternatively, and probably more safely, use the Workbook_AddinUninstall() event to clear the keys.

Unable to run macro from Application.Onkey

I am unable to run macro from Application.Onkey option. The following message is displayed upon pressing TAB key: "Cannot run the macro "C:\...\Desktop\test.xlsm!abc'. The macro may not be available in this workbook or all macros may be disabled."
However, upon pressing Enter key, selection goes 1 row below (as it always does) instead of running the same macro.
I have enabled all macros from Trust Settings and checked "Trust access to the VBA project object model". The file have been saved with xlsm extension.
All of the following macros are in thisWorkbook:
Private Sub Workbook_Open()
Test1
End Sub
Sub Test1()
Application.OnKey "{TAB}", "abc"
Application.OnKey "{ENTER}", "abc"
End Sub
Sub abc()
MsgBox "TAB"
End Sub
Can anyone help me with this one?
To make your code running, move Sub Test1 and abc to a module.
Then if you have 2 Enter keys on your keyboard, press the one on the numeric keypad (the smaller one) and your code should work. To use the big one, use ~ like this:
Sub Test1()
Application.OnKey "{ENTER}", "abc" 'The small one
Application.OnKey "~", "abc" 'The huge one
End Sub
Application.OnKey MSDN
I assume your macro abc() is in "Ten_skoroszyt" ("ThisWorkbook") module, it should be transfered to separate (new) module.

VBA Center Across Selection Hotkey

I am trying to create a hotkey shortcut for the "Center Across Selection" functionality. The "Merge Cells" functionality looks nice and all, but is horrid to use in practice because of the problems it creates with Macros and such. So, naturally, center across selection is the go-to tool. I use the keyboard to navigate in Excel, so it would be very nice to have a custom shortcut to make this selection easier and faster, since it is used often.
I have some code for this, which I believe should work, but for some reason, it is not:
In PERSONAL.XLSB - Module 1
Private Sub WorkBook_Open()
Application.OnKey "^q", "center_across_selection"
End Sub
In PERSONAL.XLSB - Module 3
Sub center_across_selection()
With Selection
'converts centered text to regular format
If .HorizontalAlignment = xlCenterAcrossSelection Then
.HorizontalAlignment = xlGeneral
'converts regular text to centered across selection
Else
Selection.HorizontalAlignment = xlCenterAcrossSelection
End If
End With
End Sub
Then, once I save this, exit out of the workbook (and all other workbooks as well), reopen the workbook, and try to use the Ctrl+Q shortcut, nothing happens to my cells! Please help me find where I am going wrong.
Move Private Sub WorkBook_Open() from MODULE 1 to the workbook code area.
You've implemented the macro in a standard module; Workbook_Open means to handle the Workbook object's Open event, but a standard module doesn't do that (the ThisWorkbook class/Excel object module does, as shown in Gary's (well, his student's anyway) answer).
So either you move the macro to ThisWorkbook's code-behind, or you rename the macro to Excel4-compatible Auto_Open:
Private Sub Auto_Open()
Application.OnKey "^q", "center_across_selection"
End Sub
Note that this Auto_Open macro will run regardless of the state of Application.EnableEvants when the workbook is opened.

Maro to always copy and paste as formulas by default in Excel

I need help to check this macro that intends to copy and paste without formatting. It doesn't work fine.
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
On Error Resume Next
Target.PasteSpecial xlPasteFormulas
Application.CutCopyMode = True
End Sub
How do I make Ctrl-V to paste without any format? I want to keep the excel clean and tidy, so users can copy and paste values and formulas without formatting.
Thanks!
If you want to make your own, custom Ctrl-V, well, you can achieve it this way:
' Code Module ThisWorkbook
Private Sub Workbook_Open()
Application.OnKey "^v", "ThisWorkbook.PasteWithoutFormat"
End Sub
Private Sub PasteWithoutFormat()
If Application.CutCopyMode Then Application.Selection.PasteSpecial xlPasteFormulas
End Sub
Try this: Assigning Macros to Short-Cut Keys in Excel
You'll need to work out how users have access to the macro - there are a few ways to do this and I'm not so sure what will work in your environment, nor your access level to make these options work.
I suppose I'd build a very simple ADD-IN in which I'd embed the macro and there also, assign the macro shortcut to override CTRL + V. Then deploy the ADD-IN according to your company policies.
However, we need more details from you to fully implement the solution.
I went for the easiest way. I set up Ctr-v (in macro's options) to run the following code:
Sub Pstfrmul()
Selection.PasteSpecial xlPasteFormulas
End Sub
Many many thanks for all your answers and comments. Best,
GerĂ³nimo

Why does this code work with F1 and not CTRL-M (for example)

I found this code online, and it works like a charm:
Sub Auto_Open()
Application.OnKey "{F1}", "WorkbooksHandler"
End Sub
Sub WorkbooksHandler()
On Error Resume Next
If ActiveWorkbook.Sheets.Count <= 16 Then
Application.CommandBars("Workbook Tabs"). _
ShowPopup 500, 225
Else
Application.CommandBars("Workbook Tabs"). _
Controls("More Sheets...").Execute
End If
On Error GoTo 0
End Sub
I press F1 and it opens a dialogue with all the sheets. I can select the sheet I want and it goes there.
If I change the code just slightly, and use:
Sub Auto_Open()
Application.OnKey "^{m}", "WorkbooksHandler"
End Sub
Now control-m opens with dialogue showing me the sheets, but when I click on the sheet I want excel doesn't navigate there. Why should the trigger make any difference, and make the execution not work?
Edit: By the way, the code also works fine when I run it manually with F5 as well, just not with the onkey control-m.
The problem appears to be that the Control key, when used with OnKey persists through the whole command even though you've no doubt released the key. This has no effect on most things that you do, but inexplicably effects the More Sheets popup. Take this code
Sub Auto_Open()
Application.OnKey "^m", "WorkbooksHandler"
End Sub
Sub WorkbooksHandler()
SendKeys "{RIGHT}"
End Sub
All that does is press the right arrow key. But it has the effect of pressing Ctrl+Right which takes you to the edge of you worksheet (for a blank worksheet). So the Control part of ^m is sticking around through the execution of WorkbooksHandler.
This happens manually also. Hold down the control key, right click on the sheet navigation buttons, select More Sheets, select a sheet. It doesn't move to that sheet when you have Control held down.
I tried all manner of SendKeys, OnTime, and DoEvents, but couldn't trick Excel into releasing the Control key. I'll bet you could find a Windows API that would do the trick, but it's probably easier to simply pick a key combination that doesn't use Control.
Make sure
Sub WorkbooksHandler()
On Error Resume Next
If ActiveWorkbook.Sheets.Count <= 16 Then
Application.CommandBars("Workbook Tabs"). _
ShowPopup 500, 225
Else
Application.CommandBars("Workbook Tabs"). _
Controls("More Sheets...").Execute
End If
On Error GoTo 0
End Sub
is pasted in Modules,
Then
Compile and Run the Auto_Open() manually then try the shortcut