Excel VBA:如何写入IF statemnt的整个使用的范围?

我的代码有些麻烦。 我试图使用一个IF语句的整个列直到IF语句内的行结束。 当我尝试运行我的代码时,它会显示“Error 1004:'Application defined or object defined error。” 这是我的代码到目前为止:

Sub AddWorksheet5() Sheets.Add.Name = "Info1" End Sub Sub MoveColumns3() Sheets("SAP data Weekly").UsedRange.Copy Destination:=Sheets("Info1").UsedRange End Sub Sub Filter2() Worksheets("Info1").Range("A1").AutoFilter _ Field:=8, _ Criteria1:=">=1/1/2018", _ Criteria2:="<=12/31/2018", _ VisibleDropDown:=False End Sub Sub Addcolumn() Range("L1").Formula = "=IF(OR(LEFT($G1,2)=""55"",LEFT($G1,2)=""45"",$G1=""FORECAST""),$G1,""No"")" 'It works until this line: Range("L1", "L" & Cells(Rows.Count, 1).End(xlUp).Row).FillDown End Sub Sub AddWorksheet6() Sheets.Add.Name = "Info2" End Sub Sub MoveColumns4() Sheets("Info1").Columns(8).Copy Destination:=Sheets("Info2").Columns(4) Sheets("Info1").Columns(6).Copy Destination:=Sheets("Info2").Columns(3) Sheets("Info1").Columns(9).Copy Destination:=Sheets("Info2").Columns(2) Sheets("Info1").Columns(5).Copy Destination:=Sheets("Info2").Columns(1) Sheets("Info1").Columns(12).Copy Destination:=Sheets("Info2").Columns(5) Sheets("Info1").Columns(4).Copy Destination:=Sheets("Info2").Columns(6) End Sub Sub Vlookup() Dim SourceLastRow As Long Dim OutputLastRow As Long Dim sourceSheet As Worksheet Dim outputSheet As Worksheet Set sourceSheet = Worksheets("Data1") Set outputSheet = Worksheets("Info2") With sourceSheet SourceLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row End With With outputSheet OutputLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row .Range("G2:G" & OutputLastRow).Formula = _ "=VLOOKUP(A2,'" & sourceSheet.Name & "'!$A$2:$B$" & SourceLastRow & ",2,0)" End With End Sub Sub Filter3() Worksheets("Info2").Range("A1").AutoFilter _ Field:=7, _ Criteria1:="<>#N/A", _ VisibleDropDown:=False End Sub Sub Filter4() Worksheets("Info2").Range("A1").AutoFilter _ Field:=5, _ Criteria1:="<>No", _ VisibleDropDown:=False End Sub Sub PivotTable() ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=Sheets("Info2").UsedRange).CreatePivotTable TableDestination:="", TableName:="PivotTable", DefaultVersion:=xlPivotTableVersion10 ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(1, 1) With ActiveSheet.PivotTables("PivotTable").PivotFields("PN") .Orientation = xlRowField .Position = 1 End With With ActiveSheet.PivotTables("PivotTable").PivotFields("Commit") .Orientation = xlColumnField .Position = 1 End With ActiveSheet.PivotTables("PivotTable").AddDataField ActiveSheet.PivotTables("PivotTable").PivotFields("Qty"), "Sum", xlSum End Sub Sub GroupPivot() Dim myrange As Range Dim PT As PivotTable Set PT = ActiveSheet.PivotTables(1) Set myrange = PT.PivotFields("Commit").DataRange.Cells(1) myrange.Select Selection.Group Start:=True, End:=True, Periods:=Array(False, False, False, _ False, True, False, False) End Sub Sub NameSheet() ActiveSheet.Name = "Pivot" End Sub 

这应该工作。

 Sub AddColumn() Dim ws As Worksheet Set ws = Sheets("Info1") ws.Range("L1").Formula = "=IF(OR(LEFT($G1,2)=""55"",LEFT($G1,2)=""45"",$G1=""FORECAST""),$G1,""No"")" ws.Range("L1").AutoFill Destination:=ws.Range("L1:L" & ws.Cells(Rows.Count, "G").End(xlUp).Row) End Sub