编辑此代码以添加条件以根据行中的特定文本值反转公式
edit this code to add a condition to reverse the formula based on specific text value in rows
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))
代码目前可以运行,但我想添加一些内容。假设添加一个条件
(IF G2 = "AR17 - Standard Report" or "IT18 - Standard Report" or "CSYT - Standard Report", then the calculation should be (RC[-2]<=RC[-4],""Met"",""Not Met and if not, the calculation should be RC[-2]>=RC[-4],""Met"",""Not Met)
为了让事情更清楚。我想反转所有具有 "AR17 - Standard Report"、"IT18 - Standard Report" 和 "CSYT - Standard Report" 的行的当前公式。我有数以千计的数据,所以我希望所有具有这些值的行都有一个反转的公式。我希望有一个人可以帮助我。我是 VBA.
的新手
已编辑:现在这是我的代码。没有错误,但对于 G 列中的那些指定文本,公式仍然没有改变。
Range("N2").Select
If Not IsError(Application.Match(Range("G2"), Array("sample1", "sample2", "sample3"), False)) Then
'First statement
ActiveCell.FormulaR1C1 = "=IF(RC[-2]<=RC[-4],""Met"",""Not Met"")"
Selection.AutoFill Destination:=Range(Cells(2, 14), Cells(a, 14))
Else
'Second
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))
End If
我家里没有Excel,不过你可以试试这个:
MAXROW = 20000 ' number of rows you have
'edit
for i = 2 to MAXROW
Range("N" & i).Select
If Not IsError(Application.Match(Range("G"& i), Array("AR17 - Standard Report", "IT18 - Standard Report" , "CSYT - Standard Report" ), False)) Then
'First statement
ActiveCell.FormulaR1C1 = "=IF(RC[-2]<=RC[-4],""Met"",""Not Met"")"
else
'Second
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""))))"
endif
Next
您可以通过查看宏录制给您的内容来调整公式。
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))
代码目前可以运行,但我想添加一些内容。假设添加一个条件
(IF G2 = "AR17 - Standard Report" or "IT18 - Standard Report" or "CSYT - Standard Report", then the calculation should be (RC[-2]<=RC[-4],""Met"",""Not Met and if not, the calculation should be RC[-2]>=RC[-4],""Met"",""Not Met)
为了让事情更清楚。我想反转所有具有 "AR17 - Standard Report"、"IT18 - Standard Report" 和 "CSYT - Standard Report" 的行的当前公式。我有数以千计的数据,所以我希望所有具有这些值的行都有一个反转的公式。我希望有一个人可以帮助我。我是 VBA.
的新手已编辑:现在这是我的代码。没有错误,但对于 G 列中的那些指定文本,公式仍然没有改变。
Range("N2").Select
If Not IsError(Application.Match(Range("G2"), Array("sample1", "sample2", "sample3"), False)) Then
'First statement
ActiveCell.FormulaR1C1 = "=IF(RC[-2]<=RC[-4],""Met"",""Not Met"")"
Selection.AutoFill Destination:=Range(Cells(2, 14), Cells(a, 14))
Else
'Second
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))
End If
我家里没有Excel,不过你可以试试这个:
MAXROW = 20000 ' number of rows you have
'edit
for i = 2 to MAXROW
Range("N" & i).Select
If Not IsError(Application.Match(Range("G"& i), Array("AR17 - Standard Report", "IT18 - Standard Report" , "CSYT - Standard Report" ), False)) Then
'First statement
ActiveCell.FormulaR1C1 = "=IF(RC[-2]<=RC[-4],""Met"",""Not Met"")"
else
'Second
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""))))"
endif
Next
您可以通过查看宏录制给您的内容来调整公式。