Deleting worksheets with looping

后端 未结 2 1350
萌比男神i
萌比男神i 2021-01-22 20:22

Could you tell me someone why just every second sheet is deleted, however if I turn off the worksheets.delete line than in the message box appears all the sheet names.



        
2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-22 21:22

    Delete the sheet in reverse. Every time you delete a sheet, the Sheet index gets shifted.

    Sub tor()
        Dim wsz As Integer, i as Long
    
        wsz = ThisWorkbook.Worksheets.Count
    
        Application.DisplayAlerts = False
    
        For i = wsz To 2 Step -1
            ThisWorkbook.Worksheets(i).Delete
        Next i
    
        Application.DisplayAlerts = True
    End Sub
    

提交回复
热议问题