此子例程检查每一行的最低值。我想知道这个值在哪一列。我该如何处理? 这是我到目前为止所得到的:
Sub LowCol()
Dim iAnz As Long
Dim myrange As Range
Dim rCell As Range
Dim iLevel As Long
Dim i As Long
Dim j As Long
Dim iPos As Long
Dim dMin As Double
iAnz = Worksheets("v1").Cells(6, 3).Value 'amount of columns
iLevel = Worksheets("b1").Cells(2, 2).Value 'amount of rows
For i = 0 To iLevel
Set myrange = Worksheets("b1").Range(Cells(5 + i, 2 + iAnz + 1), Cells(5 + i, 2 + iAnz + iAnz + 1))
dMin = Application.WorksheetFunction.Min(myrange)
Set rCell = myrange.Find(What:=dMin, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
iPos = CLng(rCell.Column) 'this line is obviously not working. How to fix?
Worksheets("b1").Cells(5 + i, 2 + iAnz + iAnz + 1).Value = iPos
Next i
End Sub
答案 0 :(得分:1)
您不需要VBA来获取MIN
值的列数。只需使用MATCH
的公式,例如:
=MATCH(MIN(A1:G1);A1:G1;0)
答案 1 :(得分:1)
这并不像Min函数一样快,但除非您使用大型数据集,否则它不会产生太大的影响。
Function getMinValueColumn(Target As Range) As Long
Dim r As Range, rMin As Range
If Target Is Nothing Then Exit Function
For Each r In Target
If r.Value <> vbNullString Then
If rMin Is Nothing Then Set rMin = r
If r.Value < rMin.Value Then Set rMin = r
End If
Next
If Not rMin Is Nothing Then getMinValueColumn = rMin.Column
End Function