我希望用三个字符串中的一个自动填充一个单元格,PASS,FAIL和TBD
我下面的代码适用于PASS和FAIL senerios,但不适用于TBD
在一列(A栏)中,我有一堆文件的名称。如果文件名包含字符串“ANT”,我有一个跳闸值,填充FAIL,任何小于9.54的值
否则,将跳闸值设置为25.
我真正想要的是以下内容:
如果值包含 SPT - -SPT (其中astric是SPT之前或之后的任何值)我将跳闸值设置为25
否则,如果我在H列中看到0,则在L列中输入“TBD”
最新的代码使用了一个公式,如下所示:
Sub PassFail()
'Specify which sheet (name) of the active workbook we want to work on
With Sheets("(5) Coax Cable Sweeps Table")
'Determine the range we want to put values into
With .Range("L1:L" & .Range("A" & .Rows.Count).End(xlUp).Row)
'Put the standard worksheet formula into all cells at once by specifying the formula for the 1st cell only
'Note that any quote marks in the actual formula need to be doubled up at this point
'This line does not consider any zeros for DTF measurments taken without a load
.Formula = "=IF(H1<IF(ISNUMBER(SEARCH(""ANT"",A1)),9.54,25),""Fail"",""Pass"")"
'The below formula DOES NOT WORK as intended.
'.Formula = "=IF(H1=0,""TBD"",""=IF(H1<IF(ISNUMBER(SEARCH(""""ANT"""",A1)),9.54,25),""""Fail"""",""""Pass"""")"")"
.Value = .Value
End With
End With
End Sub
然后我尝试使用If,elseIf逻辑并提出以下内容:
Sub PassFail_v1()
Dim x As Long
Application.ScreenUpdating = False
With ActiveWorkbook.Sheets(8)
For x = 1 To .Cells(.Rows.Count, 1).End(xlUp).Row
If InStr(.Cells(x, 1).Value, "ANT") > 0 Then
.Cells(x, 12).Value = IIf(.Cells(x, 8).Value < 9.54, "Fail", "Pass")
ElseIf InStr(.Cells(x, 1).Value, "*SPT*-*STP*") > 0 Then
.Cells(x, 12).Value = IIf(.Cells(x, 8).Value < 25, "Fail", "Pass")
' Else
' .Cells(x, 12).Value = IIf(.Cells(x, 8).Value = 0, "TBD", " ")
End If
Next x
End With
Application.ScreenUpdating = True
End Sub
哪个还行不通.. 列L中的一些单元格留空,其他列H中的0表示列L中的TBD(这很好),而其他单元格则显示FAILS为0时不应该。
有什么建议吗?
答案 0 :(得分:2)
因此,如果我理解您的代码和您想要的结果,那么您正在查看A列,如果A(n)中字符串的任何部分包含&#34; ANT&#34;,则确定通过/失败的值是9.54。如果A(n)的任何部分包含字符串&#34; SPT - STP &#34;,则通过/失败值为25(顺便说一下,使用通配符在搜索中需要LIKE
并计算为布尔值。)但是,如果列H包含0,那么L(n)应该是&#34; TBD&#34;无论A(n)中的值如何,对吗?如果是这种情况,则需要两个IF语句,其中一个评估列H在评估列A之后:
With ActiveWorkbook.Sheets(8)
For x = 1 To .Cells(.Rows.Count, 1).End(xlUp).Row
If InStr(.Cells(x, 1).Value, "ANT") > 0 Then
.Cells(x, 12).Value = IIf(.Cells(x, 8).Value < 9.54, "Fail", "Pass")
ElseIf .Cells(x, 1).Value Like "*SPT*-*STP*" Then
.Cells(x, 12).Value = IIf(.Cells(x, 8).Value < 25, "Fail", "Pass")
End If
If .Cells(x, 8).Value = 0 Then
' .Cells(x, 12).Value = "TBD"
End If
Next x
End With
附录:应该注意的是,如果A(n)不包含&#34; ANT&#34;或某种形式的&#34; SPT - STP &#34;,L(n)将留空。因此,如果您看到L列中的单元格为空白,请查看A列并确定在这些情况下您要执行的操作。
答案 1 :(得分:1)
您已将公式的第二部分括在引号中。除了公式之外,不需要=
符号和额外引号
.Formula = "=IF(H1=0,""TBD"",""=IF(H1<IF(ISNUMBER(SEARCH(""""ANT"""",A1)),9.54,25),""""Fail"""",""""Pass"""")"")"
应该是
.Formula = "=IF(H1=0,""TBD"",IF(H1<IF(ISNUMBER(SEARCH(""ANT"",A1)),9.54,25),""Fail"",""Pass""))"