Please explain this code vba about sort number

Hero123

New Member
Joined
Sep 25, 2017
Messages
6
Hello Everyone
I am studying the VBA and looking for this code about sort number .I think so much but did not understand .maybe i am stupid!
.............
Dim i As Integer, j As Integer, temp As Integer, rng As Range
Set rng = Range("A1").CurrentRegion
For i = 1 To rng.Count
For j = i + 1 To rng.Count
If rng.Cells(j) < rng.Cells(i) Then
'swap numbers
temp = rng.Cells(i)
rng.Cells(i) = rng.Cells(j)
rng.Cells(j) = temp
End If
..........
i did understand this code : rng.cells(j)=temp .
why not use temp=rnd.cell(j) and what ' different ?
Please explain
Thanks.
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
This is an implementation of a Bubble Sort (see https://en.wikipedia.org/wiki/Bubble_sort)

When you need to swap numbers, say in A3 and A4, you have to do the following:

- Store the value of A3 in a variable (temp in this case)
- Replace A3 with the value in A4
- Replace the value in A4 with the stored value (from temp)

WBD
 
Upvote 0

Forum statistics

Threads
1,223,984
Messages
6,175,783
Members
452,669
Latest member
reeseann

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top