+ Reply to Thread
Results 1 to 6 of 6

Rename sheet to match name of an imported file

  1. #1
    Registered User
    Join Date
    05-06-2010
    Location
    Nowheresville, USA
    MS-Off Ver
    Excel 2003
    Posts
    3

    Rename sheet to match name of an imported file

    Hello, I have what I hope is an easy problem to solve. I have large quantities of .csv files that I want to import into excel. Each file will be imported to a different worksheet within the same workbook. I have written a macro that will mass import these files.

    Here's the problem:

    I would like for each new sheet that is created to be renamed to match the name of the incoming file. For example, if I import files named "Jan2010.csv", "Feb2010.csv", and "Mar2010.csv" each to different worksheets, I would like for the sheets/tabs to be renamed "Jan2010", "Feb2010", and "Mar2010" respectively.

    Thanks in advance for any advice. I've been beating my head against the keyboard for months now.
    Last edited by Jackets; 05-06-2010 at 01:36 PM.

  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,259

    Re: Need help renaming sheet to match name of an imported file

    Hello Jackets,

    Welcome to the Forum!

    It would help to see the macro code. If you could post it, it will be easier and faster to answer your question. Please use the code tags (See below my signature) when posting the code. It makes it easier to read and copy.
    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
    05-06-2010
    Location
    Nowheresville, USA
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Need help renaming sheet to match name of an imported file

    Sure...here it is. Apologies in advance for the misc stuff in there. I'm very new to this so I've learned by using the "record macro" feature and am not sure what parts are neccessary and what parts aren't. Advice appreciated.

    Please Login or Register  to view this content.

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

    Re: Need help renaming sheet to match name of an imported file

    Hello Jackets,

    This should work. The macro uses Regular Expressions to parse out the file name without the extension. The sheet is then named using this string.
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    05-06-2010
    Location
    Nowheresville, USA
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Rename sheet to match name of an imported file

    Great! That seems to work with one minor correction

    The end of the WITH statement should be:

    ActiveSheet.Name = RegExp.Replace(fname(X), "$2")

    and not

    ActiveSheet.Name = RegExp.Replace(fname(1), "$2")


    Here is the code that is now working for me:

    Please Login or Register  to view this content.
    I'm not exactly sure how you did it but it is working and that meets my need. Thank you! Mark this one as solved!
    Last edited by Jackets; 05-06-2010 at 01:36 PM.

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

    Re: Rename sheet to match name of an imported file

    Hello Jackets,

    Oops! Forgot to change that after I finished testing the parsing. Glad you caught that, thanks.

+ 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