2017-06-26 11 views
0

文字列を確認するために、以下のコードを修正しようとしていました。---文字列は値9999を返し、その後、存在する場合、他の式を実行します。if cell = value if else else ... Excel VBA

.Formula = "=IF(" & strLowLimCol & "2" = ""---"," & _ strMeasCol & "2-" & strLowLimCol & "2," & _ 9999)"

私の誤差がある場合、あなたは私に言うことはできますか?ここで

は完全なコードです:

Option Explicit 

Sub ReturnMarginal() 

    Dim ws As Worksheet 
    Dim lngLowLimCol As Long, strLowLimCol As String 
    Dim lngHiLimCol As Long, strHiLimCol As String 
    Dim lngMeasCol As Long, strMeasCol As String 
    Dim lngLastRow As Long 
    Dim wsf As WorksheetFunction 

    ' get worksheetfunction references 
    Set wsf = Application.WorksheetFunction 

    ' iterate worksheets 
    For Each ws In ThisWorkbook.Worksheets 

     ' validate LowLimit label is on sheet 
     If Not (ws.Rows(1).Find("LowLimit") Is Nothing) Then 

     ' get location of input data columns and number of rows 
     lngLowLimCol = wsf.Match("LowLimit", ws.Rows(1), 0) 
     lngHiLimCol = wsf.Match("HighLimit", ws.Rows(1), 0) 
     lngMeasCol = wsf.Match("MeasValue", ws.Rows(1), 0) 
     lngLastRow = ws.Cells(1, lngLowLimCol).End(xlDown).Row 

     ' get column letters for input data columns 
     strLowLimCol = Split(ws.Cells(1, lngLowLimCol).Address(True, False), "$")(0) 
     strHiLimCol = Split(ws.Cells(1, lngHiLimCol).Address(True, False), "$")(0) 
     strMeasCol = Split(ws.Cells(1, lngMeasCol).Address(True, False), "$")(0) 

     ' output headers 
     ws.Range("P1") = "Meas-LO" 
     ws.Range("Q1") = "Meas-Hi" 
     ws.Range("R1") = "Min Value" 
     ws.Range("S1") = "Marginal" 

     ' assign formulas to outputs 
     ' Meas-LO 
     'Range("P2:P" & lngLastRow).Select 
     ' With Selection 
     '  Selection.NumberFormat = "General" 
     '  .Value = .Value 
     ' End With 
     With ws.Range("P2:P" & lngLastRow) 
      .Formula = "=IF(" & strLowLimCol & "2" = ""---"," & 
       strMeasCol & "2-" & strLowLimCol & "2," & _ 
       9999)" 

     End With 

     ' Meas-Hi 
     With ws.Range("Q2:Q" & lngLastRow) 
      .Formula = "=IF(ISNUMBER(" & strHiLimCol & "2)," & _ 
       strMeasCol & "2-" & strHiLimCol & "2," & _ 
       9999 & "2)" 
       'strMeasCol & "2)" 
     End With 

     ' Min Value 
     With ws.Range("R2:R" & lngLastRow) 
      .Formula = "=MIN(P2,Q2)" 
     End With 

     ' Marginal 
     With ws.Range("S2:S" & lngLastRow) 
      .Formula = "=IF(AND(R2>=-3,R2<=3),""Marginal"",R2)" 
     End With 
     End If 

    Next ws 

End Sub 
+1

あなたが欠落している ' "'' 9999の初めに)" '' 9999 'でなければならない) '' –

答えて

2

あなたは「いくつかの誤ったを持っている」:

.Formula = "=IF(" & strLowLimCol & "2 = ""---""," & _ 
strMeasCol & "2-" & strLowLimCol & "2, 9999)" 

strLowLimColだったのであれば、最終的な出力は(だろう、私はあなたのラインがあるべきだと思います」 B C」とstrMeasColだった "")

IF(B2 = "---"、C2-B2,9999)