Sorting Data with VBA

moliver

New Member
Joined
Aug 7, 2008
Messages
8
Office Version
  1. 2013
Platform
  1. Windows
Hi Mr Excel Community,

I am trying to sort data using VBA.
The idea is to create a Macro for different users. The problem is that each users gets the same data but the columns order is different.
So the first step is to arrange the data in the same way for different users.
I would like to pass the data to an array then move array columns? is that possible? or create an array and start passing the data based on a fixed columns criteria?
I am sure that there are different ways, just trying to understand which is the best.

I was wondering if anyone has some code or have seen anything that could help me/guide in this process.
Thanks
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

CephasOz

Board Regular
Joined
Feb 18, 2020
Messages
206
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
The following code might be helpful if:
1) You have column headings in row 1
2) The data rows and columns are not separated by blank rows or columns, but all together as a block.

To use it, copy it and paste it into the worksheet to be sorted. Then, when a user wants to sort the worksheet, all they have to do is to click on the title in row 1. The order or names of the columns don't matter.
VBA Code:
' Selection Change
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Const cstrTitle As String = "Column Sort"
    Dim dblLastRow As Double
    Dim dblLastCol As Double
    Dim vbmStyle As VbMsgBoxStyle
    Dim strMsg As String
    '
    If (Target.Cells.Count = 1) Then
        If (Target.Row = 1) Then
            dblLastRow = Me.Cells(1, 1).CurrentRegion.Rows.CountLarge
            If (dblLastRow > 2) Then
                dblLastCol = Me.Cells(1, 1).CurrentRegion.Columns.CountLarge
                If (Target.Column <= dblLastCol) Then
                    vbmStyle = vbDefaultButton2 + vbQuestion + vbYesNo
                    strMsg = "Sort by '" & Target.Value & "'?"
                    If (MsgBox(strMsg, vbmStyle, cstrTitle) = vbYes) Then
                        Me.Cells(1, 1).CurrentRegion.Sort _
                            Header:=xlYes, _
                            Key1:=Me.Columns(Target.Column), _
                            Order1:=xlAscending
                    End If
                End If
            End If
        End If
    End If
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,129,361
Messages
5,635,812
Members
416,884
Latest member
leeshjay

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
Top