Excel VBA:如何为整个使用范围编写IF statemnt?

时间:2017-06-22 18:07:06

标签: excel vba excel-vba if-statement

我的代码遇到了一些麻烦。我试图在整个列中使用IF语句,直到IF语句中的行结束。当我尝试运行我的代码时,它会显示“错误1004:'应用程序定义或对象定义错误。”到目前为止,这是我的代码:

 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

1 个答案:

答案 0 :(得分:1)

这应该有用。

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