I have a need to convert the following function to accept a user selected 2 dimensional range [datatype] in place of Arr [ the arbitrary custom defined Array] that is used in it's place. I want to do this so I can run worksheet functions on the data. I know it can be done when the range object is selected, but my data at one points need to be converted to a variant, then re assigned to a [new] range on the new worksheet (aka 'put the array values on the worksheet), then reread back as a range when it does (aka ' load the worksheet values back into the array)
Sub SortViaWorksheet()
Dim Arr(1 To 5) As String ' this is the array to be sorted
Dim WS As Worksheet ' temporary worksheet
Dim R As Range
Dim N As Long
' fill up the array with some
' aribtrary values.
Arr(1) = "aaa"
Arr(2) = "zzz"
Arr(3) = "mmm"
Arr(4) = "ttt"
Arr(5) = "bbb"
Application.ScreenUpdating = False
' create a new sheet
Set WS = ThisWorkbook.Worksheets.Add
' put the array values on the worksheet
Set R = WS.Range("A1").Resize(UBound(Arr) - LBound(Arr) + 1, 1)
R = Application.Transpose(Arr)
' sort the range
R.Sort key1:=R, order1:=xlAscending, MatchCase:=False
' load the worksheet values back into the array
For N = 1 To R.Rows.Count
Arr(N) = R(N, 1)
Next N
' delete the temporary sheet
Application.DisplayAlerts = False
WS.Delete
Application.DisplayAlerts = True
Application.ScreenUpdating = True
' test/debug/confirmation
For N = LBound(Arr) To UBound(Arr)
Debug.Print Arr(N)
Next N
End Sub
In other words, I want to be able to pass in a user selected 2d range that will be converted to a variant, then export this variant to a 2d range in a worksheet that is reloaded back into a range for vba to work with and run functions on. I understand this example uses .sort, which requires a 1d array. I do not intend on using sort, but rather things like worksheet functions on this new range, such as rank, countif, average, max, min, median, etc
My friend pointed out that the example above uses a 1d range as input and exports a 1d array to the worksheet. So I would need the ability to select a 2d range as input and export a 2d range to a worksheet that is subsequently reread as a new range.