Hi,
I have a working VBA-code for reordering columns alphabetically.
Only problem: the sheet is so big that it takes over 20 minutes to complete.
Recently I had a similar problem, which (thanks to macleanb and JackDanIce) could be solved by "assigning the value2 property of the usedrange to an array, iterating around the array and then putting in back once all replacements are done. "
Link: http://www.mrexcel.com/forum/excel-questions/894230-replace-blanks-big-sheet-fastest-way.html
I have tried to use this feedback in the code, but can't get it right.
The code that I have now (see underneath) actually does nothing anymore (also no error message)
Hopefully somebody can help me to integrate the array-part in the code above correctly.
I have a working VBA-code for reordering columns alphabetically.
Only problem: the sheet is so big that it takes over 20 minutes to complete.
Code:
[COLOR=Navy]Sub[/COLOR] OrderColumnsAZ()
[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]
Recently I had a similar problem, which (thanks to macleanb and JackDanIce) could be solved by "assigning the value2 property of the usedrange to an array, iterating around the array and then putting in back once all replacements are done. "
Link: http://www.mrexcel.com/forum/excel-questions/894230-replace-blanks-big-sheet-fastest-way.html
I have tried to use this feedback in the code, but can't get it right.
The code that I have now (see underneath) actually does nothing anymore (also no error message)
Hopefully somebody can help me to integrate the array-part in the code above correctly.
Code:
Sub OrderColumnsAZ()
Dim rng As Range
Dim i As Integer
Dim J As Integer
Dim Temp
Dim arr() As Variant
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
With Sheets("NEW")
[COLOR=Navy]Set[/COLOR] rng = Range("A1").CurrentRegion
arr = .Range(.Cells(1, 1), rng).Value2
For i = 1 To rng.Columns.Count
For J = i To rng.Columns.Count
If rng(J) < rng(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
.Cells(1, 1).Resize(UBound(arr, 1), UBound(arr, 2)).Value = arr
End With
Erase arr
Set rng = Nothing
With Application
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
End With
End Sub
Last edited: