Can I skip a step in Macro Excel
I currently have a daily task copying data from one spreadsheet and pasting to another.
I created a Macro for this, but I get a new datasheet to copy data from every day with a new date, so when I run my Macro I get the Run-time 9 error.
Easy enough to fix by just changing the date within the VBA but it defeats the purpose of automating a daily task.
Is there anyway of skipping this step or for the script to ignore the error?
Selection.AutoFilter
ActiveSheet.Range("$A$1:$F$8").AutoFilter Field:=1, Criteria1:=Array("Fri", _
"Mon", "Thu", "Tue", "Wed"), Operator:=xlFilterValues
Range("A2:F6").Select
Selection.Copy
Windows("Calc.xlsx").Activate
Application.CutCopyMode = False
Selection.Copy
Windows("09_01_2022_data.xlsx").Activate ' Error here
Application.CutCopyMode = False
Selection.Copy
Windows("Calc.xlsx").Activate
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Windows("09_01_2022_data.xlsx").Activate
End Sub
One possible answer
I think somewhere here I've got a better answer than what is in the comments, plus I can add pictures here.
Excel Instances
Assuming you're not misspelling the file name, I think it's possible you have are inadvertently running multiple instances of Excel. You can check this by right-clicking your taskbar and opening up the Task Manager and looking for the appearance of multiple Excels, or you can view this in your VBA editor's Project Explorer. I've recreated this situation below:
So what you see above is that there are two workbooks open, plus my VBA editor in the "Microsoft Excel (3)" instance on the Task Manager, and then I forced a second instance and created a workbook "WB_in_another_Excel_instance.xlsx" in the other one. Note that in the VBA editor, which is part of the 1st instance, you cannot see this other workbook/project in the list. If you have a similar situation going on, this would be one explanation for your line...
Windows("09_01_2022_data.xlsx").Activate ' Error here
...to not work. VBA code will not see the other instance, and copying, activating that workbook would give you the Run Time 9 error. Check that.
Another approach to your code
Not knowing the full extent of your code, I'm taking some license and trying to show you another way of doing some of your code so that you don't need to rely on .Activate
and step up your VBA skills.
Sub test2()
Dim wb_Date_Data As Workbook
Dim wb_Calc As Workbook
Dim rng_filter As Range
Set wb_Calc = ThisWorkbook
Set wb_Date_Data = Workbooks.Add ' Create new workbook and save it with name later.
Set rng_filter = wb_Calc.Sheets(1).Range("A1:F8")
With rng_filter
.AutoFilter Field:=1, Criteria1:=Array("Fri", _
"Mon", "Thu", "Tue", "Wed"), Operator:=xlFilterValues
.SpecialCells(xlCellTypeVisible).Copy Destination:=wb_Date_Data.Sheets(1).Range("B2")
With .Parent ' .Parent object is generic, but in this case is referring to the worksheet of the range "rng_filter"
.ShowAllData
.AutoFilterMode = False
End With
End With
With wb_Date_Data
Application.DisplayAlerts = False ' If you are overwriting an existing file in the next step, this turns off the notification.
.SaveAs ("B:\OneDrive\Desktop\" & Format(Date, "MM_DD_YYYY") & "_data.xlsx") ' Save the file with dynamic date name (USA date format)
Application.DisplayAlerts = True ' But good practice to turn it back on quickly!
.Close savechanges:=True
End With
wb_Calc.Activate ' Not necessary, but wanted to show you can use the obj variable _
' plus, I sometimes want my macro to end on a specific workbook when working with several.
End Sub
The approach above allows the macro to create, name, and save the workbook with the date automatically without you needing to change it. I am also showing you how to use object variables to assign a range or a workbook (I could have assigned a worksheet, too, but didn't) where you make the assignment in one place one time, and then use the var thereafter. Then later if you need to change a range or a workbook name, or whatever, you only change it one place. Just something to consider in your script to make it more efficient.