ISNUMBER正在返回#VALUE! 与VBA公式中的错误

这个问题是从这里find的解决scheme 。 我希望能够检查“LowLimit”单元是否是一个数字。 如果是执行方程式,则返回“MeasValue”列中的值。 下面是一个以我目前的结果为基础的数据集的例子:

在这里输入图像说明

正如你所看到的,第六次数据input计算给出了错误的计算。 22的数字LowLimit值似乎在公式中被硬编码。 你能帮我解决这个问题吗? 谢谢。

这是我迄今为止的代码:

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 

我认为你可以在这里做的主要改进是,一旦你build立了第一行的位置,就得到LowLimitHighLimitMeasValue的列字母。然后当你设置.Formula属性的时候你可以参考这些列字母。

这里有一个有用的文章转换列号到字母。

另外,您不需要自动填充列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 

输出:

在这里输入图像说明