2016-03-10 63 views
1

[代碼}的Excel/VBA /宏基於特定條件扭轉式

範圍( 「A2」)。選擇一個 =範圍(精選,Selection.End(xlDown))。COUNT + 1

Range("N2").Select 
ActiveCell.FormulaR1C1 = _ 
    "=IF(RC[-4]="""","""",IF(RC[-2]=""NA"",""N/A"",IF(RC[-2]=""N/A""," & _ 
      """N/A"",IF(RC[-2]>=RC[-4],""Met"",""Not Met""))))" 
Selection.AutoFill Destination:=Range(Cells(2, 14), Cells(a, 14)) 

[code] 這是目前我編寫公式的代碼。這是完美的工作,但我需要添加一些東西。我需要對F列中具有特定文本的每一行都有一個反轉公式。這些是:AR17-Report,CS02-Report,HSCBD-Report,KHG-Report。如果列F有任何這些文本。列N中的公式應顛倒過來。 而不是>,它應該是<。我是vba /宏的新手。

[代碼]

MAXROW = 10000 
For i = 1 To MAXROW 
     Range("N" & i).Select 
     If Not IsError(Application.Match(Range("F" & i), Array("AR17 - Past Due Receivables Outstanding", "AR18 - Past Due Receivables Outstanding greater than 60 days", "HRSC01NA - Call Abandonment Rate ", "ESC07 - Call Abandonment"), False)) Then 
      ActiveCell.FormulaR1C1 = "=IF(RC[-2]<=RC[-4],""Met"",""Not Met"")" 
     Else 
    ActiveCell.FormulaR1C1 = _  "=IF(RC[-4]="""","""",IF(RC[-2]=""NA"",""N/A"",IF(RC[-2]=""N/A"",""N/A"",IF(RC[-2]>=RC[-4],""Met"",""Not Met""))))" 
     End If 
    Next 

[代碼]

現在,這個代碼是工作和它被反轉式。然而,當我將我的代碼替換爲這個公式以反轉公式時,我的其他工作表中的數據透視表沒有更新或沒有獲取數據。

現在,這裏是我的全部代碼:

[代碼]

Sub Button5_Click() 

Dim b As Integer 
    Sheets("database_2").Select 
    ActiveSheet.Range("$A$1:$P$7436").AutoFilter Field:=8, Criteria1:="=LIVE", _ 
     Operator:=xlOr, Criteria2:="=" 
    Rows("2:2").Select 
    Range(Selection, Selection.End(xlDown)).Select 
    Selection.Delete Shift:=xlUp 
    Selection.AutoFilter 
    Range("A1").Select 
    Selection.AutoFilter 
    Range("A2").Select 
    b = Range(Selection, Selection.End(xlDown)).Count + 2 
    Range("A2").Select 
    Cells(b, 1).Select 

    Windows("NEW Consolidated Data Point System_Final.xlsm").Activate 
    Range("D1").AutoFilter Field:=4, Criteria1:=Array("HR", "OM", "PY"), Operator:=xlFilterValues 

    For Each rngCell In Range("D2:D" & Range("D2").End(xlDown).Row) 
    If Not rngCell.EntireRow.Hidden Then 

    rngCell.Value = "ES" 

    End If 
    Next rngCell 
    Selection.AutoFilter 

    Windows("Compliance.csv").Activate 
    Range("I1").AutoFilter Field:=9, Criteria1:=Array("KPI", "CPI", "GPI"), Operator:=xlFilterValues 

    For Each rngCell In Range("I2:I" & Range("I2").End(xlDown).Row) 
    If Not rngCell.EntireRow.Hidden Then 

    rngCell.Value = "NBS" 

    End If 
    Next rngCell 
    Selection.AutoFilter 


    Range("N2").Select 
    i = Range(Selection, Selection.End(xlDown)).Count + 1 

    Range("P2").Select 
    ActiveCell.FormulaR1C1 = "=TEXT(RC[-2],""mmmm"")" 
    Range("P2").Select 
    Selection.AutoFill Destination:=Range(Cells(2, 16), Cells(i, 16)) 
    Range(Cells(2, 16), Cells(i, 16)).Select 
    Selection.Copy 
    Range("N2").Select 
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
     :=False, Transpose:=False 
    Range(Cells(2, 16), Cells(i, 16)).Delete 


    Range("C2:M2").Select 
    Range(Selection, Selection.End(xlDown)).Select 
    Selection.Copy 
    Windows("NEW Consolidated Data Point System_Final.xlsm").Activate 
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
     :=False, Transpose:=False 

    Windows("Compliance.csv").Activate 
    Range(Cells(2, 15), Cells(i, 15)).Copy 

    Windows("NEW Consolidated Data Point System_Final.xlsm").Activate 
    Cells(b, 12).Select 
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
     :=False, Transpose:=False 

    Windows("Compliance.csv").Activate 
    Range(Cells(2, 14), Cells(i, 14)).Copy 

    Windows("NEW Consolidated Data Point System_Final.xlsm").Activate 
    Cells(b, 13).Select 
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
     :=False, Transpose:=False 


    Range("A2").Select 
    A = Range(Selection, Selection.End(xlDown)).Count + 1 

    Range("N2").Select 
    ActiveCell.FormulaR1C1 = _ 
     "=IF(RC[-4]="""","""",IF(RC[-2]=""NA"",""N/A"",IF(RC[-2]=""N/A"",""N/A"",IF(RC[-2]>=RC[-4],""Met"",""Not Met""))))" 
    Selection.AutoFill Destination:=Range(Cells(2, 14), Cells(A, 14)) 

    Range("O2").Select 
    ActiveCell.FormulaR1C1 = _ 
     "=IF(RC[-1]="""","""",IF(RC[-1]=""NA"",""N/A"",IF(RC[-1]=""Met"",1,0)))" 
    Selection.AutoFill Destination:=Range(Cells(2, 15), Cells(A, 15)) 

    Range("P2").Select 
    ActiveCell.FormulaR1C1 = _ 
     "=IF(RC[-2]="""","""",IF(RC[-2]=""NA"",0,IF(RC[-2]=""N/A"",0,1)))" 
    Selection.AutoFill Destination:=Range(Cells(2, 16), Cells(A, 16)) 

    DataArea = "database_2!R1C1:R" & A & "C16" 

    Sheets("Per Month").Select 
    Range("Q12").Select 
    ActiveSheet.PivotTables("PivotTable5").ChangePivotCache ActiveWorkbook. _ 
     PivotCaches.Create(SourceType:=xlDatabase, SourceData:=DataArea _ 
     , Version:=xlPivotTableVersion15) 
    ActiveWorkbook.RefreshAll 

     Sheets("Per Market").Select 
    Range("Q12").Select 
    ActiveSheet.PivotTables("PivotTable4").ChangePivotCache ActiveWorkbook. _ 
     PivotCaches.Create(SourceType:=xlDatabase, SourceData:=DataArea _ 
     , Version:=xlPivotTableVersion15) 
    ActiveWorkbook.RefreshAll 

Sheets("Computation").Select 
    Range("Q12").Select 
    ActiveSheet.PivotTables("PivotTable7").ChangePivotCache ActiveWorkbook. _ 
     PivotCaches.Create(SourceType:=xlDatabase, SourceData:=DataArea _ 
     , Version:=xlPivotTableVersion15) 
    ActiveWorkbook.RefreshAll 

    Sheets("database_2").Select 


End Sub 

[代碼]

回答

1

扭轉公式可能看起來像一個過程:

Sub ReverseFormula() 

    Dim c As Range 
    Dim sReversedFormula As String 

    For Each c In ThisWorkbook.Worksheets("TypeProperName").Range("F1:F100").Cells 
     Select Case c.Value 
      Case "AR17-Report", "CS02-Report", "HSCBD-Report", "KHG-Report" 

       '=IF(J2="","",IF(L2="NA","N/A",IF(L2="N/A","N/A",IF(L2<=J2,"Met","Not Met")))) 
       sReversedFormula = "=IF(J" & c.Row & "='',''IF(L" & c.Row & "='NA', 'N/A', IF(L" & c.Row & "<=J" & c.Row & ", 'Met', 'Not Met')))" 
       c.Offset(ColumnOffset:=8).Formula = sReversedFormula 
      Case Else 
       'do nothing 
     End Select 
    Next 

End Sub 

如何用它?

  1. 打開你想改變
  2. 轉到代碼窗格(ALT + F11)
  3. 插入新的模塊(Insert菜單=>模塊)
  4. 複製並粘貼上述過程
  5. 更改文件代碼以您的需求
  6. 運行(F5)

注意:在執行上面的代碼之前,別忘記備份你的文件

+0

我粘貼的代碼只是我整個代碼的一部分。它在中間,所以我只需要添加一些東西。一切都通過一次點擊來執行。我添加了一個按鈕併爲其分配了一個宏。或任何建議來扭轉公式。我的代碼中的默認公式是= IF(J2 =「」,「」,IF(L2 =「NA」,「N/A」,IF(L2 =「N/A」,「N/A」 L2> = J2,「Met」,「Not Met」))))。當列F中存在任何這些文本時,列N中的公式應該是= IF(J2 =「」,「」,IF(L2 =「NA」,「N/A」,IF(L2 =「N/A「,」N/A「,IF(L2 <= J2,」Met「,」Not Met「))) – markerbean

+0

無論您發佈哪部分代碼。您可以在代碼中使用上述過程的主體。正如你所看到的,我使用字符串變量來保存公式,所以你甚至可以在for循環內改變它爲'sReversedFormula =「= IF(L」&c.Row&「=''」...' –

+0

嘗試過,並且在將公式替換爲sReversedFormula =「= IF(J2 =」「,」「,IF(L2 =」NA「,」N/A「,IF(L2 =」N/A「 ,「N/A」,IF(L2> = J2,「Met」,「Not Met」))))「 – markerbean