[COLOR="Navy"]Sub[/COLOR] MG26May29
[COLOR="Navy"]Dim[/COLOR] rng [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] i [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer[/COLOR]
[COLOR="Navy"]Dim[/COLOR] J [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Temp
[COLOR="Navy"]Set[/COLOR] rng = Range("A1").CurrentRegion
[COLOR="Navy"]For[/COLOR] i = 1 To rng.Columns.Count
[COLOR="Navy"]For[/COLOR] J = i To rng.Columns.Count
[COLOR="Navy"]If[/COLOR] rng(J) < rng(i) [COLOR="Navy"]Then[/COLOR]
Temp = rng.Columns(i).Value
rng(i).Resize(rng.Rows.Count) = rng.Columns(J).Value
rng(J).Resize(rng.Rows.Count) = Temp
[COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] J
[COLOR="Navy"]Next[/COLOR] i
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Try this:-
Regards MickCode:[COLOR=navy]Sub[/COLOR] MG26May29 [COLOR=navy]Dim[/COLOR] rng [COLOR=navy]As[/COLOR] Range [COLOR=navy]Dim[/COLOR] i [COLOR=navy]As[/COLOR] [COLOR=navy]Integer[/COLOR] [COLOR=navy]Dim[/COLOR] J [COLOR=navy]As[/COLOR] [COLOR=navy]Integer[/COLOR] [COLOR=navy]Dim[/COLOR] Temp [COLOR=navy]Set[/COLOR] rng = Range("A1").CurrentRegion [COLOR=navy]For[/COLOR] i = 1 To rng.Columns.Count [COLOR=navy]For[/COLOR] J = i To rng.Columns.Count [COLOR=navy]If[/COLOR] rng(J) < rng(i) [COLOR=navy]Then[/COLOR] Temp = rng.Columns(i).Value rng(i).Resize(rng.Rows.Count) = rng.Columns(J).Value rng(J).Resize(rng.Rows.Count) = Temp [COLOR=navy]End[/COLOR] If [COLOR=navy]Next[/COLOR] J [COLOR=navy]Next[/COLOR] i [COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
[COLOR="Navy"]Sub[/COLOR] MG26May36
[COLOR="Navy"]Dim[/COLOR] rng [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] i [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer[/COLOR]
[COLOR="Navy"]Dim[/COLOR] J [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Temp
[COLOR="Navy"]Dim[/COLOR] nams [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] F
[COLOR="Navy"]Dim[/COLOR] Dex [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer[/COLOR]
nams = Array("ItemID", "FirstName", "LastName", "Address", "Week", "Month", "Year")
[COLOR="Navy"]Set[/COLOR] rng = Range("A1").CurrentRegion
[COLOR="Navy"]For[/COLOR] i = 1 To rng.Columns.Count
[COLOR="Navy"]For[/COLOR] J = i To rng.Columns.Count
[COLOR="Navy"]For[/COLOR] F = 0 To UBound(nams)
[COLOR="Navy"]If[/COLOR] nams(F) = rng(J) [COLOR="Navy"]Then[/COLOR] Dex = F: [COLOR="Navy"]Exit[/COLOR] For
[COLOR="Navy"]Next[/COLOR] F
[COLOR="Navy"]If[/COLOR] F < i [COLOR="Navy"]Then[/COLOR]
Temp = rng.Columns(i).Value
rng(i).Resize(rng.Rows.Count) = rng.Columns(J).Value
rng(J).Resize(rng.Rows.Count) = Temp
[COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] J
[COLOR="Navy"]Next[/COLOR] i
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Try this:-
Make sure all your headers are in the array "Nams" (in their final order) and that your data is the extent of your CurrentRegion (No more Data-After )
Regards MickCode:[COLOR=navy]Sub[/COLOR] MG26May36 [COLOR=navy]Dim[/COLOR] rng [COLOR=navy]As[/COLOR] Range [COLOR=navy]Dim[/COLOR] i [COLOR=navy]As[/COLOR] [COLOR=navy]Integer[/COLOR] [COLOR=navy]Dim[/COLOR] J [COLOR=navy]As[/COLOR] [COLOR=navy]Integer[/COLOR] [COLOR=navy]Dim[/COLOR] Temp [COLOR=navy]Dim[/COLOR] nams [COLOR=navy]As[/COLOR] Variant [COLOR=navy]Dim[/COLOR] F [COLOR=navy]Dim[/COLOR] Dex [COLOR=navy]As[/COLOR] [COLOR=navy]Integer[/COLOR] nams = Array("ItemID", "FirstName", "LastName", "Address", "Week", "Month", "Year") [COLOR=navy]Set[/COLOR] rng = Range("A1").CurrentRegion [COLOR=navy]For[/COLOR] i = 1 To rng.Columns.Count [COLOR=navy]For[/COLOR] J = i To rng.Columns.Count [COLOR=navy]For[/COLOR] F = 0 To UBound(nams) [COLOR=navy]If[/COLOR] nams(F) = rng(J) [COLOR=navy]Then[/COLOR] Dex = F: [COLOR=navy]Exit[/COLOR] For [COLOR=navy]Next[/COLOR] F [COLOR=navy]If[/COLOR] F < i [COLOR=navy]Then[/COLOR] Temp = rng.Columns(i).Value rng(i).Resize(rng.Rows.Count) = rng.Columns(J).Value rng(J).Resize(rng.Rows.Count) = Temp [COLOR=navy]End[/COLOR] If [COLOR=navy]Next[/COLOR] J [COLOR=navy]Next[/COLOR] i [COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
Alternatively you can sort the worksheet from left to right using a custom list instead of A-Z.
Try this:-
Make sure all your headers are in the array "Nams" (in their final order) and that your data is the extent of your CurrentRegion (No more Data-After )
Regards MickCode:[COLOR=Navy]Sub[/COLOR] MG26May36 [COLOR=Navy]Dim[/COLOR] rng [COLOR=Navy]As[/COLOR] Range [COLOR=Navy]Dim[/COLOR] i [COLOR=Navy]As[/COLOR] [COLOR=Navy]Integer[/COLOR] [COLOR=Navy]Dim[/COLOR] J [COLOR=Navy]As[/COLOR] [COLOR=Navy]Integer[/COLOR] [COLOR=Navy]Dim[/COLOR] Temp [COLOR=Navy]Dim[/COLOR] nams [COLOR=Navy]As[/COLOR] Variant [COLOR=Navy]Dim[/COLOR] F [COLOR=Navy]Dim[/COLOR] Dex [COLOR=Navy]As[/COLOR] [COLOR=Navy]Integer[/COLOR] nams = Array("ItemID", "FirstName", "LastName", "Address", "Week", "Month", "Year") [COLOR=Navy]Set[/COLOR] rng = Range("A1").CurrentRegion [COLOR=Navy]For[/COLOR] i = 1 To rng.Columns.Count [COLOR=Navy]For[/COLOR] J = i To rng.Columns.Count [COLOR=Navy]For[/COLOR] F = 0 To UBound(nams) [COLOR=Navy]If[/COLOR] nams(F) = rng(J) [COLOR=Navy]Then[/COLOR] Dex = F: [COLOR=Navy]Exit[/COLOR] For [COLOR=Navy]Next[/COLOR] F [COLOR=Navy]If[/COLOR] F < i [COLOR=Navy]Then[/COLOR] Temp = rng.Columns(i).Value rng(i).Resize(rng.Rows.Count) = rng.Columns(J).Value rng(J).Resize(rng.Rows.Count) = Temp [COLOR=Navy]End[/COLOR] If [COLOR=Navy]Next[/COLOR] J [COLOR=Navy]Next[/COLOR] i [COLOR=Navy]End[/COLOR] [COLOR=Navy]Sub[/COLOR]