我正在尝试从一个范围中找到最小到下一个最小值,并按照从最小到最大的顺序返回每个值的单元格地址,直到找到最大值。作为一个例子......如果A1:I1以随机顺序包含1-9的整数而没有重复,我如何找到单元格的单元格地址为1,然后单元格地址为2,等等,直到发现9的单元格地址?我希望我可以使用SMALL并在循环中使用变量递增n但是会出错。当我在SMALL函数中使用SmallCount时,我逐步执行此例程“lower = Error 2029”。如果我用实际整数替换Smallcount,则没有问题。任何帮助都表示赞赏....这里基本上我在......
Sub NotWorking()
LoopCount = 9
SmallCount = 1
Do While LoopCount <> "0"
smaller = [CELL("address",INDEX(A1:I1,MATCH(SMALL(A1:I1,SmallCount), A1:I1,0)))]
LoopCount = LoopCount - 1
SmallCount = SmallCount + 1
Loop
End Sub
答案 0 :(得分:0)
我不太确定你的目标,但你可以试试这段代码:
Sub HopefullyWorking()
Dim i As Long
Dim col As Variant
With Range("A1:I1") ' <--| reference your range
For i = 1 To .Count '<--| loop from 1 to referenced range cells number
col = Application.Match(WorksheetFunction.Small(.Cells, i), .Cells, 0) '<--| try finding current "small" number in referenced cells
If Not IsError(col) Then MsgBox "Number " & i & " found at position " & col
Next
End With
End Sub