VBA / Macro - Sort by ROW - Specific Requirement

ExcelFabs

New Member
Joined
Oct 9, 2018
Messages
9
Dear Community,

I have a daily report which is currently manually sorted. I would like to do this automated with a macro if possible.

The "Row Headers" look something like this:
302(USD)304(EUR)305(USD)307(USD)308(USD)310(GBP)311(USD)342(EUR)368(EUR) ....

<tbody>
</tbody>
It can be any number between 300 - 599, followed by 3 currencies, USD, GBP, EUR in brackets. This changes everyday depending on the report.

We manually sort by Ascending Number + (USD), then (GBP), and lastly (EUR).
This is done by checking the header in Row 1, apply above condition, and cut-paste column to the correct Spot manually.
In the example above, i would manually cut out column with header 304(EUR) and paste it before column with header 342(EUR).

Is there a way to automate this with a Macro?

I tried using Custom Lists without success. Appreciate any help or guidance in this matter!


Let me know if anything is unclear or you need more information.

Thanks so much in advance,
Kind regards
ExcelFabs
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
OK, give this a try in a copy of your workbook.

Code:
Sub Rearrange_Columns()
  Dim AL As Object
  Dim itm As Variant
  Dim c As Range
  Dim Pref As String
  
  Set AL = CreateObject("System.Collections.ArrayList")
  For Each c In Range("A1", Cells(1, Columns.Count).End(xlToLeft))
    Select Case Right(c.Value, 5)
      Case "(USD)": Pref = "A"
      Case "(GBP)": Pref = "B"
      Case "(EUR)": Pref = "C"
    End Select
    AL.Add Pref & c.Value
  Next c
  AL.Sort
  AL.Reverse
  Application.ScreenUpdating = False
  For Each itm In AL
    Rows(1).Find(What:=Mid(itm, 2), LookAt:=xlWhole).EntireColumn.Cut
    Columns("A").Insert
  Next itm
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks so much Peter! This works perfectly :)

I only changed two things, to start at Column "C" as i had two fixed columns before. Perfect!


Thanks again, this is a GREAT help!
 
Upvote 0
Thanks so much Peter! This works perfectly :)

I only changed two things, to start at Column "C" as i had two fixed columns before. Perfect!


Thanks again, this is a GREAT help!
I did woder if it was all columns or not. :)
Anyway, glad you could modify it yourself to do what was required. :)
 
Upvote 0
I did woder if it was all columns or not. :)
Anyway, glad you could modify it yourself to do what was required. :)

While i unfortunately can't follow the whole code logic, at least i could figure out somewhat which part is doing what :) So i was able to amend those parts to fit.

Thanks again!
 
Upvote 0

Forum statistics

Threads
1,215,459
Messages
6,124,945
Members
449,198
Latest member
MhammadishaqKhan

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