在下面给出的代码orchestrated by Robin Mackenzie中,我现在试图从等式中得到绝对值。我尝试使用ABS()
命令:
.Formula = "=IF(ISNUMBER(" & strLowLimCol & "2)," & _
strMeasCol & "ABS(2-" & strLowLimCol & "2," & _
strMeasCol & "2))"
我认为我没有正确执行命令。你能帮我从等式中得到一个abosulte值吗?
以下是代码:
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
答案 0 :(得分: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
部分通过(即您将ABS(
放在我的示例地址中的G
和2
之间,并提供类似GABS(2-K2
的内容。