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

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
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
 

schielrn

Well-known Member
Joined
Apr 4, 2007
Messages
6,941
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.
 

chiragggg

Board Regular
Joined
Mar 18, 2010
Messages
197
Apologies,

Think I made a mistake in my post

I was using

Dim Ws As Worksheet

Would it make a difference? Ws or sheet?
 

chiragggg

Board Regular
Joined
Mar 18, 2010
Messages
197

ADVERTISEMENT

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
 

schielrn

Well-known Member
Joined
Apr 4, 2007
Messages
6,941
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.
 

chiragggg

Board Regular
Joined
Mar 18, 2010
Messages
197
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.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,742
Messages
5,833,434
Members
430,209
Latest member
addms

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
Top