+ Reply to Thread
Results 1 to 4 of 4

Macro to Create Folder and Run Script

  1. #1
    Registered User
    Join Date
    12-29-2014
    Location
    Indianapolis, IN
    MS-Off Ver
    MS Office 2007
    Posts
    2

    Macro to Create Folder and Run Script

    Hello,

    I am attempting to create a macro that will do a few things.

    1. I would like it to create a folder based on the cell "A40", which is just a cell with the date (New folder would be 12/29/2014)
    2. I would then like it to run the script to fill in values on the sheet (Letters) from another worksheet (Information) and export them individually as PDF's
    3. Save the PDF's in the new folder (Destination would become P:\Early Retirees\2015\12-29-2014\name Letter.pdf)

    I had the fill-in macro working just fine, but for some reason, I am unable to figure out exactly where the folder creation should come into play in the macro scripting. This is what I have and it is currently not giving me an error, however, it's not creating the file or the folder. Could someone possibly help me?

    Thank you,

    Brian

    Sub Letter()
    Dim fileName As String
    With Sheets("Letter")
    If (.Range("A40") = vbNullString) Or (.Range("A40") = vbNullString) Then Exit Sub

    On Error Resume Next
    MkDir "P:\Early Retirees\2015\" & .Range("A40")
    ChDir "P:\Early Retirees\2015\" & .Range("A40")
    On Error GoTo 0

    Application.ScreenUpdating = False
    For Count = 8 To Worksheets("Information").Range("A65536").End(xlUp).Row Step 1

    'Name
    Worksheets("Letter").Range("A10").Value = Worksheets("Information").Cells(Count, 17).Value
    'Medical
    Worksheets("Letter").Range("C13").Value = Worksheets("Information").Cells(Count, 12).Value
    'Dental
    Worksheets("Letter").Range("C14").Value = Worksheets("Information").Cells(Count, 13).Value
    'Vision
    Worksheets("Letter").Range("C15").Value = Worksheets("Information").Cells(Count, 14).Value

    Name = Worksheets("Information").Cells(Count, 16).Text
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, fileName:="P:\Early Retirees\2015\Merged Files\" & Name & " " & "Letter" & ".pdf" _
    , Quality:=xlQualityMedium, IncludeDocProperties:=False, _
    IgnorePrintAreas:=False, OpenAfterPublish:=False

    Next Count
    End Sub

  2. #2
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Macro to Create Folder and Run Script

    Welcome to the forum!

    Please use code tags when posting code. Easy way is to click the # icon in a reply to add them.

    First off, do not use command names for variable names. Think of Name as the old Rename command word if you know DOS.

    Format() is used to format a date value into a valid string for a subfolder name.

    Your code assumes that the parent folders already exist. You can check file and folder existence with a Dir().

    Prior to running code, be sure to do a Compile. I add that to my VBE toolbar.

    Keep pressing F8 in a Sub to debug line by line. Once execute, hover mouse over variable to see the value.

    Anyway, here is a stab at your code.

    Please Login or Register  to view this content.

  3. #3
    Forum Contributor
    Join Date
    12-29-2014
    Location
    Indonesia
    MS-Off Ver
    Office 2010, 2013
    Posts
    125

    Re: Macro to Create Folder and Run Script

    hi all, allow me to see this content. thank you

  4. #4
    Registered User
    Join Date
    12-29-2014
    Location
    Indianapolis, IN
    MS-Off Ver
    MS Office 2007
    Posts
    2

    Re: Macro to Create Folder and Run Script

    This worked absolutely perfectly.

    Thank you so much for your help, Kenneth.

+ 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. Macro to create a folder and then save all files in that folder
    By gokzee in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-27-2013, 01:49 PM
  2. macro script to search and hyperlink image file from folder
    By shamiul in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-15-2013, 12:09 PM
  3. Macro to Create Folder\Sub Folder\SubFolder\
    By coolhit in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-30-2012, 12:43 PM
  4. Replies: 13
    Last Post: 06-04-2012, 02:18 PM
  5. Replies: 3
    Last Post: 07-20-2006, 01:15 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