[英]return the row and column of the absolute value in excel vba
my question is why doesn't it like the AddressOfMaxRow & AddressOfMaxCol variables?我的问题是为什么它不喜欢 AddressOfMaxRow 和 AddressOfMaxCol 变量? Am I being an idiot?我是白痴吗?
Function MaxABS(rng, AddressOfMaxRow, AddressOfMaxCol, MaxVal)
Dim arr As Variant
arr = rng.Value
For i = 1 To UBound(arr, 1)
For j = 1 To UBound(arr, 2)
arr(i, j) = VBA.Abs(arr(i, j))
Next
Next
MaxVal = Application.WorksheetFunction.Max(arr)
AddressOfMaxRow = WorksheetFunction.Index(arr, WorksheetFunction.Match(WorksheetFunction.Max(arr), arr, 0)).Cells.Row
AddressOfMaxCol = WorksheetFunction.Index(arr, WorksheetFunction.Match(WorksheetFunction.Max(arr), arr, 0)).Cells.Column
End Function
Perhaps something like this, which returns the address (eg $D$9
) of the "max" cell, but can be tweaked to return the row and column as needed.也许像这样,它返回“最大”单元格的地址(例如$D$9
),但可以根据需要调整以返回行和列。
Note that this assumes that rng
only contains numeric values and can be beefed up to not error if there's text, for example.请注意,这假设rng
仅包含数字值,并且例如,如果有文本,则可以增强为不会出错。 It will also only return the first max value address if the max value appears more than once.如果最大值出现多次,它也只会返回第一个最大值地址。
Function MaxABS(rng As Range) As String
Dim arr As Variant
arr = rng.Value
Dim i As Long, j As Long
Dim tempMax As Double
Dim tempRow As Long, tempCol As Long
For i = 1 To UBound(arr, 1)
For j = 1 To UBound(arr, 2)
arr(i, j) = VBA.Abs(arr(i, j))
If arr(i, j) > tempMax Then
tempMax = arr(i, j)
tempRow = i
tempCol = j
End If
Next j
Next i
MaxABS = rng.Cells(tempRow, tempCol).Address
End Function
To return the row and column (separated by a comma since the desired output format was not specified), replace the last line要返回行和列(由于未指定所需的输出格式,因此用逗号分隔),替换最后一行
MaxABS = rng.Cells(tempRow, tempCol).Address
with the following:具有以下内容:
With rng.Cells(tempRow, tempCol)
MaxABS = .Row & "," & .Column
End With
with sample output of 9, 4
instead of $D$9
.样本输出为9, 4
而不是$D$9
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.