jeffreybrown
Well-known Member
- Joined
- Jul 28, 2004
- Messages
- 5,152
I cannot resove what is wrong with this code. I get a subscript out of range run time error 9.
I would like to set the worksheet (in red), but this is when I get the r/t error 9.
I removed the part in red and replaced later on in the code with the sheet name in red combined with the delete.
I have three sheets in the directory and it works on the first workbook, well at least it deletes the sheet, but I get a privacy warning, "The document contains macros, ActiveX controls, etc."
In the directory I want to exclude workbook 1040.xlsm. Any thoughts?
I would like to set the worksheet (in red), but this is when I get the r/t error 9.
I removed the part in red and replaced later on in the code with the sheet name in red combined with the delete.
I have three sheets in the directory and it works on the first workbook, well at least it deletes the sheet, but I get a privacy warning, "The document contains macros, ActiveX controls, etc."
In the directory I want to exclude workbook 1040.xlsm. Any thoughts?
Code:
Sub Test()
Dim strPath As String
Dim strFile As String
Dim wkb As Workbook
Dim wks As Worksheet
On Error Resume Next
[COLOR="red"]Set wks = Sheets("1040")[/COLOR]
On Error GoTo 0
strPath = ThisWorkbook.Path & Application.PathSeparator
If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
strFile = Dir(strPath & "*.xls*")
Do While Len(strFile) > 0
If strFile <> "1040.xlsx" Then
Set wkb = Workbooks.Open(strPath & strFile)
Application.DisplayAlerts = False
[COLOR="red"]Sheets("1040")[/COLOR].Delete
Application.DisplayAlerts = True
wkb.Close savechanges:=True
strFile = Dir
End If
Loop
MsgBox "Completed...", vbInformation
Set wks = Nothing
End Sub