+ Reply to Thread
Results 1 to 8 of 8

Using Sort function in macro crashes

  1. #1
    Registered User
    Join Date
    06-03-2009
    Location
    DFW
    MS-Off Ver
    Excel 2003
    Posts
    4

    Using Sort function in macro crashes

    I have a report I produce where I take an XML file, sort it and run some simple formulas then sort another way and run a few more formulas then update a pivot table. The XML files I'm using have the same headers but vary in length. When I set the macro up for a particular XML file everything works fine. When I import another one (different size) I get the error message:

    Run-time error ‘1004’:
    Sort method of Range class failed

    When I look at the code I see that the range is fixed to the XML file I used when setting the macro up. Is there a way to auto-select the table in the macro so I can run the same macro on any size XML?

    The code for the first sort looks like this:
    Please Login or Register  to view this content.
    Any help here would be greatly appreciated.
    Last edited by sfax39; 06-04-2009 at 03:49 PM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Using Sort function in macro crashes

    Welcome to the forum, SFA. There are several ways to do what you ask.

    Is your "table" really only column A as demonstrated in the macro? It appears you are sorting on multiple "keys" but only one column. That seems odd to me.

    What is the width of the table? Does that change? Is it the # of rows you want to dynamically select?

    Perhaps it would be easier for us to picture if you uploaded a couple of sample datasets showing the table in a couple of different forms...

    Click on GO ADVANCED and use the paperclip icon to upload some some sample data, point out the real keys you want to use in each and how they were identified. Perhaps even before/after examples.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    06-03-2009
    Location
    DFW
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Using Sort function in macro crashes

    Thank you.
    i know it looks like only column A is selected in the macro but if you look closer the range is actually "A1:AI76". it's hard to see the difference between the number 1 and the alpha character I. the width of the column stays the same, always ends with AI. only the number of rows changes. yes, i believe it's the number of rows i want dynamically selected.
    the code shows 76 rows. it can get as high as 7000 depending on the sales center. attached is an example.
    Attached Files Attached Files

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Using Sort function in macro crashes

    Let's try it this way:
    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 06-04-2009 at 03:46 PM.

  5. #5
    Registered User
    Join Date
    06-03-2009
    Location
    DFW
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Using Sort function in macro crashes

    That is fantastic! thank you!
    one more question:
    i'm finding the same problem with copying the formulas that i insert. for example, the code below describes the three formulas i insert. i type the formula in the first cell and double-click to auto fill the formula down to the last cell. you can see that repeated three times below.

    example:
    Selection.AutoFill Destination:=Range("F3:F4138")

    again it specifies a range. can i use the same piece of code you provided above to copy the formula in any size XML file? otherwise when i move from a large file to one with fewer rows the formula extends far below the new XML.

    Please Login or Register  to view this content.

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Using Sort function in macro crashes

    Like this, much quicker, no autofilling needed if you already know how many rows you want to insert into, do it all in one step:
    Please Login or Register  to view this content.
    The code in blue is another way to remove formulas, just re-insert the "values".

    Notice, we didn't select anything to do this work? Much faster.

  7. #7
    Registered User
    Join Date
    06-03-2009
    Location
    DFW
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Using Sort function in macro crashes

    Thanks again JBeaucaire. i believe i'm ready to publish this report. i appreciate you're help.

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Using Sort function in macro crashes

    If that takes care of your need, be sure to EDIT your original post, click Go Advanced and mark the PREFIX box [SOLVED].



    (Also, use the blue "scales" icon in our posts to leave Reputation Feedback, it is appreciated)

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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