ststern45
Well-known Member
- Joined
- Sep 17, 2005
- Messages
- 974
- Office Version
- 365
- 2010
- Platform
- Windows
Hello,
I am having trouble sorting cell columns B:D
Column B and D are the only columns with data
Column B is the column I am sorting in ascending order with column D
This is the code I have so far: Thanks in advance
This is the code using macro recorder:
I am having trouble sorting cell columns B:D
Column B and D are the only columns with data
Column B is the column I am sorting in ascending order with column D
This is the code I have so far: Thanks in advance
VBA Code:
Sub SortColumns()
Dim aRng As String
Dim bRng As String
Application.ScreenUpdating = False
'aRng = cell column B
'bRng = cell column D
'G1 = starting row cell always 1
'G2 = this row chnages based on the new number of entries
aRng = Sheets("History1").Range("F1").Value
bRng = Sheets("History1").Range("F2").Value
Columns("B:D").Select
ActiveWorkbook.Worksheets("History1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("History1").Sort.SortFields.Add Key:= _
Range(aRng & Range("G1").Value & ":" & bRng & Range("G2").Value), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
:=xlSortNormal
With ActiveWorkbook.Worksheets("History1").Sort
.SetRange Range(aRng & Range("G1").Value & ":" & bRng & Range("G2").Value)
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Application.ScreenUpdating = True
End Sub
This is the code using macro recorder:
VBA Code:
Columns("B:D").Select
ActiveWorkbook.Worksheets("History1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("History1").Sort.SortFields.Add Key:= _
Range("B1:B10579"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
:=xlSortNormal
With ActiveWorkbook.Worksheets("History1").Sort
.SetRange Range("B1:D10579")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Last edited by a moderator: