TheRedCardinal
Board Regular
- Joined
- Jul 11, 2019
- Messages
- 243
- Office Version
- 365
- 2021
- Platform
- Windows
Hi all,
My workbook has a setup sequence that reviews all of the sheets in the workbook, and the name doesn't match a list of sheets to keep, it deletes it.
I originally did this using an IF/AND statement that checked every single sheet name (early in my coding experience) but I need the user to be able to define the sheets themselves.
So I set up a range on a main data sheet, and called it "KeepSheets"
I then tried to adapt code I found on a similar question, and it looks as follows:
I get "Object Variable or With Block variable not set" error on the Match section of the program.
WS2 is defined as a worksheet.
Can you see what I've done wrong?
Thanks!
My workbook has a setup sequence that reviews all of the sheets in the workbook, and the name doesn't match a list of sheets to keep, it deletes it.
I originally did this using an IF/AND statement that checked every single sheet name (early in my coding experience) but I need the user to be able to define the sheets themselves.
So I set up a range on a main data sheet, and called it "KeepSheets"
I then tried to adapt code I found on a similar question, and it looks as follows:
VBA Code:
For Counter = Sheets.Count To 1 Step -1
WS2 = Application.Match(Sheets(Counter).Name, Range("KeepSheets"), 0)
If IsError(WS2) Then
WBk1.Sheets(Counter).Delete
End If
End If
Next Counter
I get "Object Variable or With Block variable not set" error on the Match section of the program.
WS2 is defined as a worksheet.
Can you see what I've done wrong?
Thanks!