How do I hotkey current sheet's VBA code? - vba

I've had the issue of Worksheet_SelectionChange(ByVal Target As Range) triggering every time I click on something, which I don't want, as it means I have to wait every time I click in the actual sheet. I changed the code to Worksheet_Open, but that means I have to click the Run button whenever I want the code to run.
So I'm looking for a way of only running the current sheet's VBA code when I want to while I'm looking at the sheet (not while I'm in the VBA Editor). Ideally I'd be able to assign it to a hotkey using Application.OnKey, but I can't figure out how to refer to the sheet's code itself. The only solution I've found refers to an external macro, which isn't helpful, since I'm only wanting to use the code that's already in the sheet, as I'm using different macros for each sheet in the workbook.

Create a macro in a module for which you set the hot key, this macro will call the one you need:
CallByName ActiveSheet, ActiveSheet.Name & "Macro", VbMethod.
So you need to name the macros in the format of SheetNameMacro in each sheet.
More explanation: Trying to call a Sub with a String - VBA
Update
If your sheet names contains spaces then use this:
CallByName ActiveSheet, Replace(ActiveSheet.Name," ","_") & "Macro", VbMethod
and of course also replace spaces in your sheet (e.g. if your sheet called "list to delete" than macro name should be list_to_deleteMacro.

Related

Auto-update (calculate) the Excel sheet when a cell is changed

I am using Excel 2010 and I want that the Excel sheet will be updated (calculated) automatically when the value of a cell (Range("B7")) is changed (The Range("B7") is a drop-down list). I am trying to use the intersect-target method like:
Private Sub Worksheet_Change(ByVal target As Range)
If Not Intersect(target, Range("B7")) Is Nothing Then
ActiveSheet.Calculate
End If
End Sub
I have two questions about this:
Does it matter if I put this Private Sub under any module? Or am I supposed to write it in the exact sheet under the Microsoft Excel -object menu in VBA?
The second question is how I can make this work? The sheet doesn't update (calculate) automatically when I change the Range("B7"). It only updates when I save the Excel file.
The function should be in the sheet which B7 belongs to, e.g. if the B7 you are updating is in "Sheet1" then the code for the Worksheet_Change event should be under Sheet1 too. You can right click the tab for the sheet, and select show code to get to the right place in VBE.
Excel should be aware that any cell using B7 needs to be recalculated when changed, so that should be handled automatically.
Your code will work if you place it in the sheets event not in Modlue please find the below image for your reference.
Select the respective sheet in VB window and double click on it, it will open the worksheet event window (code window) where you need to copy paste the codes. Now try changing the validation cell it should work :)..

Is it possible to have a cell that has a formula and accepts entry at same time in excel?

Example:
A B
1 =vlookup(XX)
2
3
in cell A1 there is a Vlookup formula, Is it possible to enable user entry in this cell and override the formula then later restore the formula automatically when sheet is open again?
Even through VBA
Short, boring answer: nope.
A cell only ever has a keyed-in value, or a calculated formula. Can't have both.
Longer answer: maybe.
Shift everything 1 row down, and use row 1 to store your "original" formula - then hide that row (and pray the user isn't going to mess with it).
When the sheet is opened again sounds like you're confusing "workbook" and "worksheet" - you need to handle Workbook_Open if you want to run code when a workbook opens. Workbooks contain worksheets - it's the workbook that opens, not the sheets (sheets activate, but I doubt you would want to put that logic in there).
So, in the handler for Workbook_Open, write code that takes the formula in the hidden row and overwrites whatever is under it.
Another solution can be to hard-code the formula in the VBA code.
One possibility would be to store your Workbook as a template. Normally when a user opens the workbook by double-clicking, it will open whole new workbook based on the template, and they can modify it to their heart's content, save it, mail it to Grandma, etc.
The next person who comes along will double-click the template file and get the formula again, just as you designed it.
Short answer: Kind of, sort of
Long answer:
Save your workbook as a template. Every time someone will use it you'll see the orignal with formula, then if someone write over the formula, when using save your original will be kept intact.
What You need to do is:
press Alt + F11
select ThisWorkbook and paste this code:
Private Sub Workbook_Open()
Worksheets("Sheet1").Range("A11").Value = "asdf"
End Sub
Every time the workbook is opened, this script will run.
Instead of "Sheet1" you can write the name of the sheet you want to apply the script.
Inside the Range, You can define the cells you want to modify, You can use even multiple cells. Check this for more information about this.
After Value You can write what You want to be written inside the cell. You can write "=vlookup(XX)" and it will work.

How do I run VBA code on ListObject Change?

I know that I can write the following code in a sheets' VBA-object to run code on a worksheet change.
Private Sub Worksheet_Change(ByVal Target As Range)
End Sub
Is there anything similar that I can write to run code whenever I filter a certain ListObject?
Only in some cases. Say we include a new column in the table that we fill with the value 1. Elsewhere we insert an
=SUBTOTAL()
formula to sum that column. As the filter is operated the number of visible rows will vary. The SUBTOTAL() function will re-calculate.
At this point a Calculate Event macro would catch the re-calculation!
Try this...
I added a ListBox Form Control to my Excel sheet..
Next, I assign a macro to this object by Right Clicking and choosing "Assign Macro". (you may need to be in "Design Mode" to make this happen -- check the Developer Ribbon)
By default - the Macro Name is populated with a change event macro name. Click "New" to create the macro.
Your macro is added to a Module. Hope this helps!

Running Sheet-Specific Macro from Other Sheet

I have a Excel book that has multiple sheets, subs, and macros. In order to make it a bit more user friendly, I'd like to create a dashboard sheet where the user has to just click buttons to run everything. This is possible easily with the subs, but how can I call a macro from one sheet to another? I can't seem to find any Excel documentation on this.
Put the subs in a Module as opposed to in each sheet object.
To Create a Module right click on the book name in the VBA editor and then choose Insert -> Module. Any sub will then be accessible to any sheet (or object) within that book.
If you have a Sub in a worksheet code module, you can call it from a regular code module (or any other module) like:
Sheet1.MySub
...where Sheet1 is the codename of the sheet containing the code you want to run.

How to shell to an exe and pass cell values from Excel worksheet

Is this possible in Excel?
I have a workbook with multiple worksheets. I wrote some vba code in a code module to shell to an exe and pass it cell values as arguments.
What I want to be able to do is select a cell or row in any of my worksheets and then call my Shell Sub while passing the values from a couple cells to the Sub. A hot-key combination would be best.
The part I am having trouble with is calling a sub in a code module from a hot hey.
Can you help with this? Any sample code?
Much appreciated!
Found my answer! It was in a post from Leith Ross found here...
http://www.excelforum.com/excel-programming/590157-keyboard-shortcut-to-run-sub-module-in-vb.html
If your macro is a Sub then its easy to assign a shortcut key to your macro. While in Excel, type ALT+F8 to bring up the Macros List. Select the name of your macro by clicking it. At the bottom right corner of the dialog, click "Options..". You can then assign a shortcut key and add a description of the macro for other to see when it is selected in the Macro List.
That's exactly what I needed.