I am new to VBA. I have made a look-up function using vba and it works so fine. Here is what I wanted, I want to call my subroutine (that contains the look-up) when the Enter key is pressed. How am I going to do this? How does an event like this in VBA work?
Use Application.OnKey with a Workbook_Open event. This way, every time your workbook is loaded, you can call your macro on pressing Enter. See below:
Private Sub Worksheet_Open(ByVal Target As Range)
Application.OnKey "{RETURN}", "MyLookUp"
End Sub
Make sure you paste it in the ThisWorkbook's code. Let us know if this helps.
workbook> in the Open (and window activate) Subroutine :
Application.OnKey "{RETURN}", "Sub_Enter"
Application.OnKey "{ENTER}", "Sub_Enter"
the is a difference between numpad Enter and the other Enter.
and on workbook close (and change_window...) : (to deactivate it)
Application.OnKey "~"
Application.OnKey "{ENTER}"
You will also need a custom bit of code to know if the content of a cell has changed + ENTER. (in that case your macro Enter will have to ignore and exit sub)...
Related
I am trying to replace Enter key with Alt+Enter so that I can write multiline in cells with ease.
I have seen that there is a function Application.OnKey and Application.SendKeys and I wanted to use those something like this:
Application.OnKey "~" , Application.SendKeys("%~")
But where do I place those? Or any other ideas?
I think I agree with #Andreas, this is unlikely to work using these methods.
This is what I tried: I made a button Button1 and in its click method I assign the Enter key to send Alt-Enter as you suggest in the question:
Sub Button1_onClick()
Call Application.OnKey("~", "SendAltEnter")
End Sub
Sub SendAltEnter()
Application.SendKeys ("%~")
End Sub
This does in fact re-route the Enter key, but apparently the Alt-Enter results in another call to the method for the "Enter" part of "Alt-Enter" -- it results in an infinite loop the first time you hit enter after having clicked the button, and you have to restart your Excel application to clean it up.
I also tried this, simply using another key near Enter, namely # (at least on German keyboards) which you could hit instead of Alt-Enter:
Sub Button1_onClick()
Call Application.OnKey("#", "SendAltEnter")
End Sub
Sub SendAltEnter()
Application.SendKeys ("%~")
End Sub
The key '#' is intercepted, but not if you are in input mode in a cell, only if the focus is somewhere in the worksheet.
I think you'll have to do this outside of Excel using a keyboard remapping tool for Windows. I quickly found https://www.howtogeek.com/710290/how-to-remap-any-key-or-shortcut-on-windows-10/ by googling but know nothing about it or if it is legit or not.
Have you considered just using Shift + Enter to insert a carriage return instead?
I'm new to VBA. Below is a code to alert the user of the blank cell before printing:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
If Sheets("Sheet1").Range("F23").Value = "" Then
Cancel = True
MsgBox ("Please populate F23")
End If
End Sub
However, when I press. run, it prompts me for Macro name. May I know why? And what is the solution?
Your macro has a non-optional parameter, Cancel. When you press F5, VBA detects this parameter, determines that it cannot run the macro without it, and then opens the generic "Run Macro" screen.
If you wish to run it without triggering the Event (i.e. Printing), then you will need to put the following code into either another Subroutine, or into the Immediate Window (Ctrl+G in VBA) and then press Enter to run it from there:
Workbook_BeforePrint False
(This passes the argument False to the Parameter Cancel)
I have an Excel add-in in which I have several macros, some that have the Application.OnKey method to enable the user to use a specific keyboard shortcut to run a certain macro.
Accessing the code in the add-in requires a password.
This is a simple example of a macro:
Sub refreshMySelection()
Application.Selection.Calculate
Application.OnKey "^{ENTER}", "refreshMySelection"
End Sub
This is meant to allow the user to refresh selected Excel cells by only typing Ctrl+Enter.
However, for some reason, Excel does not automatically recognize this shortcut. Unless I manually open the add-in and execute the above macro, then only the current active workbook would do the job i.e. refresh selected cells when typing Ctrl+Enter.
# Thinkingcap, your suggestion works perfect, thanks !
Add the below in the addin
Sub Auto_Open()
Application.OnKey "^{ENTER}", "refreshMySelection"
End Sub
I am at a dead end, i have been trying to hide all macros, I have been able to do all macros that do NOT have a short Key assigned by Private Sub, but whenever i use Private on a macro that has a shortcut key assigned it doesn't not work, in fact, it disables the shortcut key
I have tried
Private Sub Workbook_Open()
Application.OnKey "+Q", "Macro1"
End Sub
and that doesn't work, yes I placed it in This Workbook.....of course if i take Private out of the macro and run it works fine. I have tried ^+Q and that doesn't work either
there has to be a way, isn't there?
Two things:
First, if you need capital "Q" then you need to do:
Application.OnKey "^+Q", "Macro1"
Also, it isn't necessary to make the macros private in order to hide them from the ribbon/macros menu. Here is a workaround that I use:
Any macro/subroutine will be 'hidden' (not displayed in the list of macros from the Ribbon/menu) if it takes at least one argument. So one workaround for you is to just add an optional, meaningless argument for each subroutine, and then you can leave them as public subs.
Example:
Sub Macro1(Optional dummy)
MsgBox "Hi!"
End Sub
The above macro should not appear in the list of available macros, but it should still work with your hotkey.
I'd like to disable changing the format of the cell except making font bold. Is there any way to achieve that?
Obviously,
.Protect AllowFormattingCells:=True enables all possible format changes.
I've thought that maybe making custom button on Ribbon could serve for this (i.e. unprotecting sheet, making the content bold and protecting again), but I wonder whether there is some more convenient approach to this problem.
I've come across similar issue at http://www.excelforum.com/excel-programming-vba-macros/676299-use-vba-to-lock-all-cell-formatting-except-background-color.html - but it also remains unsolved.
Not a perfect solution, but a possible workaround. In the ThisWorkbook module, paste these events:
Private Sub Workbook_Activate()
Application.OnKey "^b", "MakeBold"
End Sub
Private Sub Workbook_Deactivate()
Application.OnKey "^b"
End Sub
Then, in a regular module:
Sub MakeBold()
ActiveSheet.Unprotect
On Error Resume Next
Selection.Font.Bold = Not (Selection.Font.Bold)
On Error GoTo 0
ActiveSheet.Protect
End Sub
Limitation is that it only works for the keyboard shortcut, and not the ribbon button. I suppose you could create a custom button and "disguise" it as the Bold button, but it's still an imperfect workaround.