Forum

Sort Macro Multiple...
 
Notifications
Clear all

Sort Macro Multiple Columns

17 Posts
3 Users
0 Reactions
343 Views
(@mymalone)
Posts: 103
Estimable Member
Topic starter
 

Hi

Can you help me with adjusting my macro?  My goal is to create a macro that can sort multiple columns together.  Meaning, if there a name in column 2 but should be in column 1 because  it is an alphabetical listing.  Would like for it to view three columns and place in the appropriate column.

 

Thank you

 
Posted : 05/05/2018 12:28 pm
(@sunnykow)
Posts: 1417
Noble Member
 

There is no attachment

 
Posted : 05/05/2018 8:42 pm
(@mymalone)
Posts: 103
Estimable Member
Topic starter
 

Sorry.  Here is the file.

 

Thanks

 
Posted : 06/05/2018 12:11 pm
(@sunnykow)
Posts: 1417
Noble Member
 

Hi Melinda

I don't quite understand your question. Is it sort by columns or rows?

If you can provide us the expected result from your attachment, it will help us understand better.

Sunny

 
Posted : 06/05/2018 12:33 pm
(@mymalone)
Posts: 103
Estimable Member
Topic starter
 

After all the names are entered in columns B, D, and F.  I would like for it to look at all 3 columns and sort them all together.  A comprehensive sort where all the names are alphabetical order.  It would wrap to the next column.  For example, once the names have been entered in Column B row 70, then the next name in alpha order would be placed in Column D, row 6.  And  once the names have been entered in Column D, row 70, it would place the next alpha name in Column F, row 6.  

I hope this helps.

 
Posted : 07/05/2018 6:10 am
(@sunnykow)
Posts: 1417
Noble Member
 

Hi Melinda

Give this a try.

I created a temp sheet with the names, sorted them and write back to the worksheet.

The temp sheet is then deleted.

Hope this helps.

Sunny

 
Posted : 07/05/2018 10:36 pm
(@mymalone)
Posts: 103
Estimable Member
Topic starter
 

Sunny

It works perfectly as requested.  However, I did not factor in what happens when I enter the date.  Is there a way to adjust the macro?

Thanks

 
Posted : 07/05/2018 11:21 pm
(@sunnykow)
Posts: 1417
Noble Member
 

Hi Melinda

Hope you don't mind that I hard-coded the ranges.

Cheers

 
Posted : 08/05/2018 12:19 am
(@mymalone)
Posts: 103
Estimable Member
Topic starter
 

Sunny

Thank you so very much!!  I appreciate all of your wealth of knowledge... 

 
Posted : 08/05/2018 11:23 am
(@sunnykow)
Posts: 1417
Noble Member
 

Hi Melinda

Thanks for your feedback.

Sunny

 
Posted : 08/05/2018 8:48 pm
(@mymalone)
Posts: 103
Estimable Member
Topic starter
 

Sunny

Can you "walk me through" or explain something to me?  For example, how did you know what to put the ts.Range as?.  If I wanted to change the  ws.Range("B6:C50").Copy for "D6:E50" and "F6:G50" would the ts.Range stay the same as below? 

 

ws.Range("B6:C70").Copy ts.Range("A1")
    ws.Range("D6:E70").Copy ts.Range("A66")
    ws.Range("F6:G70").Copy ts.Range("A131") 

 

 

Sub SortColumns()
    Dim ws As Worksheet
    Dim ts As Worksheet
   
    Application.ScreenUpdating = False
    Set ws = Sheets("Sheet1")
   
    'Create temp sheet
    Sheets.Add.Name = "MyTemp"
    Set ts = Sheets("MyTemp")
   
    'Copy data to temp sheet
    ws.Range("B6:C70").Copy ts.Range("A1")
    ws.Range("D6:E70").Copy ts.Range("A66")
    ws.Range("F6:G70").Copy ts.Range("A131")
   
    'Sort data
    ActiveSheet.Range("A:B").Sort Key1:=Range("A1"), _
    Order1:=xlAscending, Header:=xlNo
   
    'Copy sorted data back to sheet
    Range("A1:B65").Copy ws.Range("B6")
    Range("A66:B130").Copy ws.Range("D6")
    Range("A131:B195").Copy ws.Range("F6")

    'Delete temp sheet
    Application.DisplayAlerts = False
    ts.Delete
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub

 
Posted : 26/06/2018 2:03 pm
(@sunnykow)
Posts: 1417
Noble Member
 

Hi Melinda

What I am doing is to copy your data (65 rows) below one another into a temporary worksheet, sort them and copy them back to the original worksheet.

So ts.Range("A1") will start at row 1 to row 65. The next row will start from ts.Range("A66") at the 66th row etc.

If you have 45 rows, then you will need to change all the row numbers to an increment of 45 (instead of 65)

When you copy back, you will need to adjust the range accordingly e.g. Range("A1:B45").Copy ws.Range("B6") etc

I had hard-coded the ranges as I have no idea what your actual data looks like.

I hope I have explain it clearly.

Sunny

 
Posted : 26/06/2018 9:11 pm
(@mymalone)
Posts: 103
Estimable Member
Topic starter
 

Sunny,  thank you for taking the time to explain.  I have a better understanding of the macro.  

Much appreciated!

 
Posted : 26/06/2018 9:51 pm
(@mymalone)
Posts: 103
Estimable Member
Topic starter
 

Sunny

The macro was working perfectly.  Today I am receiving error message "Run time error 1004" Method 'Add'...

Highlighted in yellow:   Sheets.Add.Name = "MyTemp"
                                 Set ts = Sheets("MyTemp")

Do you know how to fix it?

Sub SortColumns()
    Dim ws As Worksheet
    Dim ts As Worksheet
   
    Application.ScreenUpdating = False
    Set ws = Sheets("Sheet1")
   
    'Create temp sheet
    Sheets.Add.Name = "MyTemp"
    Set ts = Sheets("MyTemp")
   
    'Copy data to temp sheet
    ws.Range("B6:C70").Copy ts.Range("A1")
    ws.Range("D6:E70").Copy ts.Range("A66")
    ws.Range("F6:G70").Copy ts.Range("A131")
   
    'Sort data
    ActiveSheet.Range("A:B").Sort Key1:=Range("A1"), _
    Order1:=xlAscending, Header:=xlNo
   
    'Copy sorted data back to sheet
    Range("A1:B65").Copy ws.Range("B6")
    Range("A66:B130").Copy ws.Range("D6")
    Range("A131:B195").Copy ws.Range("F6")

    'Delete temp sheet
    Application.DisplayAlerts = False
    ts.Delete
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub

 
Posted : 16/01/2019 1:03 pm
(@catalinb)
Posts: 1937
Member Admin
 

I can see 2 reasons:

-the sheet might already exist

-the workbook structure might have been protected, that means no sheet can be added or deleted. Check the workbook structure protection.

 
Posted : 16/01/2019 1:46 pm
Page 1 / 2
Share: