+ Reply to Thread
Results 1 to 6 of 6

How to execute a macro across a specific number of columns

Hybrid View

  1. #1
    Registered User
    Join Date
    06-05-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    2

    How to execute a macro across a specific number of columns

    Hello,

    I am trying to learn rules in altering a macro that has been recorded. I need the following code to execute across columns A-AX. Thank you in advance for your time.

    Range("A2:A21").Select
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A2"), _
            SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Sheet1").Sort
            .SetRange Range("A2:A21")
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End Sub

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: How to execute a macro across a specific number of columns

    Hi Lucasdylan,

    Try: Range("A2:AX21")
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Registered User
    Join Date
    06-05-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: How to execute a macro across a specific number of columns

    Hello xladept:

    Thank you for the response. The change you suggested works, but I need the columns to be sorted independently. Right now it seems they are all being sorted on the numbers in column A. Any thoughts? Thanks again.

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: How to execute a macro across a specific number of columns

    Hi lukasdylan,

    Maybe:

    Sub SortSingleColumns(): Dim i As Integer
    For i = 1 To 50
        Range(Cells(2, i), Cells(21, i)).Select
            ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
            ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A2"), _
                SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
                    With ActiveWorkbook.Worksheets("Sheet1").Sort
                    .SetRange Range(Cells(2, i), Cells(21, i)).Select
                    .Header = xlGuess
                    .MatchCase = False
                    .Orientation = xlTopToBottom
                    .SortMethod = xlPinYin
                    .Apply
                    End With
    Next i: End Sub

  5. #5
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: How to execute a macro across a specific number of columns

    Hi lukasdylan,

    Get rid of that select at the end of the set - sorry.

  6. #6
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: How to execute a macro across a specific number of columns

    Alternate version, using Range.Sort instead of Worksheet.Sort:
    Sub tgr()
        
        Dim i As Long
        For i = Columns("A").Column To Columns("AX").Column
            With Range(Cells(2, i), Cells(Rows.Count, i).End(xlUp))
                .Sort .Cells, xlAscending, Header:=xlGuess
            End With
        Next i
        
    End Sub
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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