I don't know why this should be an issue but I am running into a loop because the sheets are not being deleted with this command.
What may explain this is as follows: I have run across a message (upon opening excel) which states ' data may exist in the sheets selected for deletion. To permanently delete the data press delete' - and this requires selection of 'delete' in excel. This message then pops on each of the sheets marked for deletion. this could be the reason my sheets are not deleting inside the loop.
Is there a way around this?
owb.Sheets(mname).Delete
k
>looping from highest # down to first might be more stable...
>
>HTH
>
>thomas
>
>>much cleaner and more logical - thanks borislav.
>>problem now: It gets rid of the furthest two sheets (of a 4 sheet file) then gets stuck in a loop at which point I still have 2 sheets remaining.
>>I can't seem to solve it even adding if clauses - strange behavior - seems to defy your logic.
>>k
>>
>>>>I have set this up to remove all multiple sheets except the first sheet.
>>>>It's not working - any help would be appreciated. thanks
>>>>k
>>>>
>>>>
>>>>lcFile='myfile.xlsx'
>>>>Erase (Forceext(lcFile,'xxx'))
>>>>ox=Createobject('excel.application')
>>>>owb=ox.Workbooks.Open(lcFile)
>>>>
>>>>If owb.Sheets.Count > 1
>>>> Nu= owb.Sheets.Count
>>>> ms=1
>>>> Do While ms < Nu+1
>>>> mname=owb.Sheets(m1).Name
>>>> If ms>1
>>>> Wait Window 'removing '+ mname timeout 2
>>>> owb.Sheets(mname).Delete
>>>> Endif
>>>> ms=ms+1
>>>> Enddo
>>>>Endif
>>>>ox.Visible=.T.
>>>>
>>>
>>>
>>>lcFile='myfile.xlsx'
>>>ox=Createobject('excel.application')
>>>owb=ox.Workbooks.Open(lcFile)
>>>
>>>If owb.Sheets.Count > 1
>>> Nu= owb.Sheets.Count
>>> Do While Nu > 1
>>> mname=owb.Sheets(Nu).Name
>>> Wait Window 'removing '+ mname timeout 2
>>> owb.Sheets(mname).Delete
>>> Nu= owb.Sheets.Count
>>> Enddo
>>>Endif
>>>ox.Visible=.T.
>>>