0

在下面给出的代码中,由 Robin Mackenzie 编排,我现在正试图从等式中获取绝对值。我尝试使用以下ABS()命令:

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

我认为我没有正确执行命令。你能帮我从方程中得到绝对值吗?

这是代码:

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
4

1 回答 1

2

最简单的方法是将Abs整个公式放在周围:

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

但是,如果您愿意,可以将它放在每个组件周围:

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

您必须记住的是strMeasCol & "2-" & strLowLimCol & "2使用两个地址创建一个公式,例如G2-K2- 所以您不能从其中开始ABS(即您在我的示例地址中放置了 the和 theABS(之间,给出了类似的东西)。G2GABS(2-K2

于 2017-06-26T03:36:36.463 回答