Sort Cell Columns VBA

ststern45

Well-known Member
Joined
Sep 17, 2005
Messages
974
Office Version
  1. 365
  2. 2010
Platform
  1. 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

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:

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi ststern45,

what about

VBA Code:
Sub SortColumns_mod()
' https://www.mrexcel.com/board/threads/sort-cell-columns-vba.1227161/
Dim lngLRow As Long
Dim ws As Worksheet

Application.ScreenUpdating = False

Set ws = ActiveWorkbook.Worksheets("History1")

lngLRow = ws.Cells(ws.Rows.Count, 2).End(xlUp).Row
With ws.Sort
  .SortFields.Clear
  .SortFields.Add Key:=ws.Range("B1:B" & lngLRow), _
                  SortOn:=xlSortOnValues, _
                  Order:=xlAscending, _
                  DataOption:=xlSortNormal
  .SetRange ws.Range("B1:D" & lngLRow)
  .Header = xlYes
  .MatchCase = False
  .Orientation = xlTopToBottom
  .SortMethod = xlPinYin
  .Apply
End With
Set ws = Nothing

Application.ScreenUpdating = True

End Sub

Ciao,
Holger
 
Upvote 0
Thank you the reply and the code. I will try this and will definitely reply.
Thanks,
Steve
 
Upvote 0
Thank you again for the code.
Everything works great except the code skips over cell B1:D1
Everything sorts perfectly with the exception of these 2 cells.
Thanks
Steve
 
Upvote 0
Hi Steve,

the code has the codeline

VBA Code:
  .Header = xlYes

which excludes the first row of the area from sorting, If you know that there will be no headers to stay on top of the data you may either use

VBA Code:
  .Header = xlGuess

where Excel will decide if Row 1 will be included in the sorting or use

VBA Code:
  .Header = xlNo

to advise to sort the complete area no matter if there are headers or not.

Holger
 
Upvote 0
@ststern45
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time.
 
Upvote 0
Hi Steve,

the code has the codeline

VBA Code:
  .Header = xlYes

which excludes the first row of the area from sorting, If you know that there will be no headers to stay on top of the data you may either use

VBA Code:
  .Header = xlGuess

where Excel will decide if Row 1 will be included in the sorting or use

VBA Code:
  .Header = xlNo

to advise to sort the complete area no matter if there are headers or not.

Holger
Thank you for all your help.
Steve
 
Upvote 0
Hi Steve,

the code has the codeline

VBA Code:
  .Header = xlYes

which excludes the first row of the area from sorting, If you know that there will be no headers to stay on top of the data you may either use

VBA Code:
  .Header = xlGuess

where Excel will decide if Row 1 will be included in the sorting or use

VBA Code:
  .Header = xlNo

to advise to sort the complete area no matter if there are headers or not.

Holger
Thank you again for all your help.
.Header = xlGuess works perfectly

Thank you,
Steve
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top