0

我的代码应该在“L”列中自动填充 K2=J2 等的“IF”公式,但遇到运行时错误 1004。

我希望宏完成整个列,直到它到达 K 列中包含数据的最后一行。

代码:

Sub SiteAccess()

Dim mySheet As Worksheet, myOtherSheet As Worksheet, myBook As Workbook
Set myBook = Excel.ActiveWorkbook
Set mySheet = myBook.Sheets("SiteAccessReports")
Set myOtherSheet = myBook.Sheets("Sheet1")

Dim ff As Long

ff = myOtherSheet.Range("L2:L" & myOtherSheet.Cells(myOtherSheet.Rows.Count, "L").End(xlUp).Row).Count
With Sheets("Sheet1")
.Range("L2").Formula = "=IF(RC[-2]=RC[-1],""No"",""Yes"")"
Selection.AutoFill Destination:=Range("L2:L" & ff), Type:=xlFillDefault 'Error Line
End With

End Sub
4

1 回答 1

0

我相信有一个更好的答案,但到目前为止还没有其他答案:

Sub SiteAccess()
    Dim myBook As Workbook
    Dim myOtherSheet As Worksheet
    Dim ff As Long
    Set myBook = Excel.ActiveWorkbook
    Set myOtherSheet = myBook.Sheets("Sheet1")
    ff = myOtherSheet.Range("K1:K" & myOtherSheet.Cells(myOtherSheet.Rows.Count, "K") _
    .End(xlUp).Row).Count
    With Sheets("Sheet1")
        .Range("L2").Select
        .Range("L2").Formula = "=IF(RC[-2]=RC[-1],""No"",""Yes"")"
        Selection.AutoFill Destination:=Range("L2:L" & ff), Type:=xlFillDefault
    End With
End Sub
于 2013-08-06T19:35:33.353 回答