Thread: DIM problem
View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Paul Smith[_3_] Paul Smith[_3_] is offline
external usenet poster
 
Posts: 26
Default DIM problem

Your problem is the way you are referring to the worksheets to be deleted.

Try using the worksheets collection rather than individual worksheets.

Something like


Sub test()
dim wks as worksheet
dim n as integer
Application.DisplayAlerts = False

for each wks in activeworkbook.worksheets
for n = 1 to 20
wks.delete
next n

Application.DisplayAlerts = true
next wks


"Jeff Wright" wrote in message
news:waUHe.54739$4o.35775@fed1read06...
Good evening all,



In the following macro, I'm attempting to delete 20 worksheets from a
workbook that has, say 30 worksheets. When I run it, it deletes several
sheets, but then it crashes with a runtime error 9 (subscript out of
range).
I know that an array has to be DIM'd, but I can't figure out the name of
the
name of the array to be DIM'd. Is the array going to something like
"worksheets(50)"?? I've tried this and it doesn't work. Please help!



Thanks,



JW





Sub test()

Application.DisplayAlerts = False

For i = 1 To 20

ThisWorkbook.worksheets(i).Delete

Next i

Application.DisplayAlerts = True

End If