+ Reply to Thread
Results 1 to 8 of 8

Sorting Macro Replaces Column Headers

  1. #1
    Valued Forum Contributor
    Join Date
    10-10-2012
    Location
    New York
    MS-Off Ver
    Excel 2007/2010
    Posts
    337

    Sorting Macro Replaces Column Headers

    I'm having an issue where I have a macro (see attached file) that is supposed to auto-fill and sort. The problem I'm having is when it tries to run the auto-fill on a sheet in its blank/template state. It will take my formulas in columns AC:AZ and fill them up to the header row.

    Please Login or Register  to view this content.


    This code was hobbled together using record macro and my minor understanding of coding. Please let me know if you need further explanation.

    (CK) Macro issue.xlsm
    Don't just use the answers provided for you. Try to understand how it works by reverse engineering or asking about it.

    Please mark the thread as [SOLVED] (Thread Tools->Mark thread as Solved) when answered.
    If you're happy with an answer given, please click the * under the person's name to boost their reputation.

  2. #2
    Valued Forum Contributor
    Join Date
    12-02-2012
    Location
    Melbourne, VIC
    MS-Off Ver
    Excel 2016
    Posts
    750

    Lightbulb Re: Sorting Macro Replaces Column Headers

    I believe this is happening so because when the Macro runs on a blank slate (i.e. with just headers) your last row value evaluates to 3; which is this part of the code
    Please Login or Register  to view this content.
    ; and the Auto-fill then applies to "AC4:AZ3" i.e. Row 4 auto-fills row 3.
    To avoid this, you may declare an Interger/Long variable to capture the value of the lastrow; say
    Please Login or Register  to view this content.
    . Also, change auto-fill range to "AC4:AZ" & iLastRow.

  3. #3
    Valued Forum Contributor
    Join Date
    10-10-2012
    Location
    New York
    MS-Off Ver
    Excel 2007/2010
    Posts
    337

    Re: Sorting Macro Replaces Column Headers

    I did what you said and altered my code (in red) to be

    Please Login or Register  to view this content.
    I'm getting an issue where it's stopping on

    Please Login or Register  to view this content.
    What am I doing wrong?

  4. #4
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Sorting Macro Replaces Column Headers

    Did you do what jewelsharma told you i.e. add a dim statment for "iLastRow"

    Please Login or Register  to view this content.
    Dim as integer works for 32732 row as far as I remember more than that you need to dim it as Long.

    Alf

  5. #5
    Valued Forum Contributor
    Join Date
    10-10-2012
    Location
    New York
    MS-Off Ver
    Excel 2007/2010
    Posts
    337

    Re: Sorting Macro Replaces Column Headers

    ALF/Jewel,

    That seems to be working. I had never made a Dim statement before. But now I have another issue that I probably should have stated in the OP.

    My full code (see below)incorporates 4 separate tabs. When I run the code it sets iLastRow to the number of rows in my Q1 sheet only and keeps the value for all the other sheets. I know that a variable can be reset but I don't know how to do it for each individual tab.

    Please Login or Register  to view this content.
    Thanks for your help.

  6. #6
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Sorting Macro Replaces Column Headers

    The Dim statement is only added once by preference at the start of a macro.

    Then you just add the "iLastRow" statement to each sheet you select i.e. for sheet Q2

    Please Login or Register  to view this content.
    and similar for the Q3 Q4 sheets

    An other alternative could be to a "For loop" if you run the same code on every sheet Q1 to Q4

    Something along these lines.

    Please Login or Register  to view this content.
    As j goes from 1 to 4 sheet Q1 gets activated and code run on sheet Q1.

    The command "Next" changes j to 2 an sheet Q2 is then activated and so fort.

    Alf

  7. #7
    Valued Forum Contributor
    Join Date
    10-10-2012
    Location
    New York
    MS-Off Ver
    Excel 2007/2010
    Posts
    337

    Re: Sorting Macro Replaces Column Headers

    Thanks for the solution(s) ALF. I kept getting an error that said Run-time error '1004': AutoFill method of Range class failed on the line
    Please Login or Register  to view this content.
    I had to do some digging around and found that the AutoFill function doesn't work if its autofill range is the same as it's source range (in this case my source range was "AC4:AZ4"). I reworked the For Loop you suggested (So much simpler!) and made it read like this

    Please Login or Register  to view this content.
    It seems to be working for my needs. I never would have gotten to this point without your help.

  8. #8
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Sorting Macro Replaces Column Headers

    Glad to be of help, thanks for feed back and rep.

    Alf

+ 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. Sorting data by column headers
    By mj_bowen in forum Excel General
    Replies: 0
    Last Post: 06-17-2012, 11:22 AM
  2. active column headers for sorting
    By thetrystero in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-28-2008, 04:12 AM
  3. sorting a column into headers
    By Boulderguy in forum Excel General
    Replies: 3
    Last Post: 03-25-2008, 11:10 PM
  4. [SOLVED] Sorting by Column Headers
    By Richard in forum Excel General
    Replies: 2
    Last Post: 06-19-2006, 07:00 PM
  5. [SOLVED] how to protect column headers from sorting.
    By rabbit in forum Excel General
    Replies: 3
    Last Post: 08-13-2005, 07:45 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