2017-06-25 20 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

爲了避免硬編碼的值,你可以嘗試'.Address' .Value'的'代替。 –

+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」不存在的工作表中,計算不會針對包含所有標題的下一個工作表完成。也就是說,只要LowLimit時」頭沒有被發現,沒有更多的計算進行了有沒有辦法解決這個問題由於 – Joe

+1

看到這個話題 - ?!https://stackoverflow.com/questions/20681306/skip- to-next-iteration-in-loop-vba - 我會去找BH提供的答案。 –