+ Reply to Thread
Results 1 to 8 of 8

Macro based on header search

  1. #1
    Registered User
    Join Date
    10-13-2011
    Location
    OKC
    MS-Off Ver
    Excel 2007
    Posts
    5

    Smile Macro based on header search

    I need a macro to run that will save Zip and Zip+4 in a certain format (00000 and 0000 respectively). The issue is that I have about 8 different files this macro needs to run on and the Zip and Zip+4 field is not in the same location every time. I know how to do the macro to run the formatting, I now need a macro that will search every cell in the first row and when it finds ZIP will format as needed (00000) and when it finds ZIP+4 will format as needed (0000). In addition to this, the field does not have common naming, sometimes it will simply be ZIP or Zip+4 but other times it will be CompanyZip and CompanyZip+4 so the search needs to search the cell contents for a non exact match. It also only needs to search the first row, nothing else.

    It would be good (not needed) if we could also not apply the macro if the column is not populated past the first row. Some projects have multiple fields that will hold zip but not all projects will have that info populated.

    This code will be part of existing code that is used to save each file as pipe (|) delimited so it does not need to be stand alone code.

    Any help is greatly appreciated!! Feel free to ask any questions needed.
    Last edited by cardinalsfan0510; 10-13-2011 at 03:22 PM. Reason: Solved!! Thanks Leith!!

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Macro based on header search

    Hello cardinalsfan0510,

    Welcome to the Forum!

    This macro will open all ".xls" files in the specified folder and check each worksheet's first row for Zip and Zip+4 in the cells values. If there are not at least two rows on the worksheet, the macro will skip the worksheet. You can add this macro to your existing code by copying the code and pasting it into a separate VBA module. You will then need to call this macro from the existing code when needed.

    You will need to change the file path to where your files are located. Currently, the macro searchs for ".xls" workbook files. You can change the extension if needed. Change the variable Ext in the code to the extension you need. Remember to include the period before the extension.

    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    10-13-2011
    Location
    OKC
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Macro based on header search

    Thanks Leith!! The files will already be open when I run the macro. The macro saves each file as a pipe delimited file after we have done what we needed to do. We call the macro up manually and run it ourselves. Let me post the code I already have so it can be modified. I just need to change the middle part that right now does formatting based on the location of the cells in this particular project.

    Please Login or Register  to view this content.

    Quote Originally Posted by Leith Ross View Post
    Hello cardinalsfan0510,

    Welcome to the Forum!

    This macro will open all ".xls" files in the specified folder and check each worksheet's first row for Zip and Zip+4 in the cells values. If there are not at least two rows on the worksheet, the macro will skip the worksheet. You can add this macro to your existing code by copying the code and pasting it into a separate VBA module. You will then need to call this macro from the existing code when needed.

    You will need to change the file path to where your files are located. Currently, the macro searchs for ".xls" workbook files. You can change the extension if needed. Change the variable Ext in the code to the extension you need. Remember to include the period before the extension.

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    10-13-2011
    Location
    OKC
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Macro based on header search

    I took your code and modified it and added it to my code but can't get it to work. It goes through the save process (pick location and filename, choose delimiter character) but doesn't save the file or change the format for the cells. Here is what I have:

    Please Login or Register  to view this content.

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Macro based on header search

    Hello cardinalsfan0510,

    Are these converted files still Excel workbooks or text files?

  6. #6
    Registered User
    Join Date
    10-13-2011
    Location
    OKC
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Macro based on header search

    Quote Originally Posted by Leith Ross View Post
    Hello cardinalsfan0510,

    Are these converted files still Excel workbooks or text files?
    The files we open to modify are text files, pipe delimited. We then QA the data there and make any changes needed and run the macro shown previously to save it as a pipe delimited file again.

    But when we edit the data and run the macro, it is open as a workbook.

    EDIT: Here is what I have now. I think it is close. I got rid of a few things and simplified others. I made it only look at the open sheet (will only have one sheet open at a time) and changed the range to check only the first row as far as I need it to go. It will run without errors but will not save the file and the formats for the columns in question do not change.

    Again, thanks for the help. I am sure i am just missing something small. I can upload a copy of the file I am testing with if that will help.

    Please Login or Register  to view this content.
    Last edited by cardinalsfan0510; 10-13-2011 at 02:59 PM.

  7. #7
    Registered User
    Join Date
    10-13-2011
    Location
    OKC
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Macro based on header search

    I got it!! I didn't need the loop in there and finally figured it out. Posting my final code below in case this helps someone else. Thanks for the code that got me going Leith!!

    Please Login or Register  to view this content.

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Macro based on header search

    Hello cardinalsfan0510,

    Well done! It is much more satisfying when you solve a problem yourself and you learn more in the process. Glad I could help.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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