【问题标题】:ISNUMBER returning #VALUE! error in formula with VBAISNUMBER 返回#VALUE! VBA公式错误
【发布时间】:2017-11-28 12:18:06
【问题描述】:

这个问题是从solution found here 构建的。我希望能够检查“LowLimit”单元格是否是数字。如果是则执行方程,否则从“MeasValue”列返回值。这是我当前结果的数据集示例:

如您所见,第 6 次数据输入计算给出了错误的计算。公式中的数字 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

【问题讨论】:

  • 你为什么要使用 VBA?
  • 为避免硬编码值,您可以尝试使用.Address 而不是.Value
  • 让我明确一点:您应该为此使用 VBA。
  • @AndreTerra - 猜测 OP 正在使用 VBA,因为显示的代码暗示 LowLimitHighLimitMeasValue 不会总是出现在 HIJ 列中。
  • @RobinMackenzie 他可以为此使用命名范围

标签: vba excel excel-formula


【解决方案1】:

我认为您可以在此处进行的主要改进是获取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

输出:

【讨论】:

  • 感谢您的解决方案。我刚刚发现,如果在一张纸上不存在“LowLimit”,则不会为下一张包含所有标题的工作表进行计算。也就是说,只要没有找到“LowLimit”标头,就不再进行计算。有没有办法解决这个问题?谢谢!
  • 查看此线程 - stackoverflow.com/questions/20681306/… - 我会寻求 BH 提供的答案。
猜你喜欢
  • 2011-05-09
  • 1970-01-01
  • 2018-10-08
  • 1970-01-01
  • 2013-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多