I have scoured Google and the Stack for examples of what I am trying to do, while I have found some examples the perform part of what I am trying to do, I am having trouble accomplishing the desired result. I have included the below code which seems to look like it will do what I want to accomplish. Unfortunately I get a "Subscript out of range" which I assume has to do with my syntax.
Per the screenshots included I am trying to sort the columns found on "Sheet2" by the order of the values found in Column A on "Sheet1". Conceptually,
I figure the best route would be to assign the values in sheet1 to an array, the columns in sheet2 to an array and then order array2 according to the array1.
Any thoughts on how to accomplish this? Any help is appreciated:
Public Sub sortColumn()
Dim rng As Range
Dim i As Integer
Dim J As Integer
Dim Temp
Dim nams As Variant
Dim F
Dim Dex As Integer
Dim Arr As Variant
nams = Array(Worksheets("Sheet1").Range("A1:A350").Value2)
Set rng = Worksheets("Sheet2").Range("B1:JS1")
For i = 1 To rng.Columns.Count
For J = i To rng.Columns.Count
For F = 0 To UBound(nams)
If nams(F) = rng(J) Then Dex = F: Exit For
Next F
If F < i Then
Temp = rng.Columns(i).value
rng(i).Resize(rng.Rows.Count) = rng.Columns(J).value
rng(J).Resize(rng.Rows.Count) = Temp
End If
Next J
Next i
End Sub
Above code, as stated above, results in a "Sub script out of range". I have checked by range names and they are valid. Below graphics show what I am working with: