2017-06-25 6 views
1

この質問は、solution found hereからビルドされています。私は、 "LowLimit"セルが数字かどうかをチェックできるようにしたかったのです。等式を実行する場合は、 "MeasValue"列から値を返します。ここに私の現在の結果とデータセットの例である:ISNUMBERが#VALUEを返す! VBAで式にエラーがあります

enter image description here

あなたが見ることができるように、第六データ入力の計算が間違っている計算になります。 LowLimitの値22の数は、数式にハードコードされているようです。これを解決する手助けはできますか?ありがとう。ここで

は、私がこれまで持っているコードは次のとおりです。

Sub ReturnMarginal() 
'UpdatebySUPERtoolsforExcel2016 
    Dim xOut As Worksheet 
    Dim xWb As Workbook 
    Dim xWks As Worksheet 
    Dim InterSectRange As Range 
    Dim lowLimCol As Integer 
    Dim hiLimCol As Integer 
    Dim measCol As Integer 

    Application.ScreenUpdating = False 
    Set xWb = ActiveWorkbook 
    For Each xWks In xWb.Sheets 
    xRow = 1 
    With xWks 
     FindString = "LowLimit" 
     If Not xWks.Rows(1).Find(FindString) Is Nothing Then 

     .Cells(xRow, 16) = "Meas-LO" 
     .Cells(xRow, 17) = "Meas-Hi" 
     .Cells(xRow, 18) = "Min Value" 
     .Cells(xRow, 19) = "Marginal" 
     lastRow = .UsedRange.Rows.Count 
     lowLimCol = Application.WorksheetFunction.Match("LowLimit", xWks.Range("1:1"), 0) 
     hiLimCol = Application.WorksheetFunction.Match("HighLimit", xWks.Range("1:1"), 0) 
     measLimCol = Application.WorksheetFunction.Match("MeasValue", xWks.Range("1:1"), 0) 

     'If IsNumeric(.Cells(2, lowLimCol).Value2) Then 
     '  .Range("P2:P" & LastRow).Formula = "=" & Cells(2, measLimCol).Address(False, False) & "-" & Cells(2, lowLimCol).Address(False, False) 
     'Else 
     '  .Range("P2:P" & LastRow).Formula = "=" & Cells(2, measLimCol).Address(False, False) 
     'End If 

     .Range("P2:P" & lastRow).Formula = "=IF(ISNUMBER(" & .Cells(2, lowLimCol).Value & ")," & Cells(2, measLimCol).Address(False, False) & "-" & Cells(2, lowLimCol).Address(False, False) & "," & Cells(2, measLimCol).Address(False, False) & ")" 

     .Range("Q2:Q" & lastRow).Formula = "=" & Cells(2, hiLimCol).Address(False, False) & "-" & Cells(2, measLimCol).Address(False, False) 


     .Range("R2").Formula = "=min(P2,Q2)" 
     .Range("R2").AutoFill Destination:=.Range("R2:R" & lastRow) 

     .Range("S2").Formula = "=IF(AND(R2>=-3, R2<=3), ""Marginal"", R2)" 
     .Range("S2").AutoFill Destination:=.Range("S2:S" & lastRow) 



     End If 

    End With 

    Application.ScreenUpdating = True 'turn it back on 

Next xWks 
End Sub 
+1

をあなたがすべてで、このためにVBAを使用しているのはなぜ -

私は少しあなたのコードを更新し、それが役に立てば幸い? –

+2

ハードコードされた値を避けるには、 '.Value'の代わりに' .Address'を試してみてください。 –

+0

私にはっきりさせてください。あなたはこのためにVBAを使うべきではありません。 –

答えて

2

私はここで行うことができます主な改善は、彼らが行のどこにいるか、あなたが確立するとLowLimitHighLimitMeasValueの列の文字を取得することだと思います1. .Formulaのプロパティを設定すると、これらの列の文字を参照できます。

カラム番号をアルファベットのhereに変換すると便利なポストがあります。

また、あなたが自動入力列RSする必要はありません - あなたは、列PQのためにやっているのと同じ方法で取り込むことができます。

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 ws.Rows(1).Find("LowLimit") Is Nothing Then Exit Sub 

     ' 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 
     With ws.Range("P2:P" & lngLastRow) 
      .Formula = "=IF(ISNUMBER(" & strLowLimCol & "2)," & _ 
       strMeasCol & "2-" & strLowLimCol & "2," & _ 
       strMeasCol & "2)" 
     End With 

     ' Meas-Hi 
     With ws.Range("Q2:Q" & lngLastRow) 
      .Formula = "=" & strHiLimCol & "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 

    Next 'ws 

End Sub 

出力:

enter image description here

+0

あなたのソリューションに感謝します。私は、「LowLimit」が1つのシートに存在しない場合、すべてのヘッダーを含む次のワークシートに対して計算が行われないことを発見しました。つまり、LowLimitヘッダーが見つからないと、それ以上の計算は実行されません。これを修正する方法はありますか?ありがとう! – Joe

+1

このスレッドを見る - https://stackoverflow.com/questions/20681306/skip- to-next-iteration-in-loop-vba - 私はBHによって提供された答えに行きます。 –

関連する問題