How to run automatically Excel macros? - vba

I have created a cricket sheet in excel. I have also created a macro which is used to display a message when overs bowled are equal to 20 (A T-20 Match). My problem is that when the over get to 20 no message is shown unless I press the shortcut key. Is there anything which I can do so that whenever overs reach to 20 a message automatically displays.
My code is:
Sub Innings()
If Range("H43") = 20# Then
MsgBox "Innings Completed"
End If
End Sub

Include the following macros in the worksheet code area:
Private Sub Worksheet_Calculate()
Call Innings
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("H43")) Is Nothing Then
Call Innings
End If
End Sub
Because they are worksheet code macros, they are very easy to install and automatic to use:
right-click the tab name near the bottom of the Excel window
select View Code - this brings up a VBE window
paste the stuff in and close the VBE window
If you have any concerns, first try them on a trial worksheet.
If you save the workbook, the macros will be saved with it.
If you are using a version of Excel later then 2003, you must save
the file as .xlsm rather than .xlsx
To remove the macros:
bring up the VBE windows as above
clear the code out
close the VBE window
To learn more about macros in general, see:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
and
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
To learn more about Event Macros (worksheet code), see:
http://www.mvps.org/dmcritchie/excel/event.htm
Macros must be enabled for this to work!

Gary's Student's code is working well. You just need to write Worksheet_Change and Worksheet_Calculate to the List1 not to the Module1. I add a picture.

Related

Is there a way to hide macros in Excel?

I just finished some VBA and I was wondering if there is a way to hide certain macros on Excel.
I need the user to run a certain macro and only that one, but it shows all the sub macros in Excel. I want to hide the unnecessary macros from the user so that way the user doesn't accidentally click on the wrong one.
You can also do this by placing the macros you want to hide in a separate module and using Option Private Module at the top of the module before the code. The macros will still be available to your project but will not appear in the Macros seen by the user when he clicks the Macros button.
You can either create a button in the ribbon to run the macro, or you can add "Private" before each "Sub" in the VBA editor that you don't want the user to easily access.
To subjectively 'hide' certain sub procedures (i.e. 'macros') from the (Alt+F8) Developer, Macros dialog use an optional non-variant parameter that means nothing.
Sub meh(Optional w As Worksheet)
Debug.Print "hello world"
End Sub
The meh macro will not show up in the list of macros to run. If you dim the parameter as variant it will show in the list. This is likely due to a optional variant parameter being able to use the IsMissing function. It will also not be able to be run from the VBE with F5 or stepped through with F8.
The test sub procedure will run the code correctly.
Sub test()
meh
End Sub
Sub meh(Optional w As Worksheet)
Debug.Print "hello world"
End Sub

Cannot run the macro. The macro may not be availabe in this workbook

Having an issue with VBA error 'run time 1004'. Using the following code. The macro is called from a button in Row 5, hence the subtraction. There are other buttons in rows 6, 7, 8 etc. all calling the same macro (to subsequently call a specific userform), hence the variable.
Sub Export()
i = RowNumber - 4
Reinstated = "ReinstateR" & i
Application.Run Reinstated
End Sub
The macros 'ReinstateR1', 'ReinstateR2' etc. are all stored in a separate module.
Sub ReinstateR1()
'Macro function etc.
End Sub
For some reason, though, when I click the button, I get the following error message:
"Cannot run the macro 'ReinstateR1'. The macro may not be available in this workbook or all macros may be disabled."
All macros are enabled, the macro is in the same workbook, etc. Trust centre settings are set to disable all macros with notification, etc.
I'm stumped. I can call the macro without the variable, but that's not the point...
If you have a module with the same name as a routine it contains, you need to prefix any call to it using Application.Run with the module name as well (or change the name of either the module or the routine), so in this case it's:
Application.Run "Reinstater1." & Reinstated

VBA Select a sheet when the workbook is opened

The following program doesn't work when I open my workbook. What are the possible reasons?
' Select the first sheet when the workbook is opened.
Private Sub Workbook_Open()
Sheet4.Select
Sheet4.Range("B1").Select
End Sub
If you hit alt+F11 to go to the VBA code editor. On the left side, under the file name you will see the different sheets, and whatever modules you might have in there. If you go under the ThisWorkbook module
and place your code there, it will automatically run when you start up the Excel File.
you are using the "Select" Method without an Activating a Sheet First!
Yes, when you have closed your workbook last time, the current sheet will remain in the memory index and when you will again open the same workbook, the pointer search for the most recent sheet used based on the index no.
'Here is the code
Private Sub Workbook_Open()
Sheet4.Activate
Sheet4.Select
Sheet4.Range("B1").Select
End Sub
using "Select Method" without Activating the Parent Object is a Crime. lol
Hope this will help you.

Add-in with predefined shortcut key?

Firstly, this is all in Excel 2007, FYI.
I'm trying to set up an Add-in that will use a shortcut key to run a simple macro. I want to define that shortcut key so that I can simply tell my users to press "ctl+alt+l" to run the macro, hopefully keeping explanations to a minimum on my team since the user interface would be unified.
My code to set up the shortcut key is right here:
Sub LiteralizeShortcutKey()
Application.OnKey "%^l", "Literalize"
End Sub
When I run the macro it works just fine.
I then create a Workbook macro to autoload when the excel file opened.
Private Sub Workbook_Open()
LiteralizeShortcutKey
End Sub
This works fine when I open the workbook. But it all falls apart when I export the workbook to a .xlam and then load it into another workbook. My little LiteralizeShortcutKey() Sub won't load, so I can't use my predefined shortcut key.
Thoughts?

How can I run a Macro when the value in a specific cell changes?

Let me preface by saying that I am very new to VB...
I am trying to run a macro whenever the value in a certain cell changes. I've read up on how to do this, but can't seem to get it to work. I have entered the following code into the private module of the worksheet object:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("$C$5")) Is Nothing Then
Application.Run _
"'Amex Payments_Experiment.xlsm'!SelectCells"
End If
End Sub
C5 is the cell I am trying to monitor for change.
"SelectCells" is the macro I want to run.
"Amex Payments_Experiment.xlsm" is the name of the file.
When I change the value in C5 nothing happens. Some help would be great. Thanks!
UPDATE:
Cyberkiwi - No, that is not exactly how I did it, but when I follow you're instructions I do find the code where you say it should be. To get to the private module of the worksheet object I right clicked the sheet tab at the bottom, selected "view code", then selected "worksheet" from the dropdown in the top center of the page.
User587834 - Yes. Macro's are enabled.
Any other suggestions?
if you use Excel2007 be sure that macros are enabled, by default Excel 2007 deactivate macro execution for new workbook.
for that try to execute any other macro to be sure that macros are enabled.
I have entered the following code into the private module of the worksheet object:
How exactly have you done that? As below?
Alt-F11 to switch to code view
On the left, double-click on the target sheet
On the right, do you see the code you entered? if not, proceed to next step
Paste the code block
This code works Ok for me
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("$C$5")) Is Nothing Then
MsgBox "hello"
End If
End Sub
Maybe the problem is with your Application.Run:
- select the line containing the intersect and click F9 to switch on Debug, then try changing a cell to see if you get to the sub.
If you never reach that line then you have not got the code in the worksheet clkass module, or you have switched off events, or macros are disabled or ...
Check the Application.EnableEvents property, and set it to True if required.