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
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.
I'm trying to get a macro on a schedule through Application.OnTime as below (located in ThisWorkbook):
Private Sub Workbook_Open()
Application.OnTime TimeValue("15:50:02"), "CopyScreen"
End Sub
Whereas the "CopyScreen" sub is as below (located in regular module):
Sub CopyScreen()
Application.SendKeys "({1068})", True
DoEvents
ActiveSheet.Paste
End Sub
The CopyScreen() module works fine when calling it from a manual prompt (F5), but whenever calling it through the scheduled Workbook_Open() sub, a black screenshot gets copied. Any ideas here?
I got it to work with this code:
Sub CopyScreen()
Dim sht As Worksheet
Set sht = ActiveSheet
Application.SendKeys "({1068})", True
DoEvents
With sht
.Activate
.Paste
End With 'sht
Apparently, Excel forgets ActiveSheet when it goes to sleep waiting for the set time. Go figure!
HTH
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 want to auto-run this private sub when opening the excel sheet.
I tried using Private Sub Workbook_Open() method but as the first private sub does not have a name, it does not work.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ws As Worksheet: Set ws = Sheets("Budget- Reporting")
If Range("W6").Value = 0 Then
HideFG
Else
HideF
End If
End Sub
Sub HideF()
'
' HideF Macro
'
'
For i = 1 To ActiveSheet.Shapes.Count
ActiveSheet.Shapes(i).Visible = msoTrue
Next i
ActiveSheet.Shapes.Range(Array("F")).Visible = msoFalse
Application.CommandBars("Selection").Visible = False
End Sub
Sub HideFG()
'
' HideFG Macro
'
'
For i = 1 To ActiveSheet.Shapes.Count
ActiveSheet.Shapes(i).Visible = msoTrue
Next i
ActiveSheet.Shapes.Range(Array("FG")).Visible = msoFalse
Application.CommandBars("Selection").Visible = False
End Sub
I hope that it automatically checks cell W16 when opening the excel file and carries on with HideF macro or HideFG macro. Currently, the two macros run once you actual type on the cell after opening the file.
the easiest way is to use the default Module "ThisWorkbook" which gets executed when opening the excel file. You can find it within your VBA Project Explorer on the left side of the window.
Just take the sub you want to execute and copy it into the space.
Its explained in great detail here:
https://support.office.com/en-us/article/automatically-run-a-macro-when-opening-a-workbook-1e55959b-e077-4c88-a696-c3017600db44
If it is necessary for your usecase this can help you to call a private sub:
Private Sub PrivateCallDemo()
'Module2
Application.Run "Module1.Worksheet_Change"
End Sub
This way your actual Sub could stay in another Module.
You have a few problems. First you don't want Worksheet_Change(ByVal Target As Range)
as that is for events triggers on changes to the workbook, you want Workbook_Open(). This gets stored under ThisWorkbook not a separate module/sheet.
Here is working code, I commented out your ws declaration for testing.
Private Sub Workbook_Open()
'Dim ws As Worksheet: Set ws = Sheets("Budget- Reporting")
If Range("W6").Value = 0 Then
HideFG
Else
HideF
End If
End Sub
Sub HideF()
MsgBox "HideF"
End Sub
Sub HideFG()
MsgBox "HideFG"
End Sub
Here is a screenshot of my editor.
G.M. posted a great resource as well found here --> https://support.office.com/en-us/article/automatically-run-a-macro-when-opening-a-workbook-1e55959b-e077-4c88-a696-c3017600db44
I just put the modules in the same spot for the screenshot, but you can put them separately and still use the Call HideFG method if you want to store your modules separately from the workbook_open event as I would want to.
I am trying to achieve recursive functionality in macro using a code similar to this -:
Dim showTime As Boolean
Sub RunClock()
Range("A1", "A1").Value = Now
If showTime = True Then
Application.OnTime Now + TimeValue("00:00:01"), "RunClock"
End If
End Sub
Sub StartClock()
showTime = True
RunClock
End Sub
Sub StopClock()
showTime = False
End Sub
When I execute StartClock it shows time once then gives error - "Cannot run the macro 'Book1.RunClock'. The macro may not be available in this workbook or all macros may be disabled.
Try to prefix sheet name along with macro name. Excel might me be looking in whole work book with name like Sheet1.RunClock, so try as below (assuming the macro is in Sheet1)
Application.OnTime Now + TimeValue("00:00:01"), "!Sheet1.RunClock"