1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

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

Discussion in 'Business Applications' started by lukasdylan, Jun 5, 2012.

Thread Status:
Not open for further replies.
Advertisement
  1. lukasdylan

    lukasdylan Thread Starter

    Joined:
    Jun 5, 2012
    Messages:
    3
    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, where each column is to be sorted independently of the others. Thank you in advance for your time.

    Code:
        Range("A2:AX21").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. Keebellah

    Keebellah Trusted Advisor

    Joined:
    Mar 27, 2008
    Messages:
    5,946
    Hi, Welcome to the forum,
    I did not entirely use your code.
    Sheet1 contains code and I added a module with code that I use now thanks to Zack for retrieving column letters

    I will sort columns A to AX independantly of each other.

    I just used two columns to test, A and D
     

    Attached Files:

  3. lukasdylan

    lukasdylan Thread Starter

    Joined:
    Jun 5, 2012
    Messages:
    3
    Hans:

    Thank you for taking the time to share this with me. The macro works on your file, but when I try to run it on my file it asks me to create a new macro. (I copied the macro from your file into the VB Editor of my file.) Any thoughts as to what I'm doing wrong? Thanks again.
     
  4. Keebellah

    Keebellah Trusted Advisor

    Joined:
    Mar 27, 2008
    Messages:
    5,946
    The macro either works, or doesn't work. It will never 'create' a new macro.
    Could you explain more, where you put the code, did you also copy the GetcolumnModule to your file? etc.etc
     
  5. lukasdylan

    lukasdylan Thread Starter

    Joined:
    Jun 5, 2012
    Messages:
    3
    I got it to work. It seemed to be an issue with file types. My data set was small, so I copied the whole thing into the file you created and everything worked great. Sorry for the confusion, but thanks again for helping me out with this. :)
     
  6. Keebellah

    Keebellah Trusted Advisor

    Joined:
    Mar 27, 2008
    Messages:
    5,946
    No problem, happy coding :)
     
  7. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1055912