我正在尝试将自动筛选添加到Excel文件中的工作表中:
'---- SLIDE THREE ----
Sub SlideTangerine3() 'reportWeek As String)
'Declare variables
Dim pre As Presentation
Dim slide As slide
Dim textbox As Shape
Dim book As Workbook
Dim sheet As Worksheet
Dim switch As String
'Set variables
Set pre = ActivePresentation
Set slide = pre.Slides(3)
'-----------------------------------------------------
Set book = Workbooks.Open("C:\Users\User\Desktop\Book1.xlsx")
Set sheet = book.Worksheets(3)
If Not sheet.AutoFilterMode Then
sheet.Range("A1").AutoFilter
End If
book.Close
End Sub
我点击运行,vba编辑器冻结
结束过程后,我收到消息“自动化错误,远程过程调用失败”
答案 0 :(得分:1)
根据上述评论,我注意到您在没有book.close
的情况下正在book.save
。这将引发一个对话框,要求您保存或放弃对工作簿的更改。应用AutoFilter - 即使没有任何条件 - 也是一个改变"。
由于Excel应用程序不可见,您无法看到此对话框,并且无法等待您回复(但您无法回复,因为您无法看到它!)。我认为这两种方法都可以解决您的问题。
选项1 使工作簿可见。这将允许显示对话框,然后您可以选择保存或放弃更改。
Sub SlideTangerine3() 'reportWeek As String)
'Declare variables
Dim pre As Presentation
Dim slide As slide
Dim textbox As Shape
Dim book As Workbook
Dim sheet As Worksheet
Dim switch As String
'Set variables
Set pre = ActivePresentation
Set slide = pre.Slides(3)
'-----------------------------------------------------
Set book = Workbooks.Open("C:\Users\User\Desktop\Book1.xlsx")
book.Application.Visible = True
Set sheet = book.Worksheets(3)
If Not sheet.AutoFilterMode Then
sheet.Range("A1").AutoFilter
End If
book.Close
End Sub
选项2 明确保存(或不保存)文件,这样就完全避免了对话框:
Sub SlideTangerine3() 'reportWeek As String)
'Declare variables
Dim pre As Presentation
Dim slide As slide
Dim textbox As Shape
Dim book As Workbook
Dim sheet As Worksheet
Dim switch As String
'Set variables
Set pre = ActivePresentation
Set slide = pre.Slides(3)
'-----------------------------------------------------
Set book = Workbooks.Open("C:\Users\User\Desktop\Book1.xlsx")
Set sheet = book.Worksheets(3)
If Not sheet.AutoFilterMode Then
sheet.Range("A1").AutoFilter
End If
book.Close True
'### OR to discard changes, use this:
'book.Close False
End Sub