+ Reply to Thread
Results 1 to 2 of 2

VBA to open the Folder that contains the previous month folder

Hybrid View

  1. #1
    Registered User
    Join Date
    02-17-2010
    Location
    Brisbane Australia
    MS-Off Ver
    Excel 2016
    Posts
    45

    VBA to open the Folder that contains the previous month folder

    HI all ,

    I am trying to build a macro that does the following:

    1. Look for the folder named after the current year
    2. In that folder look for the Folder that contains the previous month number and name ("4. April")
    3. Create a folder in there called "Stakeholder files" and save another macro manipulated Excel file in there.

    This is what I have so far. I know is not much, still some work to do.

    Sub Open_PreviousMonth_WB()
    
        Dim strMonthNumber As Integer, strPath As String, strFolder As String, strMonth As String, strFname As String
    
          strPath = "\Users\PMD\Desktop\MP\Report automation"
    
         strMonth = Format(DateAdd("m", -1, Date), "mmmm")
    
        strMonthNumber = Month(currDate) - 1
    
        strFolder = strPath & strMonthNumber & strMonth
    
        'strFname = Format(Date - 1, "mmmmmmm") & ".xls"
    
        ChDrive "C"
    
        ChDir strFolder
    
        Workbooks.Open Filename:=(strFolder & strFname)
    
     End Sub
    Your help would be greatly appreciated.

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,516

    Re: VBA to open the Folder that contains the previous month folder

    Hi there,

    See if the following code does what you need:

    
    
    Sub CreateFolder()
    
        Const sSOURCE_FOLDER    As String = "C:\Users\PMD\Desktop\MP\Report automation"
        Const sNEW_FOLDER       As String = "Stakeholder files"
    
        Dim dteLastMonth        As Date
        Dim sMonthFolder        As String
        Dim sYearFolder         As String
        Dim sFullPath           As String
    
        dteLastMonth = DateValue("1/" & Month(Date) & "/" & Year(Date)) - 1
    
        sYearFolder = Format(dteLastMonth, "yyyy")
    
        sMonthFolder = Format(dteLastMonth, "m. mmmm")
    
        sFullPath = sSOURCE_FOLDER & "\" & sYearFolder & "\" & sMonthFolder
    
    '   Check that the monthly folder exists before trying to create the Stakeholder Files folder
        If Dir$(sFullPath, vbDirectory) = sMonthFolder Then
            MkDir sFullPath & "\" & sNEW_FOLDER
        End If
    
    End Sub
    The highlighted values can be altered to suit your own requirements.


    Hope this helps - please let me know how you get on.

    Regards,

    Greg M

+ 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] Excel VBA code for selecting a folder then search & open specified files in that folder
    By Excelrookie_1 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-27-2021, 03:09 AM
  2. Look in previous month folder if -2 business day is previosu month
    By jamesmullens in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-18-2018, 12:10 AM
  3. [SOLVED] Creating a VBA Script to save in a current month's folder (create folder as needed)
    By spittingfire in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-15-2015, 03:08 PM
  4. Open Folder (Prompt User for final folder name in location path)
    By synergeticink in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-21-2014, 03:33 PM
  5. Macros Open folder, copy, paste and close the folder
    By hahahahahahaha in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-10-2013, 05:03 PM
  6. [SOLVED] Need to open all files of a certain name using wildcard for folder for month
    By BillDoor in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-09-2013, 03:30 AM
  7. Need to open all files of a certain name using wildcard for folder for month
    By BillDoor in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-08-2013, 05:56 AM

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