+ Reply to Thread
Results 1 to 2 of 2

Change Directory Based on Worksheet Name

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Change Directory Based on Worksheet Name

    I have a large number of worksheets in a workbook and a macro that will save each sheet individually to a folder of the worksheet name. The worksheet names range from AAXXX to AZXXX (where XXX is alphanumeric). In my I use the two following lines in the code to specify the directory and then the relevant folder within that directory to save the worksheets to, however I now need to save the worksheets to different subfolders based on the worksheet name.

    mDir = "C:\Client Jobs\Test\"
    
    ActiveWorkbook.SaveAs FileName:=mDir & wsName & "\" & "Report\Report (" & wsName & ")" & ".xlsx"
    So instread of having a load of folders inside C:\Client Jobs\Test\, named as per the worksheet name, I now have:

    C:\Client Jobs\Test\North
    C:\Client Jobs\Test\South
    C:\Client Jobs\Test\East
    C:\Client Jobs\Test\West

    and folders inside each as follows:

    North
    AAXXX to AJXXX

    South
    AKXXX to AQXXX

    East
    ARXXX to ARXXX

    West
    ATXXX to AZXXX

    How would I adapt the folder and file structure so that the relevant worksheets are saved in the correct folders, along the lines of

    IF (wsName) = AAXXX to AJXXX then mDir = C:\Client Jobs\Test\North

    ElseIf (wsName) = AKXXX to AQXXX then mDir = C:\Client Jobs\Test\South

    and so on.

    Many thanks
    Thanks,
    HangMan

    You can say "Thank you!" by clicking Add Reputation below the post.
    Please, mark your thread [SOLVED] if you are happy with the solution.

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,288

    Re: Change Directory Based on Worksheet Name

    You could use code like this, based on the second letter of the name

        Select Case Mid(UCase(wsName), 2, 1)
            Case "A" To "J": mDir = "C:\Client Jobs\Test\North"
            Case "K" To "Q":  mDir = "C:\Client Jobs\Test\South"
            Case "R": mDir = "C:\Client Jobs\Test\East"
            Case Else: mDir = "C:\Client Jobs\Test\West"
        End Select
    Last edited by Bernie Deitrick; 11-04-2015 at 05:56 PM.
    Bernie Deitrick
    Excel MVP 2000-2010

+ 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. Replies: 5
    Last Post: 01-26-2013, 01:55 PM
  2. [SOLVED] Change Autoshape Color in one worksheet based on cell value in a different worksheet
    By Vigny Mathew in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-03-2012, 11:00 AM
  3. Trying to move worksheet to folder in a directory based on conditional formatting
    By dwhite30518 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-12-2012, 02:24 PM
  4. save active worksheet in directory called in another worksheet cell
    By chi11 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-12-2012, 06:15 PM
  5. Change on another worksheet based on a drop-down
    By yunesm in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-12-2011, 10:58 AM
  6. Change worksheet name based on prompt
    By thedon_1 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-04-2011, 05:41 AM
  7. Change Worksheet name based on B5 and N5
    By esmith972 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-16-2009, 05:41 PM
  8. [SOLVED] Change Directory Help!
    By smonczka in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-19-2005, 01:05 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