GJ_ is correct: the problem is because the worksheets renumber themselves
when you delete.
A solution, though, is to delete from the end:
For ptr1 = ptr To 2 Step -1
wk.worksheets(ptr1).Delete
Next ptr1
Another is
For ptr1 = 2 To ptr
wk.worksheets(2).Delete
Next ptr1
--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no private e-mails, please)
"Perry" <(E-Mail Removed)> wrote in message
news:F159184B-1DE4-4936-A526-(E-Mail Removed)...
> Thank you. It did not work. I tried many different ways without success.
>
> "GJ_" wrote:
>
>> Op 3-4-2010 4:03, Perry schreef:
>> > Sorry for the typo, the routine should read. It executed but sheets
>> > were not
>> > deleted.
>> >
>> > Sub test1()
>> > Dim oXL, wk, sht1, cnt, ptr, ptr1
>> > Set oXL = CreateObject("Excel.Application")
>> > Set wk = oXL.workbooks.Open(CurrentProject.Path&
>> > "\POReport#2.xls")
>> > ptr = wk.sheets.Count
>> > Debug.Print "Before count:"& ptr
>> > For ptr1 = 2 To ptr
>> > wk.worksheets(ptr1).Delete
>> > Next ptr1
>> > Debug.Print "After count:"& wk.sheets.Count
>> > wk.Close
>> > Set oXL = Nothing
>> > End Sub
>>
>> Hi,
>>
>> Could it be the case, that your code deletes worksheet 2, (your former
>> worksheet 3 will be worksheet 2 from now on), then your code skips to
>> worksheet 3 (formerly known as ws 4) and deletes it, etc?
>> Or were none of the sheets deleted at all?
>>
>> I would try something like this (code not tested):
>>
>> Dim oXL, wk, sht1, cnt, ptr, ptr1
>> Set oXL = CreateObject("Excel.Application")
>> Set wk = oXL.workbooks.Open(CurrentProject.Path & "\POReport#2.xls")
>> ptr = wk.sheets.Count
>> Debug.Print "Before count:" & ptr
>> While ptr > 2
>> ptr = wk.sheets.Count
>> wk.worksheets(ptr).Delete
>> Wend
>> Debug.Print "After count:" & wk.sheets.Count
>> wk.Close
>> Set oXL = Nothing
>>
>> End Sub
>>
>> .
>>