Reorder columns by name + Wildcard?

  • Hello all! I'm looking for some VBA code to help me put my columns in a specific order. Here is the catch I need to do it by the column's name (first row values), not by the letter. I have code that will work but I've ran into some difficulty. I'll try my best to explain, from the code below:


    "CAT", "DOG ", "MOUSE", "COMPUTER MONITOR"


    are my column names. I want them in that order, however the columns were pasted in from another program making the " MONITOR" with the space included in "COMPUTER MONITOR" unreadable in the code.


    Is there a way I can ignore " MONITOR" in the array and still reorder the column with the name "COMPUTER MONITOR". Also the same with DOG, can I ignore the spaces in front of it and still derive the "DOG " with the spaces? Sorry if this isn't making any sense. I think what I am looking for is a wildcard.






    All help is appreciated!

  • Re: Reorder columns by name + Wildcard?


    John


    I am not quite sure I've understood, so maybe one of:


    Code
    columnMatch = Application.Match("*" & orderedHeadings(i) & "*", Range("1:1"), 0)
        
    columnMatch = Application.Match(Left(orderedHeadings(i), 3) & "*", Range("1:1"), 0)
        
    columnMatch = Application.Match(Left("*" & orderedHeadings(i), 3) & "*", Range("1:1"), 0)


    I assume you will change the match strings to remove spaces.

  • Re: Reorder columns by name + Wildcard?


    Hey Dangle, thanks for the reply.
    I actually used a different method, I made a separate macro to just rename my columns and then called my rearrange function. It seemed to work just fine.


    But your help was much appreciated!

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!