Thread: DIM problem
View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Jim Thomlinson[_4_] Jim Thomlinson[_4_] is offline
external usenet poster
 
Posts: 1,119
Default DIM problem

Have you tried this code... It crashes for me... For each worksheet in the
workbook you are delete that sheet 20 times? Looks like an error to me...
Maybe something more like (untested)

dim wks as worksheet
dim n ans integer

n = 1
for each wks in worksheets
wks.delete
if n = 20 then exit sub
n = n+1
next wks
--
HTH...

Jim Thomlinson


"Paul Smith" wrote:

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