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
 
your sort key1 is not defined anywhere in the code !!

Range("Date of work")

your need to tell the macro what date of work is OR where it resides, similar to what you did with combo !!
 
Upvote 0

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
How would I do that, Date of work refers to the first column in my tables?
 
Upvote 0
OK, but I'm guessing that is the heder of that column.
A normal sort key would be something like
Code:
Sort Key1:=Range("A2")
Try srting by the first date in that column instead.
Using named ranges is great but can make it almost impossible for someone else to modify or debug.
You really would be better of using actual ranges rather than names until the code works THEN start substituting Named Range names !
 
Upvote 0
Michael, I think when the OP is referring to a "Table" they do mean a Table i.e. what we used to call a ListTable so they are selecting the range then doing insert - table.

Unfortunately I can't remember the syntax we used to use in the old pre 2007 style of coding for a sort using a ListTable.
 
Last edited:
Upvote 0
Michael, I think when the OP is referring to a "Table" they do mean a Table i.e. what we used to call a ListTable so they are selecting the range then doing insert - table.

Unfortunately I can't remember the syntax we used to use in the old pre 2007 style of coding for a sort using a ListTable.

That's it, I am using a table from selecting the data then choosing insert, table or pressing ctrl t.
 
Upvote 0
Is there a problem with using a table? I have just read that you should always use a table when dealing with data in excel.
 
Upvote 0
Is there a problem with using a table?

Not as such, it is personal preference. They have advantages (they are especially useful if you are dealing with people who don't know Excel well) so some prefer them but others don't.

I fall in the 2nd category.

The syntax for coding I find annoying which is probably why I don't use them much (I also wish people wouldn't make statements like "should always use a table" rather than let people judge for themselves what is best in their situation as it is just an opinion :biggrin:).
 
Last edited:
Upvote 0
Maybe I will try getting rid of the tables and use that sorting code and see how that goes. Thanks
 
Upvote 0
I converted it back to not use tables and now I was going to use the xldown method to select the dates to sort. I have this code for that:
Code:
worksheets(combo).Range("A4").End(xlDown)

What code do I need to add to sort the range considering the range has headings in A3:D3, so the data doesn't start until row 4.
 
Upvote 0
Maybe (untested)
Code:
Worksheets(combo).Range("A4:D" & Worksheets(combo).Range("A4").End(xlDown).Row).Sort key1:=Worksheets(combo).Range("A4"), order1:=xlAscending, Header:=xlNo
 
Upvote 0

Forum statistics

Threads
1,215,987
Messages
6,128,125
Members
449,425
Latest member
NurseRich

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