+ Reply to Thread
Results 1 to 10 of 10

exporting data from master file to template file (autopopulate a template)

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-04-2010
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    469

    exporting data from master file to template file (autopopulate a template)

    Hi all,

    I'm hoping someone can help me with some code to export from a database to a template which would be manageable to modify and expand on for an amateur level vba user.

    I've attached 2 test files, one is the database master file containing the projects (each row represents a project, unique reference number in column A) and the other is the blank template file i'm hoping to export data into and then save down with the naming convention "column A_column B.xlsx"

    Kept the test files simple but would need to modify any code to apply to much larger database consisting of many more fields etc.

    Thanks
    Neill
    Attached Files Attached Files
    Last edited by Gti182; 05-06-2014 at 11:27 AM.

  2. #2
    Forum Contributor
    Join Date
    10-04-2010
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    469

    Re: exporting data from master file to template file (autopopulate a template)

    this should help, i suppose i'd need to bring in a loop too

    master.Range("A" & Row) = template.Range("B1")  'Reference no.
    master.Range("B" & Row) = template.Range("B2")  'Title
    master.Range("C" & Row) = template.Range("B4")  'Confidentiality
    master.Range("D" & Row) = template.Range("B3")  'Name
    master.Range("E" & Row) = template.Range("B6")  'Department
    master.Range("F" & Row) = template.Range("B8")  'Priority
    master.Range("G" & Row) = template.Range("B7")  'Building
    master.Range("H" & Row) = template.Range("B10") 'Current Stage
    master.Range("I" & Row) = template.Range("C11") 'Stage 1
    master.Range("J" & Row) = template.Range("C12") 'Stage 2
    master.Range("K" & Row) = template.Range("C13") 'Stage 3
    master.Range("L" & Row) = template.Range("C14") 'Stage 4
    master.Range("M" & Row) = template.Range("B16") 'Value

  3. #3
    Forum Contributor
    Join Date
    03-29-2012
    Location
    Canada
    MS-Off Ver
    2007
    Posts
    818

    Re: exporting data from master file to template file (autopopulate a template)

    How do you need to populate your Template?

    On double click? with a userform search? I mean you have 4 rows of information in your Master and only space for one project in your template. Do you need to save every row from Master to Template

    or

    Save individual rows from Master to save in accordance to Column A_B (Copy one row from Master then create new workbook, then add the information in accordance, then save as?)

  4. #4
    Forum Contributor
    Join Date
    10-04-2010
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    469

    Re: exporting data from master file to template file (autopopulate a template)

    Hi Excelnoub

    Yes each row/project would populate a template so for this example 4 templates would be populated.

    Ideally i'd like the templates to be populated all at once rather than individually as there can be anything up to 300+ projects within the master file to export
    Last edited by Gti182; 05-06-2014 at 08:02 AM.

  5. #5
    Forum Contributor
    Join Date
    10-04-2010
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    469

    Re: exporting data from master file to template file (autopopulate a template)

    bump back up to page 1

  6. #6
    Forum Contributor
    Join Date
    03-29-2012
    Location
    Canada
    MS-Off Ver
    2007
    Posts
    818

    Re: exporting data from master file to template file (autopopulate a template)

    Give me a couple, I have a similar code that does the same thing.

    It creates a Folder and then a workbook, just trying to delete the folder part. For the transfer I also have a similar code that will create and paste the information, save then close.
    The issue, out of my scope, will be looping through rows and creating each rows vs a new workbook.

  7. #7
    Forum Contributor
    Join Date
    10-04-2010
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    469

    Re: exporting data from master file to template file (autopopulate a template)

    That would be great, i may have some loop code i could try integrate

  8. #8
    Forum Contributor
    Join Date
    03-29-2012
    Location
    Canada
    MS-Off Ver
    2007
    Posts
    818

    Re: exporting data from master file to template file (autopopulate a template)

    First, add the following to a module in your Master workbook: this will need tweeking but you will get the idea

    This code was provided by shg

    Function MakeDir(ByVal sDir As String) As Long
    
    ' Returns -1 if sDir already exists
    '     1 if sDir is successfully created
    '     0 if sDir cannot be created
    
    Dim sPS         As String
    Dim astr()      As String
    Dim iLvl        As Long
    
    If Len(Dir(sDir, vbDirectory)) Then
    MakeDir = -1
    
    Else
    sPS = Application.PathSeparator
    
    If Right(sDir, 1) = sPS Then sDir = Left(sDir, Len(sDir) - 1)
    astr = Split(sDir, sPS)
    sDir = vbNullString
    
    ' MkDir will fail if sDir contains character codes 0-31
    ' or any of the characters ' < > : " / \ | ? *
    ' or if the drive does not exist,
    ' or if the user does not have permissions, so ...
    On Error GoTo Oops
    
    For iLvl = 0 To UBound(astr)
    sDir = sDir & astr(iLvl) & sPS
    If Len(Dir(sDir, vbDirectory)) = 0 Then MkDir sDir
    Next iLvl
    
    MakeDir = 1
    End If
    OuttaHere:
    Exit Function
     
    Oops:
    Err.Clear
    Resume OuttaHere
    End Function

  9. #9
    Forum Contributor
    Join Date
    10-04-2010
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    469

    Re: exporting data from master file to template file (autopopulate a template)

    wow Excelnoub, have no idea how i'd tweak that to fit my scenario

    I did manage to find a mixture of code and tweak it for this example and seems to work as required

    Sub Create()
    
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Application.DisplayAlerts = False
    Application.AskToUpdateLinks = False
    
    Dim AWB As Workbook 'Active Workbook
    Dim NWB As Workbook 'New Workbook
    Dim AWS As Worksheet 'Active Worksheet
    Dim LastRow As Long
    Dim i As Long
    
    Const Path As String = "C:\Users\User\Desktop\"
    Const TPath As String = "C:\Users\User\Desktop\Template.xlsx"
        
        Set AWB = ActiveWorkbook
        Set AWS = AWB.Sheets(1)
        
        LastRow = AWS.Range("A" & Rows.Count).End(xlUp).Row
        
        For i = 2 To LastRow
        
            Set NWB = Workbooks.Add(Template:=TPath)
            With NWB.Sheets(1)
            
    Range("B1") = AWS.Range("A" & i)  'Reference no.
    Range("B2") = AWS.Range("B" & i)  'Title
    Range("B4") = AWS.Range("C" & i)  'Confidentiality
    Range("B3") = AWS.Range("D" & i)  'Name
    Range("B6") = AWS.Range("E" & i)  'Department
    Range("B8") = AWS.Range("F" & i)  'Priority
    Range("B7") = AWS.Range("G" & i)  'Building
    Range("B10") = AWS.Range("H" & i)  'Current Stage
    Range("C11") = AWS.Range("I" & i)  'Stage 1
    Range("C12") = AWS.Range("J" & i)  'Stage 2
    Range("C13") = AWS.Range("K" & i)  'Stage 3
    Range("C14") = AWS.Range("L" & i)  'Stage 4
    Range("B16") = AWS.Range("M" & i)  'Value
    
            End With
            NWB.SaveAs Path & CStr(AWS.Range("A" & i) & "_" & AWS.Range("B" & i)) & ".xlsx"
            
                    NWB.Close 0
            Set NWB = Nothing
            
        Next i
        Set AWB = Nothing
        Set AWS = Nothing
        
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    Application.AskToUpdateLinks = True
    
    End Sub

  10. #10
    Forum Contributor
    Join Date
    03-29-2012
    Location
    Canada
    MS-Off Ver
    2007
    Posts
    818

    Re: exporting data from master file to template file (autopopulate a template)

    Nice... I was making a way longer code then expected with Matching / Find. I am at work so limited time frame to play around with it...


+ 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 extract a tab from one file, add it to a master template file and then save as
    By SweetSorcery in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-03-2014, 11:36 AM
  2. Replies: 2
    Last Post: 02-14-2014, 01:39 PM
  3. [SOLVED] Exporting data from master file to pre-populate many excel files from a template
    By Gti182 in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 08-07-2013, 07:52 AM
  4. Replies: 0
    Last Post: 06-24-2013, 11:27 AM
  5. Replies: 3
    Last Post: 05-15-2013, 11:34 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