+ Reply to Thread
Results 1 to 3 of 3

Change current VBA code to sort differently

Hybrid View

theexcelguy31 Change current VBA code to... 09-28-2023, 10:07 AM
ranman256 Re: Change current VBA code... 09-29-2023, 10:05 AM
Artik Re: Change current VBA code... 10-08-2023, 12:33 PM
  1. #1
    Registered User
    Join Date
    09-28-2023
    Location
    St Louis, Missouri
    MS-Off Ver
    Office 16
    Posts
    1

    Exclamation Change current VBA code to sort differently

    I have written the current VBA code below to sort each column alphabetically. I need to alter this code so that it sorts in the following order: 1. Alphabetically 2. Numerically 3. By Symbols

    The current VBA code sorts by 1. Numbers 2. Symbols. and then alphabetically.

    The current code is as follows:
    
    Sub SortColumnsAlphabetically()
        Dim ws As Worksheet
        Dim rng As Range
        Dim lastRow As Long
        Dim lastCol As Long
        Dim col As Long
        
        ' Define the worksheet (change "Data List" to your actual sheet name)
        Set ws = ThisWorkbook.Sheets("Data List")
        
        ' Define the range (A3:E27 in this case)
        Set rng = ws.Range("A3:E27")
        
        ' Find the last row and last column in the range
        lastRow = rng.Rows.Count + rng.Row - 1
        lastCol = rng.Columns.Count + rng.Column - 1
        
        ' Loop through each column and sort it alphabetically
        For col = rng.Column To lastCol
            ws.Range(ws.Cells(rng.Row, col), ws.Cells(lastRow, col)).Sort _
                Key1:=ws.Cells(rng.Row, col), _
                Order1:=xlAscending, _
                Header:=xlNo
        Next col
    End Sub
    Attached Images Attached Images
    Last edited by davesexcel; 09-28-2023 at 10:25 AM.

  2. #2
    Valued Forum Contributor ranman256's Avatar
    Join Date
    07-29-2012
    Location
    Kentucky
    MS-Off Ver
    Excel 2003
    Posts
    1,192

    Re: Change current VBA code to sort differently

    did you try :
    record a macro,
    run your new sort,
    stop macro.

    now you have a new sort macro.

  3. #3
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,543

    Re: Change current VBA code to sort differently

    I was hoping that a Custom List could be used for sorting. Unfortunately, I was disappointed with the sorting behavior using the list.
    Below is a somewhat wonky macro that should sort the data in each column in the order you expect.
    Sub SortColumnsAlphabetically()
        Dim ws As Worksheet
        Dim rng As Range
        Dim lastRow As Long
        Dim lastCol As Long
        Dim col As Long
        
        Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual
        Application.EnableEvents = False
        
        ' Define the worksheet (change "Data List" to your actual sheet name)
        Set ws = ThisWorkbook.Sheets("Data List")
        
        ' Define the range (A3:E27 in this case)
        Set rng = ws.Range("A3:E27")
        
        ' Find the last row and last column in the range
        lastRow = rng.Rows.Count + rng.Row - 1
        lastCol = rng.Columns.Count + rng.Column - 1
        
        ' Loop through each column and sort it alphabetically
        For col = rng.Column To lastCol
            ws.Range(ws.Cells(rng.Row, col), ws.Cells(lastRow, col)).Sort _
                Key1:=ws.Cells(rng.Row, col), _
                Order1:=xlAscending, _
                Header:=xlNo
            
            Call SortCustom(ws.Range(ws.Cells(rng.Row, col), ws.Cells(lastRow, col)))
        Next col
        
        Application.Calculation = xlCalculationAutomatic
        Application.EnableEvents = True
        
    End Sub
    
    
    Sub SortCustom(rng As Range)
        Dim NumCount As Long
        Dim SymbCount As Long
        Dim AlphaCount As Long
        
        Dim v As Variant
        Dim vSymbols As Variant
        Dim vAlpha As Variant
        Dim vNum As Variant
        
        Dim i As Long
        
        v = rng.Value
        
        'how many numbers in range
        NumCount = Application.Count(v)
        
        'look for position of last symbol
        For i = NumCount + 1 To UBound(v)
          If Not IsEmpty(v(i, 1)) Then
            If Asc(LCase(v(i, 1))) >= 97 Then
                i = i - 1
                Exit For
            End If
          End If
        Next i
        
        'how many symbols
        SymbCount = Application.Min(i, UBound(v)) - NumCount
        'how many alpha
        AlphaCount = UBound(v) - NumCount - SymbCount
        
        'remove empty elements (if any) from the arrays vNum, vSymbols, vAlpha
        If NumCount > 0 Then
            vNum = rng(1).Resize(NumCount)
            
            For i = UBound(vNum) To 1 Step -1
                If Not IsEmpty(vNum(i, 1)) Then
                    Exit For
                End If
            Next i
            
            If i > 0 Then
                vNum = rng(1).Resize(i)
            End If
        End If
        
        
        If SymbCount > 0 Then
            vSymbols = rng(NumCount + 1).Resize(SymbCount)
            
            For i = UBound(vSymbols) To 1 Step -1
                If Not IsEmpty(vSymbols(i, 1)) Then
                    Exit For
                End If
            Next i
            
            If i > 0 Then
                vSymbols = rng(NumCount + 1).Resize(i)
            End If
        End If
        
        
        If AlphaCount > 0 Then
            vAlpha = rng(NumCount + SymbCount + 1).Resize(AlphaCount)
            
            For i = UBound(vAlpha) To 1 Step -1
                If Not IsEmpty(vAlpha(i, 1)) Then
                    Exit For
                End If
            Next i
            
            If i > 0 Then
                vAlpha = rng(NumCount + SymbCount + 1).Resize(i)
            End If
        End If
        
        'recalculate the count of each character group
        On Error Resume Next
        NumCount = 0
        NumCount = UBound(vNum)
        SymbCount = 0
        SymbCount = UBound(vSymbols)
        AlphaCount = 0
        AlphaCount = UBound(vAlpha)
        
        'insert data into the range in order: Alpha, Numeric, Symbols
        rng(1).Resize(AlphaCount).Value = vAlpha
        rng(AlphaCount + 1).Resize(NumCount).Value = vNum
        rng(AlphaCount + NumCount + 1).Resize(SymbCount).Value = vSymbols
        On Error GoTo 0
        
    End Sub
    Artik

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to change code to print differently
    By SKEEEETER in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-25-2020, 10:05 PM
  2. [SOLVED] Vba code to loop A:A and change value in current row
    By Grahamfeeley in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-17-2018, 02:27 AM
  3. need to sort three sheets differently in same workbook
    By sagikerius in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 10-23-2017, 01:51 PM
  4. [SOLVED] How can I modify my current code to not change once populated
    By Dena in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-05-2013, 01:52 PM
  5. Change code to look for current worksheet?
    By dalewms2 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-08-2011, 03:35 PM
  6. Excel 2007 code to sort certain cells depending on current active cell
    By mpalm in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-26-2011, 05:00 PM
  7. [SOLVED] excel change default column sort to current selection
    By john palmer in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-08-2005, 12:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1