我一直在为这段代码苦苦挣扎(可能是非常简单的错误),有人介意指出我的问题在哪里吗?我的总体目标是允许这个子例程接受一系列可变大小,但是我似乎无法让它在固定大小下工作。
如果我手动分配数组,事情会按预期工作,但是当我分配一个范围时,就会出现问题。输出原封不动地返回,这让我相信我在分配方面做得不对。当我尝试将 ws.UsedRange 与固定范围相反时,我也会遇到错误。
Private Sub InsertionSort(ByRef a(), ByVal lo0 As Long, ByVal hi0 As Long)
Dim i As Long, j As Long, v As Long
For i = lo0 + 1 To hi0
v = a(i)
j = i
Do While j > lo0
If Not a(j - 1) > v Then Exit Do
a(j) = a(j - 1)
j = j - 1
Loop
a(j) = v
Next i
End Sub
Sub runSort()
Dim ws As Worksheet
Set ws = ActiveWorkbook.ActiveSheet
Dim myArr() As Variant
Dim rangeUse As Range
With ws.Range("D17:K17")
ReDim myArr(1 To 1, 1 To ws.Range("D17:K17").Columns.Count)
myArr = ws.Range("D17:K17").Value
End With
Call InsertionSort(myArr, LBound(myArr), UBound(myArr))
Range("D19:K19") = myArr
End Sub
任何帮助,将不胜感激!TIA