Sorting in Array

Peterso

Board Regular
Joined
Nov 28, 2012
Messages
113
I wrote the following:

Sub mark6()
Dim A(6) As String
For i = 1 To 6
A(i) = InputBox("Enter Number " & i)
Next i
End Sub

I would like to know how to write the script so as to rearrange and sort the 6 numbers from the smallest to the bigest. ie A(1) is the smallest and A(6) is the biggest. thanks.
 
Last edited:

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
If you declare your array variable A as a Variant data type, then you can use this method to create your sorted array...
Code:
Sub CreateSortedArray()
  Dim i As Long, A As Variant
  With CreateObject("System.Collections.ArrayList")
    For i = 1 To 6
      .Add InputBox("Enter Number ")
    Next i
    .Sort
    A = .ToArray
  End With
[COLOR="#008000"][B]  '
  '  At this point, the variable A will contain a
  '  one-dimensional array of six elements
  '  whose indexes range from 0 to 5.
  '
[/B][/COLOR]End Sub
 
Upvote 0

Forum statistics

Threads
1,223,948
Messages
6,175,570
Members
452,652
Latest member
eduedu

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