我不确定为什么我的VBA代码无效:
所以我尝试了代码,这对CNHK很有用
但是,当我向下复制代码时,它会停止工作
因此对TW来说(我只包括TW)我不断收到此错误消息:
"删除Range类的方法失败"
代码的这部分:
r.Offset(1,0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
我不太清楚为什么这是我调整的唯一部分是每个部分的范围。
Sub CNHK()
Dim oLo As ListObject
Dim r As Range
Set oLo = Sheets("Data").ListObjects("Table2")
Set r = oLo.AutoFilter.Range
oLo.Range.AutoFilter Field:=4, Criteria1:= _
Array("AUSTRALIA", "FUKUOKA", "INDIA", "INDONESIA", "LONDON", "MALAYSIA", "NAGOYA", _
"NORTH AMERICA", "OSAKA", "PHILIPPINES", "SINGAPORE", "SOUTH AMERICA", "SOUTH KOREA" _
, "TAIWAN", "THAILAND", "TOKYO", "VIETNAM"), Operator:=xlFilterValues
r.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
oLo.Range.AutoFilter
Sheets(Array("Dash Fwd", "Dash Bck")).Select
Sheets("Dash Fwd").Activate
Rows("40:75").Select
Selection.EntireRow.Hidden = True
Rows("110:459").Select
Selection.EntireRow.Hidden = True
Rows("635:1054").Select
Selection.EntireRow.Hidden = True
Sheets("Dash Bck").Activate
Rows("40:75").Select
Selection.EntireRow.Hidden = True
Rows("110:459").Select
Selection.EntireRow.Hidden = True
Rows("635:1054").Select
Selection.EntireRow.Hidden = True
Sheets("Dash Fwd").Select
ActiveSheet.Protect Password:="013054", DrawingObjects:=False, Contents:=True, Scenarios:= _
False, AllowFormattingCells:=True, AllowFormattingColumns:=False, _
AllowFormattingRows:=False, AllowInsertingColumns:=True, AllowInsertingRows _
:=True, AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _
AllowDeletingRows:=True, AllowSorting:=True, AllowFiltering:=True, _
AllowUsingPivotTables:=True
Range("A1").Select
Sheets("Dash Bck").Select
ActiveSheet.Protect Password:="013054", DrawingObjects:=False, Contents:=True, Scenarios:= _
False, AllowFormattingCells:=True, AllowFormattingColumns:=False, _
AllowFormattingRows:=False, AllowInsertingColumns:=True, AllowInsertingRows _
:=True, AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _
AllowDeletingRows:=True, AllowSorting:=True, AllowFiltering:=True, _
AllowUsingPivotTables:=True
Range("A1").Select
End Sub
Sub TW()
Dim oLo As ListObject
Dim r As Range
Set oLo = Sheets("Data").ListObjects("Table2")
Set r = oLo.AutoFilter.Range
oLo.Range.AutoFilter Field:=4, Criteria1:= _
Array("AUSTRALIA", "FUKUOKA", "INDIA", "INDONESIA", "LONDON", "MALAYSIA", "NAGOYA", _
"NORTH AMERICA", "OSAKA", "PHILIPPINES", "SINGAPORE", "SOUTH AMERICA", "SOUTH KOREA" _
, "BEIJING", "THAILAND", "TOKYO", "VIETNAM", "CHENGDU", "GUANGZHOU", "HONG KONG", "SHANGHAI"), Operator:=xlFilterValues
r.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
oLo.Range.AutoFilter
Sheets(Array("Dash Fwd", "Dash Bck")).Select
Sheets("Dash Fwd").Activate
Rows("40:110").Select
Selection.EntireRow.Hidden = True
Rows("145:1055").Select
Selection.EntireRow.Hidden = True
Sheets("Dash Bck").Activate
Rows("40:110").Select
Selection.EntireRow.Hidden = True
Rows("145:1055").Select
Selection.EntireRow.Hidden = True
Sheets("Dash Fwd").Select
ActiveSheet.Protect Password:="013054", DrawingObjects:=False, Contents:=True, Scenarios:= _
False, AllowFormattingCells:=True, AllowFormattingColumns:=False, _
AllowFormattingRows:=False, AllowInsertingColumns:=True, AllowInsertingRows _
:=True, AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _
AllowDeletingRows:=True, AllowSorting:=True, AllowFiltering:=True, _
AllowUsingPivotTables:=True
Range("A1").Select
Sheets("Dash Bck").Select
ActiveSheet.Protect Password:="013054", DrawingObjects:=False, Contents:=True, Scenarios:= _
False, AllowFormattingCells:=True, AllowFormattingColumns:=False, _
AllowFormattingRows:=False, AllowInsertingColumns:=True, AllowInsertingRows _
:=True, AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _
AllowDeletingRows:=True, AllowSorting:=True, AllowFiltering:=True, _
AllowUsingPivotTables:=True
Range("A1").Select
End Sub
答案 0 :(得分:1)
可能问题是没有任何过滤器。尝试使用以下条件嵌入错误代码:
If not r is Nothing then
r.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
end if
此外,要查看是否是这种情况,请在错误之前的行中写入debug.print r.Address
。如果没有设置,它也应该是一个错误。否则,它将在即时窗口中打印地址。
答案 1 :(得分:0)
替换此部分
r.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
你的代码
Application.DisplayAlerts = False
r.Offset(1, 0).Resize(ActiveSheet.UsedRange.Rows.Count - 1).Rows.Delete
Application.DisplayAlerts = True
在删除之前,您不需要调用SpecialCells,因为Delete方法仅对可见行有效。