Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

excel - Delete worksheets on every pc

I've written a simple VBA sub that deletes a couple of worksheets while keeping some others when the user clicks a button. If I use it on my pc it works perfectly but it doesn't run on the pc of the person I've written the macro for. The error message the person gets is:

"Method 'Delete' of object '_Worksheet' failed.

This is the macro:


Sub deleteWorksheets()

Dim wb                      As Workbook
Dim wks                     As Worksheet

Set wb = ThisWorkbook
Application.DisplayAlerts = False
For Each wks In wb.Worksheets
    If wks.Name <> "A" And wks.Name <> "B" And wks.Name <> "C" Then
        wks.Delete
    End If
Next
Application.DisplayAlerts = True

End Sub 

The sheets A, B, C are included in the workbook. So that's ok. He sent me his references and I see he uses:

  • Microsoft Excel 14.0 Object Library where I use Microsoft Excel 16.0 Object Library
  • Microsoft Office 14.0 Object Library where I use Microsoft Office 16.0 Object Library

Anybody who faced a similar issue?

Update:

A hidden sheet created by a third party software add-in created the issue. As I don't have that software installed I don't have the issue.

Modifying code to:


Sub deleteWorksheets()

Dim wb                      As Workbook
Dim wks                     As Worksheet

Set wb = ThisWorkbook
Application.DisplayAlerts = False
For Each wks In wb.Worksheets
    If wks.Name <> "A" And wks.Name <> "B" And wks.Name <> "C" And wks.Name <> "__FDSCACHE__" Then
        wks.Delete
    End If
Next
Application.DisplayAlerts = True

End Sub 


solved the issue

question from:https://stackoverflow.com/questions/66065793/delete-worksheets-on-every-pc

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...