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
 

Some videos you may like

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

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,934
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,934
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.
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,597
Messages
5,512,310
Members
408,886
Latest member
kashifziatevta423

This Week's Hot Topics

  • Sort code advice please
    Hi, I have the code below which im trying to edit but getting a little stuck. This was the original code which worked fine,columns A-F would sort...
  • SUMPRODUCT with nested If statement
    Hi everyone, Hope you're all well. I'm hoping someone will be able to point me in the right direction with a problem I'm having with a SUMPRODUCT...
  • VBA - simple sort is killing me!
    Hello all! This should be so easy, but not for me, apparently! I have a table of data that can be of varying lengths and widths. My current macro...
  • Compare Two Lists
    I have two Lists and I need to be able to Identify differences between them. List 100 comes from a workbook - the other is downloaded form the...
  • Formula that deducts points for each code I input.
    I am trying to create a formula that will have each student in my class start at 100 points and then for each code that I enter (PP for Poor...
  • Conditional formatting formula required for day of week and a value
    Hi, I have a really simple spreadsheet where column A is the date, column B is the activity total shown as a number and column C states the day of...
Top