Hello , i have an array A with element, i need to sort that array ( the lenght of array depend on n - cellB2)by taking element and putting it into the right place, by doing a loop it should give me an ordered list in column D. Can someone please tell me if my code is close to the correct code?
Sub InsertSort()
Dim i As Integer
n = Range("B2").Value
Dim test As Double
Dim temp As Double
Dim j As Integer
For i = 2 To n
j = A(i - 1, 1)
test = A(i, 1)
Do While j > 0
If A(j, 1) > test Then
temp = A(j, 1)
A(j, 1) = A(j + 1, 1)
A(j + 1, 1) = temp
j = A(j - 1, 1)
Else
test = A(j, 1)
End If
Loop
If j = 0 Then
test = A(1, 1)
End If
Next i
Range("D1:D100000").Value = A
End Sub
Sub InsertSort()
Dim i As Integer
n = Range("B2").Value
Dim test As Double
Dim temp As Double
Dim j As Integer
For i = 2 To n
j = A(i - 1, 1)
test = A(i, 1)
Do While j > 0
If A(j, 1) > test Then
temp = A(j, 1)
A(j, 1) = A(j + 1, 1)
A(j + 1, 1) = temp
j = A(j - 1, 1)
Else
test = A(j, 1)
End If
Loop
If j = 0 Then
test = A(1, 1)
End If
Next i
Range("D1:D100000").Value = A
End Sub