I have several macros so I use call macro to run them simultaneously but it only works on worksheet1 so now I want the macros to run on all worksheets in a workbook. Is there a way whereby i use callmacros to run the first macro for worksheet1 and then the other macros to run on all worksheets.
Sub callmacros()
Call macro1 '(run on only worksheet1)
Call macro2 '(run on all worksheets)
Call macro3 '(run on all worksheets)
End Sub
Pls help. Thanks
In Sheet1
Sub Test1()
MsgBox "1"
End Sub
In Sheet2
Sub Test2()
MsgBox "3"
End Sub
In Sheet3
Sub Test3()
MsgBox "3"
End Sub
In ThisWorkbook
Private Sub Workbook_Open()
Call Sheets(1).Test1
Call Sheets(2).Test2
Call Sheets(3).Test3
End Sub
Hope this helps.
Related
I created macros in vba but they don't work.
In the ThisWorkbook code I wrote:
Private Sub Workbook_Open()
Worksheets("Sheet1").Activate
End Sub
In the code of Sheet1 (Sheet1) I wrote:
Private Sub Worksheet_Activate()
MsgBox ("hello")
End Sub
but when I open the file nothing happens ...
If Sheet1 is already active when you open the workbook, then
Worksheets("Sheet1").Activate
does exactly nothing. Because you cannot activate what is already active.
Test it by adding 2 Sheets Sheet1 and Sheet2. Then select Sheet2 save the file and close it. Now open it again, it will run Worksheets("Sheet1").Activate and this will trigger the Worksheet_Activate properly.
Also note that MsgBox ("hello") should be without parenthesis as it does not return a value to a variable: MsgBox "hello"
An alternative solution is:
Write in a module:
Public Sub Sheet1_Activate()
MsgBox "Sheet1 Activate"
End Sub
In Sheet1 write:
Private Sub Worksheet_Activate()
Sheet1_Activate
End Sub
And in ThisWorkbook write:
Private Sub Workbook_Open()
If ThisWorkbook.ActiveSheet.Name = "Sheet1" Then
Sheet1_Activate
Else
ThisWorkbook.Worksheets("Sheet1").Activate
End If
End Sub
The idea is to have a new procedure Sheet1_Activate that takes the actual code and is triggered by the Worksheet_Activate as well as by the Workbook_Open in case the sheet is already the active sheet.
Sub auto_open()
MsgBox "HueHueHue"
End Sub
You can't activate the same thing twice.
Im getting a Run-time error '1004':
Select method of range class failed
This only happens when I move away from the sheet (Bet Angel) that has the macro running to another sheet. The code is below:
Sub DeleteStatus()
Worksheets("Bet Angel").Range("O6:O50").Select
Selection.ClearContents
Call Start
End Sub
Sub Start()
Application.OnTime Now + TimeValue("00:00:15"), "DeleteStatus"
End Sub
I want the macro to run even if I am moving between different sheets in the workbook.
Since you are moving to another worksheets, you need to activate or select the target worksheet first.
Sub DeleteStatus()
Sheets("Bet Angel").Activate
Worksheets("Bet Angel").Range("O6:O50").Select
Selection.ClearContents
Call Start
End Sub
Sub Start()
Application.OnTime Now + TimeValue("00:00:15"), "DeleteStatus"
End Sub
If you want to go back to your original worksheet, you can activate it at the end of DeleteStatus()
Like this
Sub DeleteStatus()
Sheets("Bet Angel").Activate
Worksheets("Bet Angel").Range("O6:O50").Select
Selection.ClearContents
Call Start
Sheets("your original worksheet").Activate
End Sub
If you don't want to use activate, then you need to dim the worksheet first.
Like this
Sub DeleteStatus()
Dim ws As Worksheet
Set ws = Sheets("Bet Angel")
ws.Range("O6:O50").ClearContents
Call Start
End Sub
Sub Start()
Application.OnTime Now + TimeValue("00:00:15"), "DeleteStatus"
End Sub
My focus is to clean a specific column in a specific sheet every one hours.
I wrote this code but doesn't work.
Sub CleanColumn()
Worksheets("Calculation").Columns(2).ClearContents
Call test
End Sub
Sub test()
Application.OnTime Now + TimeValue("01:00:00"), "CleanColumn"
End Sub
Where I wronge?
Thanks in advance.
below code is working.
Public Const ClCol = "CleanColumn"
Sub CleanColumn()
'Change the range as per your req..
Sheet1.Range("A1").ClearContents
Call test
End Sub
Sub test()
'Once you change the below time it will be adjusted accordingly.
Application.OnTime Now + TimeValue("00:01:00"), ClCol
End Sub
Hope this helps..
cI got it to work by copying your code to Module1 in a workbook and changing to this:
Sub test()
Application.OnTime Now + TimeValue("01:00:o0"), "Module1.CleanColumn"
End Sub
I believe that you need to tell it where the subroutine CleanColumn resides
Since it is on the hour, you should explicitly define which workbook and which worksheet like:
Workbook("\\f2\folder\wb_time.xlsm").Worksheets("Sheet1").Columns(2).ClearContents
I have added an activex combobox "ComboBox1" to my excel spreadsheet "Sheet1" and I want to be able to call different macros based on the selection in the combobox. Some of the macro names I have are "MC323", "MC616", "MC813".
So basically you select MC616 from the combobox list I want it to run the MC616 macro.
I have tried searching for answers but nothing has made since to me. Here is what I have so far, which isn't much and I'm sure isn't right anyway.
Option Explicit
Private Sub Workbook_Open()
With Sheet1.ComboBox1
ComboBox1.Clear
.AddItem "MC323"
.AddItem "MC616"
.AddItem "MC813"
End With
End Sub
Sub ComoBox1_Change()
With Sheet1.ComboBox1
Select Case ComboBox1.Value
Case "MC323": MC323
Case "MC616": MC616
Case "MC813": MC813
End Select
End With
End Sub
Sub MC323()
Call MC323
End Sub
Sub MC616()
Call MC616
End Sub
Sub MC813()
Call MC813
End Sub
You have a spelling error, that could be an issue, anyway...
This should go into the Sheet1 module
Sub ComboBox1_Click()
Select Case ComboBox1.Value
Case Is = "MC323": MC323
Case Is = "MC616": MC616
Case Is = "MC813": MC813
End Select
End Sub
This should go into a regular module,
Sub MC323()
MsgBox "MC323"
End Sub
Sub MC616()
MsgBox "MC616"
End Sub
Sub MC813()
MsgBox "MC813"
End Sub
This is in the Workbook Module,
Private Sub Workbook_Open()
With Sheet1.ComboBox1
.Clear
.AddItem "MC323"
.AddItem "MC616"
.AddItem "MC813"
End With
End Sub
It all looks good until you get here:
Sub MC323()
Call MC323
End Sub
Sub MC616()
Call MC616
End Sub
Sub MC813()
Call MC813
End Sub
Subroutine MC323 has only one line and that line says to call subroutine MC323 which has only one line and that line and that line says to call subroutine MC323 which has only one line and that line says to call subroutine MC323 which has only one line and that line says to call subroutine MC323 which has only one line and that line says to call subroutine MC323 which has only one line and that line says to call subroutine MC323 which has only one line and that line asays to call subroutine MC323.
So anyway, you get into a endless recursive loop, which is no good. Don't call subroutine MC323 inside of MC323. Put the code you want ran into the subroutine MC323. Like...
Sub MC323()
MsgBox("Endless recursive loops are bad")
End Sub
I've been wondering why I couldn't get my examples to work - MC323, MC616 and MC813 are also worksheet cell references.
So..... ensure you have a sheet with a codename Sheet1 and an ActiveX combo-box called ComboBox1.
In the ThisWorkbook module:
Private Sub Workbook_Open()
With Sheet1
.ComboBox1.Clear
.ComboBox1.AddItem "MC323"
.ComboBox1.AddItem "MyMacro2"
.ComboBox1.AddItem "MyMacro3"
End With
End Sub
Note the first item will cause an error - 'Cannot run the macro 'MC323'. The macro may not be available in this workbook or all macros may be disabled.
In a normal module:
Public Sub MC323()
MsgBox "1"
End Sub
Public Sub MyMacro2()
MsgBox "2"
End Sub
Public Sub MyMacro3()
MsgBox "3"
End Sub
Finally, in the Worksheet module for Sheet1:
Private Sub ComboBox1_Change()
Application.Run Sheet1.ComboBox1.Value
End Sub
Useful links to help files:
Application.Run Method
Ron de Bruin - How do I use Application.Run in Excel
I want to autorun a macro whenever I create a new worksheet in a workbook. Below is what I tried:
Private Sub Workbook_NewSheet()
Call Macro1
End Sub
Sub Macro1()
...
...
End Sub
Turns out nothing happened. Why?
I guess you have put them under the wrong place.
Private Sub Workbook_NewSheet(ByVal Sh as Object)
Call Macro1
End Sub
should be put under ThisWorkBook while
Sub Macro1()
...
...
End Sub
should be put under Modules