Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 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 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...