Can I simulate pressing the Enter key in an Excel macro? - excel

I am using an Excel macro to perform a text-to-speech readout of text in a specific cell:
Range("$D$10").Select
Application.Speech.SpeakCellOnEnter = True
This requires the user to press the Enter key after the macro is called for this to be activated. That means two actions by the user, since the macro is called by the user clicking a "Listen" link on the sheet. Is there a way to simulate the Enter key press within the macro? Or is there a better option for text-to-speech?

Include the Sendkeys in your Macro
Application.SendKeys "{ENTER}"
Range("$D$10").Select
Application.Speech.SpeakCellOnEnter = True

Related

Excel macro to simply type predefined text into the active cell, as plain text

Very rudimentary: I wish to press F1 (Windows OS) to print, i.e., "XZ" in the active Excel cell (as plain/unformatted text), but in such a way that I can then enter "123" immediately thereafter, hence resulting in a cell value of XZ123.
This .xls is shared by other users, four of whom are in the document adding data at any given time. We save this to a Sharepoint folder, but nobody on the team ever accesses the document via the web version of Excel. We instead just double-click the document, at which point Excel boots up from our local hard drive, but when the document loads, we can see other users' active cell(s) based on highlights around the cells with their initials.
My hope is that this silly two-letter automation can be triggered only from my computer, and not embedded in the document file itself.
Huge thanks in advance for any guidance! This seems crazy simple but I honestly can't figure out how to do it. Cheers!
Unfortunately the only way for vba to enter edit mode is to use SendKeys
You'll need to use OnKey to map the function to F1
Const StringToEnter As String = "XZ"
Sub EnterValue()
Application.SendKeys "{F2}" & StringToEnter
End Sub
' Run this once to map F1 key. Maybe call from the `Workbook_Open` event
Sub SetupMappingToF1()
Application.OnKey "{F1}", "EnterValue"
End Sub

VBA Excel - Replace Enter key with Alt+ Enter

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?

Why does VBA asks me for Macro Name when I press Run?

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)

Stop macro to allow the workbook to be edited

I have written a code to open a workbook and filter data by the number in a particular column. I need to filter "0" first and then edit the data and then filter between "+8" and "-8".
I added a message box to pause the macro but I am not able to edit the opened workbook while the message box is displayed. I tried with "Application.waiting" option.
I need to pause the macro automatically and start it manually.
Sub Filter_data()
Workbooks.open"D:\Reposrts\AAA.csv"
Activesheet.Range("I1:I100").Autofilter field:=1,Criterial:="0"
Activesheet.columns("A:Z").Autofit
MsgBox"Task ok" 'Here I need to pause the macro and allow for edit opened wb and then manually start macro for below line'
Activesheet.Range("I1:I100").Autofilter field:=1,Criterial:=">8", Opersator:=xlAnd, Criterial:="<-8"
End Sub
Split your existing Macro into two parts
The first part contains everything up to have the MsgBox"Task ok" line
The second part contains everything after that point
Create a Modeless (or "Non-Modal") User Form with your label and an "OK" button to call the second part of your split macro
"Modeless" means you can edit the workbook while the User Form is open. MsgBox is "Modal", which is why you can't edit anything. More details available from Microsoft
Replace the MsgBox"Task ok" line in the first part of your split macro with a line that opens your User Form.
Once the first half-macro has finished, and the User Form is waiting for you to click "OK", you will be able to edit workbooks. Once you click "OK", the second part will start.
It is probably best to first consider if the "edits" you need the user to make are possible via VBA, a User Form, or a DialogBox (which include the likes of the "Select Range" DialogBox, or the "Colour Picker" DialogBox)
I would recommend you not to try to edit the Workbook while a macro is running- I don't even think that is possible at all. Furthermore, try not to refer to ranges by activating/selecting ranges.
Activesheet.Range("I1:I100").Autofilter 'Instead of this use the code below
With Workbooks("AAA.csv")
.Sheets("NAME").Range("I1:I100).Autofilter
'More code
End With
Like Chillin mentioned, you could assign hotkeys to your filter macros- split them in two. Other than that, you can use buttons to activate the macros.
To assign a keyboard shortcut to a macro:
Press ALT+F8 to open the macro dialog box. Select the macro, and click on Options. In the window that opens you can assign a keyboard shortcut to the macro you selected.

Bloomberg Short Cuts in EXCEL

Is there a method to remove or at least disable Bloomberg keyboard short cuts in Excel?
For example, normally to open a filter I can press CONTROL + SHIFT + L. However, this shortcut is now being used in Bloomberg and it causes my Excel to either freeze or not load for some time.
Go to the Bloomberg tab in the workbook, select Options -> Hot Key Manager and disable Bloomberg hotkey (top right corner)
Run the following code in VBA:
Public Sub DisableBloomberg()
Application.OnKey "+^l"
End Sub
This should disable them, if you are lucky :)
Here you can read more about Application.OnKey.
If you do not know what VBA is, then in Excel do the following:
Press Alt+F11
Press Ctrl+G
Write Application.OnKey "+^l" to the opened window and press Enter

Resources