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

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

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

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.

Excel VBA, Code name appearing with sheet name

Why is that when I have the following VBA code:
public sub Run
End Sub
Sometimes it appears as Run in the macro list on the view tab but sometimes it appears as sheet1!Run?
You've put the macro into one of the worksheet code sheets. This probably occurred by opening the VBE with a right-click to the worksheet name tab and choosing View Code.
The macro can reside there but it will remain Private to that worksheet unless specifically declared otherwise. Generally, a module sheet is the preferred location.
It's also a bad idea to name your macros with reserved words. Run is a function as in Application.Run and your macro can only cause confusion if it carries the same name.

How to run automatically Excel macros?

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.

Macro Fails, 'The macro may not be available in this...'

Hello and thank you in advance for your assistance.
I have some code that I admittedly borrowed from a site. It changes the sheet that is being displayed every X seconds. In my case 3 seconds. When I run it it will change to the next sheet one time and then error out after the 3 seconds.
The error I receive is "Cannot run the macro "C:\users\BenjaminSmith\Desktop\Book1.xlsm'!displaysheets'. The Macro may not be available in this workbook or all macros may be disabled."
Here is the code for my Macro
Sub displaysheets()
ShtNum = ActiveSheet.Index
ShtNum = ShtNum + 1
If ShtNum > Sheets.Count Then
ShtNum = 1
End If
Sheets(ShtNum).Activate
Application.OnTime Now + TimeValue("00:00:03"), "displaysheets"
End Sub
If I remove the line
Application.OnTime Now + TimeValue("00:00:03"), "displaysheets"
I can run the macro over and over and there are no issues. Other than the fact it doesn't continue on its own...
The spreadsheet is an XLSM.
MS VBA is 7.0.
Excel is 2010.
I am thinking maybe the issue is because the code is recursive?
Thanks for your suggestions.
Further from the comments...
The code didn't work because you didn't paste the code in a module. This is a very common mistake among new programmers. In such a case, Excel is unable to find the code as it searches the module.
#Siddharth Rout I had the code in 'ThisWorkbook' I inserted a module 'Module1' and moved the code there and everything works as expected. What is the difference with these two places?
I would recommend going through Chip Pearson's link HERE
Extract from the link if the link ever rots.
Standard Code Modules, also called simply Code Modules or just
Modules, are where you put most of your VBA code. Your basic macros
and your custom function (User Defined Functions) should be in these
modules. For the novice programmer, all your code will be in standard
modules. In addition to your basic procedures, the code modules
should contain any Declare statements to external functions (Windows
APIs or other DLLs), and custom Data Structures defined with the Type
statement.
Your workbook's VBA Project can contain as many standard code modules
as you want. This makes it easy to split your procedure into
different modules for organization and ease of maintenance. For
example, you could put all your database procedures in a module named
DataBase, and all your mathematical procedures in another module
called Math. As long as a procedure isn't declared with the Private
keyword, or the module isn't marked as private, you can call any
procedure in any module from any other module without doing anything
special.
Workbook And Sheet Modules are special modules tied directly to the
Workbook object and to each Sheet object. The module for the
workbook is called ThisWorkbook, and each Sheet module has the same
name as the sheet that it is part of. These modules should contain
the event procedures for the object, and that's all. If you put the
event procedures in a standard code module, Excel won't find them, so
they won't be executed. And if you put ordinary procedures in a
workbook or sheet module, you won't be able to call them without fully
qualifying the reference.
User Form Modules are part of the UserForm object, and contain the
event procedures for the controls on that form. For example, the
Click event for a command button on a UserForm is stored in that
UserForm's code module. Like workbook and sheet modules, you should
put only event procedures for the UserForm controls in this module.
Class Modules are used to create new objects. Class modules aren't
discussed here, except to say that a class module is used to handle
Application Event Procedures.
Try : (i use this code)
With Application
.EnableEvents = True 'needed
.OnTime EarliestTime:=Now + TimeSerial(0, 0, 3), Procedure:="displaysheets", Schedule:=True
End With
Try to put your timer in a global variable and add it each time you run the function, also configure OnTime to be schedulable
Global tmrTimer1
Sub displaysheets()
tmrTimer1 = Now + TimeValue("00:00:03")
'Enable the schedule
Application.OnTime tmrTimer1 , "displaysheets", , True
End Sub