Results 1 to 3 of 3

Change current VBA code to sort differently

Threaded View

  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.

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