+ Reply to Thread
Results 1 to 5 of 5

VBA Sort when Column Order Changes

  1. #1
    Registered User
    Join Date
    10-18-2012
    Location
    Washington
    MS-Off Ver
    Excel 2007
    Posts
    3

    VBA Sort when Column Order Changes

    Challenge!

    I have a 4-item sort that relies on my fields being in a static order. I want the ability to rearrange my columns without having to revise my code, so I need the Range.Sort to look up the column location for each of my fields. Are you clever enough to figure this one out? Is the key a dynamic lookup based on column name?

    Assume header row 1 is as follows:
    C = Work Order
    E = Group
    AN = Due Date
    V = Site

    Please Login or Register  to view this content.
    To throw a wrench in things, my actual column headings have two carriage returns after each field name so "Work Order"&Char(10)&Char(10) in code terms.

    Many Thanks!

  2. #2
    Forum Expert
    Join Date
    01-25-2011
    Location
    Belgium, Alveringem
    MS-Off Ver
    Excel 2003, 2007, 365
    Posts
    1,418

    re: VBA Sort when Column Order Changes

    4 columns to sort ?
    Please Login or Register  to view this content.
    Remember, saying thanks only takes a second or two. Click the little star * below, to give some Rep if you think an answer deserves it.

  3. #3
    Registered User
    Join Date
    10-18-2012
    Location
    Washington
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: VBA Sort when Column Order Changes

    Thanks for the reply, bsalv! I gave this a try after changing "MySheet" to the correct sheet name and changing Range to my named range that begins in A1 (I also tried leaving it as A1). In all cases I keep getting the Else clause Msgbox. I narrowed it down one item using language like this for each K item 1-4.

    Please Login or Register  to view this content.
    In each case, I get the "not ok" message. I also tried removing the char(10) and changing my "Site" & Chr(10) & Chr(10) to just "Site" in box code and in Row 1 of my worksheet. I tried to look up Application.Index but couldn't find anything excep Application.WorksheetFunction.Index however when I revised to that I got a run time error 13 type mismatch message. Are we trying to count text with the IsNumeric If statement? Where should I go from here?

  4. #4
    Forum Expert
    Join Date
    01-25-2011
    Location
    Belgium, Alveringem
    MS-Off Ver
    Excel 2003, 2007, 365
    Posts
    1,418

    Re: VBA Sort when Column Order Changes

    can you post an example of your file, so that's easier for me to see how your "Site..." is exactly.
    Let's say the header and 5 lines of (dummy) data.

  5. #5
    Registered User
    Join Date
    10-18-2012
    Location
    Washington
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: VBA Sort when Column Order Changes

    Example Dynamic Column Sort VBA.xlsm

    Here is a sample spreadsheet where I copied the header row directly from my original file. Thanks!

+ 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. [SOLVED] Sort worksheets into same order as column
    By exarranum in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-21-2013, 10:45 AM
  2. Replies: 1
    Last Post: 05-18-2012, 11:14 PM
  3. Replies: 15
    Last Post: 04-16-2011, 04:01 PM
  4. sort column in date order dd/mm/yy
    By K in forum Excel General
    Replies: 4
    Last Post: 04-13-2005, 04:06 PM

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