Sorting in Array

Peterso

Board Regular
Joined
Nov 28, 2012
Messages
88
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:

Some videos you may like

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,686
Office Version
  1. 2010
Platform
  1. Windows
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
 

Watch MrExcel Video

Forum statistics

Threads
1,122,689
Messages
5,597,559
Members
414,156
Latest member
WDMix

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
Top