VBA Excel – 自动填充方法超出范围类错误

我有自动填充方法超出范围类错误从某种原因,我找不到原因。 我在代码中有很多的公式,我使用它很多,每次都停止我的代码。 这是我sub的相关部分:

'U means union, M means main 'Advanced filter for visual worksheet UnionWB.Worksheets("Union").ShowAllData Dim ULR As Long, ULC As Long, MLR As Long ULR = Cells(Rows.Count, "A").End(xlUp).Row ULC = Cells(1, Columns.Count).End(xlToLeft).Column With MainWB.Worksheets(sheet1) MLR = .Cells(Rows.Count, "A").End(xlUp).Row End With MainWB.Worksheets("aheet2").Columns("A:Z").Clear UnionWB.Worksheets("Union").Range(Cells(1, 1), Cells(ULR, ULC)).AdvancedFilter Action:=xlFilterInPlace, _ CriteriaRange:=MainWB.Worksheets("sheet1").Range("A1", "A" & MLR), Unique:=False UnionWB.Worksheets("Union").Activate Range("A1", "Y" & ULR).Select Selection.Copy MainWB.Worksheets("sheet2").Activate Range("A1").PasteSpecial xlPasteValues Range("AB2").Select Selection.AutoFill Destination:=Range("AB2", "AB" & MLR) If MLR > 2 Then With MainWB.Worksheets("sheet1).Range("N2") ActiveCell.FormulaR1C1 = _ "=IF(ISNUMBER(SEARCH(""sheet2"",RC[-9])),""yes"",""no"")" Selection.AutoFill Destination:=Range("N2", "N" & MLR) End With End If 

Selection.AutoFill目标:=范围(“AB2”,“AB”&MLR)

Selection.AutoFill目标:=范围(“N2”,“N”和MLR)

更改下面的2行:

 Range("AB2").Select Selection.AutoFill Destination:=Range("AB2", "AB" & MLR) 

至:

 Range("AB2").AutoFill Destination:=Range("AB2:AB" & MLR), Type:=xlFillDefault