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

时间:2017-11-12 07:17:52

标签: excel vba excel-vba

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

'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 Destination:= Range(“AB2”,“AB”& MLR)

     

Selection.AutoFill Destination:= Range(“N2”,“N”& MLR)

1 个答案:

答案 0 :(得分:2)

更改以下两行:

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

要:

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