Sorting a table using vba code

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,352
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I am trying to sort a table by the date column with this code:

Code:
    'declare variables
    Dim combo As String                                                     'Combo worksheet name
    
    'assign values to variables
    combo = Worksheets("Home").Range("Q5")                                  'string in cell Q5 of Home worksheet
    
    Range(combo).CurrentRegion.Sortkey1:=Range("Date of work"), order1:=xlAscending, Header:=xlYes

Combo has the relevant sheet name stored within it and the table goes up column Q. Can anyone tell me what I have done wrong with this code please as it gives me a syntax error and highlights the range(combo) line of code?

Thanks,
Dave
 
What box ???
Did you apply this line of code in your last macro

Code:
Worksheets(combo).Range("A1").Select
 
Upvote 0

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
That seems to partially work but half way through the procedure, it throws up an error saying object doesn't support this property or method. This is the line of code that is highlighted:

Code:
    ActiveWorkbook.Worksheets(Combo).Sort.SortFields.Add2 Key:=Range( _
        "A4:A1000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
 
Upvote 0
It worked previously, what else have you changed ???
 
Upvote 0
I don't think it did work previously. I have only just decided to add the sort procedure in. The copy procedure worked, but not the sort procedure.
 
Last edited:
Upvote 0
Can you see anything wrong with this code as when I comment the sort sub out, it works, except for the sorting?

Code:
Sub cmdSort()
'
   'Sorting procedure
'
    Dim Combo As String
        Combo = Worksheets("Home").Range("Q5")
        
        
        
        
    Range("A3:D1920").Select
        
    Worksheets(Combo).Sort.SortFields.Clear
    Worksheets(Combo).Sort.SortFields.Add2 Key:=Range( _
        "A4:A1000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With Worksheets(Combo).Sort
        .SetRange Range("A3:D1000")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

Application.CutCopyMode = False

End Sub
 
Upvote 0
I think I have found what is going on. With this code
Code:
   Sheets("August 2018").Sort key1:=Range("A4"), _
      order1:=xlAscending, Header:=xlYes
it sorts the August 2018 sheet but when i try this
Code:
   Sheets(Combo).Sort key1:=Range("A4"), _
      order1:=xlAscending, Header:=xlYes

I am told named argument not found, even though I have dimmed combo as a variable to match the sheet name.


I made a macro to try and get it working
Code:
Sub SortDates()
'
' sortDates Macro
'

  'Set up your variables and turn off screen updating.
   Dim iCounter As Integer
   Dim Combo As String
        Combo = Worksheets("Home").Range("Q5")
   Application.ScreenUpdating = False
   
  
   'Sort the rows based on the data in column C

   Sheets(Combo).Sort key1:=Range("A4"), _
      order1:=xlAscending, Header:=xlYes
   
   'Clear out the temporary sorting value in column C, and turn screen updating back on.
   'Columns(3).ClearContents
   Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
the Last box was the code I got the code snippets above it.
 
Upvote 0
Use

Code:
Sub cmdSort()
'
   'Sorting procedure
'
    Dim Combo As String
        Combo = Worksheets("Home").Range("Q5")
    'Range("A3:D1920").Select
    With Worksheets(Combo)
        .Sort.SortFields.Clear
        .Sort.SortFields.Add Key:=Range("A4:A1000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With Worksheets(Combo).Sort
        .SetRange Range("A3:D1000")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End With
Application.CutCopyMode = False

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,982
Messages
6,128,104
Members
449,421
Latest member
AussieHobbo

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