Help with Loop, Error: Varialbe not Set

lleifsen

New Member
Joined
Jan 14, 2011
Messages
39
Hello,

I am having trouble with a Loop and I am hoping someone will have some insight.

I have a For x = 1 To x-1, Next x loop. The Macro loops through the active sheets and deletes sheets with a given criteria. My problem is...when a sheet is deleted my x is not recalculating when a sheet is deleted. x = ActiveWorkbook.Sheets.Count


My Code is:
"Sub WhAAAAt()
Application.DisplayAlerts = False
LastCell = ActiveCell.SpecialCells(xlLastCell).Address

x = ActiveWorkbook.Sheets.Count
For x = 1 To MyCount - 1

LastCell = ActiveCell.SpecialCells(xlLastCell).Address

If LastCell = "$T$10" Or LastCell = "$T$11" Then
ActiveWindow.SelectedSheets.Delete
End If

ActiveSheet.Next.Activate
Next x

Application.DisplayAlerts = True
End Sub"

Any Help would be greatly appreciated.

Lee
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Try this:

Code:
Sub WhAAAAt()
   Application.DisplayAlerts = False

   Dim ws As Worksheet
   For Each ws in ThisWorkbook.Worksheets

      LastCell = ws.SpecialCells(xlLastCell).Address
      If LastCell = "$T$10" Or LastCell = "$T$11" Then
         ws.Delete
      End If

   Next ws

   Application.DisplayAlerts = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,566
Messages
6,179,558
Members
452,928
Latest member
101blockchains

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