assign a shortcut key AND hide macro - excel

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.

Related

Temporarily modify where the return key moves the selection

I want, when I press the return key, to move the selection one cell to the right.
I know this option can be changed under options in Excel.
I want to set this option programmatically so it only works with one workbook when it loads, but not with other workbooks.
So the option will be reset back to the original "down" action when the workbook is closed.
While maybe not completely recommended – using TAB would be how most people work this – You can change the option with the MoveAfterReturnDirection property.
So if you would have something like
Private Sub Workbook_WindowActivate(ByVal Wn As Excel.Window)
Application.MoveAfterReturnDirection = xlToRight
End Sub
Private Sub Workbook_WindowDeactivate(ByVal Wn As Excel.Window)
Application.MoveAfterReturnDirection = xlDown
End Sub
in the code for the workbook, that should accomplish something along the lines of what you are asking.
Notice that this will change the setting for anyone who are using the workbook, and if they had anything else than Down set, then that will change.
You could work around this by capturing the original value using a global variable, but I wouldn't bet my life on that it wouldn't mess up someones setting at some point.
Actually, testing the latter managed to somehow set my settings to "left", after closing the book.
An even worse way to do it would be to completely change the function of the Enter key, using the OnKey method. Making the Enter key return a Tab key press. The approach would be the same, but using OnKey and then creating a sub that uses something like SendKeys to return the Tab press.

Blocking charts from removing

I have a chart that I want user not to delete. Yet I'd like to give him option to move or resize it if he wants to, so I think, that option locked in properties is not appropriate here (I use secure worksheet too).
Is there any possibility here to make code that works when user clicks Del on the keyboard, then some msgBox with information with error, that this chart is not deletable appears and then discard this action of removing?
It is impossible to prevent Chart deletion, while simultaneously enabling it to be moved
There are two following options, which *sorta* achieve what you want:
Enable Sheet Protection (Review -> Protect Sheet) which will achieve the following:
prevent the Chart from being deleted
unfortunately also prevent chart from being moved
Use Application.OnKey to detect press of Delete
Private Sub Worksheet_Activate()
Application.OnKey "{DELETE}", "PreventDeletion"
End Sub
Private Sub PreventDeletion()
MsgBox ("Deletion not allowed in this worksheet")
End Sub
this allows the chart to be moved
unfortunately only creates an illusion of delete protection. If user uses mouse2 and selects the delete option, it will still be available to him. This only interrupts pressing the Delete
also this renders Delete obsolete for entire Sheet, which can be annoying (for the users)
Neither option is ideal, though personally I prefer the first one, as preventing usage of Delete altogether is bound to annoy your users.

Leaving Macro Dialog Box In Place

If I want to run a set of macros from the worksheet, I touch Alt+F8 and pick the first macro:
The macro runs, but the dialog box vanishes.
If I want to run several macros sequentially, I need to continue touching Alt+F8.
Is there any way I can configure the VBE to leave the Dialog Box open when a macro runs ??
As a dirty solution just add SendKeys "%{f8}" at the end of your macros to call the macro window again ;)
EDIT:
got it now:
Application.Dialogs(xlDialogRun).Show
EDIT 2
You also could create a userform with a list box which shows all important subs. This way you also could exclude subs you do not need or simply don't want to show up for whatever reason.
Also you could run subs this way which normally can't be run (missing dependency / parameter).
Why don't you create a macro to run the others in sequence?
Sub RunAll()
ColorMeYellow
FormatDownload
HyperKiller
NewCheck
ShapeKiller
SheetCounter
End Sub
or add flexibility with a ParamArray:
Sub RunAll(ParamArray runSequence() As Variant)
For Each arg In runSequence
Application.Run CStr(arg)
Next
End Sub
Used like RunAll "ColorMeYellow", "FormatDownload", "SheetCounter"

Excel – Detect if 2 specific keys being pressed at once

I don’t know if this is possible. I am working with Excel and using VB script. Is there a way to detect if two keys are being pressed at the same time and which ones they are? Then if it is those keys that are pressed I can use an If/Then statement to do whatever processes I need to do?
I know if I have something like a combo box I can use the keydown feature to detect a single key pressed, but this will only work on one key and not two keys. Also, I am not using combo boxes, text boxes, or anything else. I am strictly using cells, so there doesn’t appear to be anything like keydown to detect even the press of a single key.
Again, I would need for it to detect two keys at the same time being pressed. I would also somehow like to get it to detect this at the workbook level as well, instead of each individual worksheet, since there are several worksheets and would like these pressed keys to work from one sheet to the next.
Please let me know if this is possible or not, but I have a feeling it is not possible.
Doug,
Thanks for your suggestion, I figured everything out due to that. Here it is, in case someone else finds this useful:
Private Sub Workbook_Activate()
'When the workbook is active this will run the script in the place of the standard Ctrl + C.
Application.onkey "^{c}", "ThisWorkbook.cCopy"
End Sub
Private Sub Workbook_Deactivate()
'When another workbook is active this will disable this script so the standard Ctrl + C will work again.
Application.onkey "^{c}"
End Sub
Sub cCopy()
'This is the script to run when active. This was used for testing purposes only.
Worksheets("Sites").Range("I1").Value2 = "Yes"
End Sub

Trap delete key

My problem is fairly trivial: I need to apply logic to the delete button in Excel. In a related question I asked for means to clear cells in a pivot table, and realising now that this might not be the right approach, this is yet another alternative I'm considering. Unfortunately, I admittedly have little experience with Visual Basic, and this task is proving such a challenge (quite surprisingly).
Is it possible to trap (as in listen to) the delete-key in Excel and trigger a subroutine in VBA when pressed?
I'd appreciate all kind of input! Thanks!
Yes.
You case use Application.Onkey to assign code to keys
If you
(1) add this code to a sheet module that contains your pivottable (right-click your sheet tab, View Code and past the code below)
This code activates and then de-activates the intercept when the user enters then leaves this particular sheet:
Private Sub Worksheet_Activate()
Application.OnKey "{DELETE}", "Intercept"
End Sub
Private Sub Worksheet_Deactivate()
Application.OnKey "{DELETE}"
End Sub
(2) add this code to a regular module
Sub Intercept()
MsgBox "user just pressed delete"
End Sub
then delete on that sheet will be trapped and the user given a message

Resources