下面的代码调用另一个子中的子(宏)。
当调用第二个Sub“Universal_Dry_Good(表格)时,它会一直运行到
sheet.Range("$B$2:$X$21200").AutoFilter Field:=1, Criteria1:="**TUN**", Operator:=xlFilterValues
“Universal_Dry_Good”在Excel内部运行正常。就在我在另一个宏中调用它失败时。
目标:当Sub Cat()运行时,它会打开一个Excel文件,然后在指定的Excel文件/工作表上运行“Universal Dry Good”。这些宏存在于Outlook内部,并由传入的电子邮件触发。
为什么一旦称为“Universal Dry Good”,它就不再起作用了?
错误是
范围类的自动过滤方法失败
它可以独立工作,也可以替换为其他操作(例如Columns.Replace
,Rows.Delete
等)。
Sub Universal_Dry_Good(sheet)
'
' Universal_Dry_Good Macro
'
sheet.Rows("1:3").Delete
sheet.Rows("1:3").Delete Shift:=xlUp
'ERROR IN THE LINE BELOW
sheet.Range("$B$2:$X$21200").AutoFilter Field:=1, Criteria1:="**TUN**", _
Operator:=xlFilterValues
sheet.Range("B3:C3").Select
sheet.Range(Selection, Selection.End(xlDown)).Select
sheet.Range("B3:AA10000").Select
Selection.EntireRow.Delete
sheet.Rows("2:2").Select
sheet.ActiveSheet.ShowAllData
Selection.AutoFilter
End Sub
Sub Cat()
Set xlApp = CreateObject("Excel.Application")
xlApp.Application.Visible = True
xlApp.Workbooks.Open "---------"
Dim sheet As Object
Set sheet = xlApp.Worksheets("Report 1")
Universal_Dry_Good sheet
End Sub
答案 0 :(得分:0)
以下代码已解决:感谢您的帮助@RicardoA:
Sub Universal_Dry_Good(sheet)
'
' Universal_Dry_Good Macro
'
sheet.Application.DisplayAlerts = False
sheet.Rows("1:3").Delete
sheet.Rows("1:1").Select
sheet.Range("B2:C2").Select
sheet.Range("$B$2:$X$21200").Autofilter Field:=1, Criteria1:="**TUN**"
sheet.Range("B3:C3").Select
sheet.Range("B3:AA10000").Delete
sheet.Rows("2:2").Select
sheet.ShowAllData
sheet.Cells.Autofilter
sheet.Application.DisplayAlerts = True
End Sub
Sub Pat()
Set xlApp = CreateObject("Excel.Application")
xlApp.Application.Visible = True
xlApp.Workbooks.Open "--------"
Dim sheet As Object
Set sheet = xlApp.Worksheets("Report 1")
Universal_Dry_Good sheet
End Sub