I am trying to delete all worksheet in excel exept last one and save it then move its location. I can not get it took work as it deletes all other worksheets but errors out with and out of range error.
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.DisplayAlerts = False
Set objWorkbook = objExcel.Workbooks.Open("C:\M-tek 10-31-12_Tony.xlsx")
i = objWorkbook.Worksheets.Count
Do while i = i
i = i - 1
objWorkbook.Worksheets(i).Delete
Loop
Your loop will be infinite since your condition can never be false (
iwill always equali). So you need to change your loop to start at the last-but-one worksheet and work its way backwards to the first. You’re nearly there.That should do it (in fact it does, I just tested it).