我创建的函数有 2 个问题。首先是二维数组上 2 列的冒泡排序(降序排序 - 首先是第 2 列,然后是第 1 列)。我相信我已经正确地实施了这一点,但结果往往不同。
输入
COL 1 COL 2
35.484%38.296%
1.075%0.112%
1.075%0.056%
48.387%0.124%
1.075%0.005% 2.151
%0.051%
2.151%0.006%
2.151%0.002%
3.226%0.003%
1.075%0.032%
1.075%0.184%
1.075 % 0.263%输出
35.484%38.296%
48.387%0.124%
1.075%0.112% 2.151
%0.051%
2.151%0.006%
1.075%0.056%
3.226%0.003%
1.075%0.005%
1.075%0.032%
1.075%0.184%
2.151%0.002%
1.075%0.263%
您可以立即看到,输出的最后一行应该位于更高阶的某个位置。
而且我无法将整个数组输出到一个范围。没有显示错误,函数简单地退出。我很感激这方面的任何帮助。代码如下所示,再次感谢。
Function larger(range1 As Range, range2 As Range)
Dim Q() As Variant
Dim x As Range, y As Range
Dim i As Integer, j As Integer
Dim varTemp(1 To 2) As Variant
Q = Range(range1.address, range2.address)
ReDim Q(1 To UBound(Q, 1), 1 To UBound(Q, 2))
j = 1
i = 1
While i < UBound(Q, 1)
For Each x In range1
While j < UBound(Q, 2)
Q(i, j) = x
j = j + 1
Q(i, j) = range2(i)
Wend
i = i + 1
j = 1
Next
Wend
' Bubble sort - first with the 2nd col and then the 1st col
'2nd col
For i = LBound(Q) To UBound(Q) - 1
If Q(i, 2) < Q(i + 1, 2) Then
For j = 1 To 2
varTemp(j) = Q(i, j)
Q(i, j) = Q(i + 1, j)
Q(i + 1, j) = varTemp(j)
Next j
End If
Next
'1st col
For i = LBound(Q) To UBound(Q) - 1
If Q(i, 1) < Q(i + 1, 1) Then
For j = 1 To 2
varTemp(j) = Q(i, j)
Q(i, j) = Q(i + 1, j)
Q(i + 1, j) = varTemp(j)
Next j
End If
Next
j = 1
For i = LBound(Q, 1) To UBound(Q, 1)
MsgBox (Q(i, j) & " " & Q(i, j + 1))
Next
MsgBox ("end")
Range("P3:Q14") = Q 'Not writing entire queue into specified range
End Function