Error 91

chiragggg

Board Regular
Joined
Mar 18, 2010
Messages
197
Get an error 91. Im trying to delete worksheets in a workbook name. Worksheet names are in column Z of a particular sheet.

Object variable or With block variable not set (Error 91)

Code below


Sub finaldelsheets()

Dim Ws As Worksheet
Dim c As Range



'deletes analysis sheets that are selected at start of process

Application.DisplayAlerts = False

For Each c In Ws("Africa").Range("z1:z4").Cells
Sheets(c.Value).Delete
Next c

Application.DisplayAlerts = True

End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Try

Code:
Sub finaldelsheets()

Dim c As Range


'deletes analysis sheets that are selected at start of process

Application.DisplayAlerts = False
For Each c In Sheets("Africa").Range("z1:z4")
Sheets(c.Value).Delete
Next c
Application.DisplayAlerts = True

End Sub
 
Upvote 0
Is there a reason you are declaring Ws as a worksheet and using it the way you are?

Maybe just try:

Code:
Sub finaldelsheets()

Dim c As Range


'deletes analysis sheets that are selected at start of process

Application.DisplayAlerts = False
For Each c In Sheets("Africa").Range("z1:z4").Cells
Sheets(c.Value).Delete
Next c
Application.DisplayAlerts = True

End Sub
Hope that helps.
 
Upvote 0
Apologies,

Think I made a mistake in my post

I was using

Dim Ws As Worksheet

Would it make a difference? Ws or sheet?
 
Upvote 0
I get Subscript out of range (Error 9)

'deletes analysis sheets that are selected at start of process
Application.DisplayAlerts = False
For Each c In ActiveSheet.Range("z1:z4").Cells
Sheets(c.Value).Delete
Next c

I changed sheets to activesheet instead
 
Upvote 0
Does it delete any of the sheets listed in Z1:Z4?

Also I assume that the values in Z1 through Z4 are actually sheet names and there are not spaces or anything like that incorrect?

And is the activesheet "Africa" when you are running this since you were directing at that shete before?

Typically you get that error when it tries to access a sheet or object that does not exist.

What is listed in Z1:Z4 on the active sheet?

Hope that helps.
 
Upvote 0
Does it delete any of the sheets listed in Z1:Z4?
No it doesnt I get an error message saying out of range
Also I assume that the values in Z1 through Z4 are actually sheet names and there are not spaces or anything like that incorrect?
Yes the values are the sheet names
And is the activesheet "Africa" when you are running this since you were directing at that shete before?
Yes it is
Typically you get that error when it tries to access a sheet or object that does not exist.
I will give it another try
What is listed in Z1:Z4 on the active sheet?
The sheet names of which I want to delete are on there, the range will vary as I may want 2 sheets deleted or 3 or just 1. so its not always the full range of Z1:Z4 to be deleted.
 
Upvote 0

Forum statistics

Threads
1,214,121
Messages
6,117,846
Members
448,782
Latest member
lepaulek

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top