Results 1 to 3 of 3

VBA to copy data to a template file then save it as a new file.

Threaded View

  1. #3
    Registered User
    Join Date
    08-24-2015
    Location
    Emsworth, England
    MS-Off Ver
    2019
    Posts
    27

    Question Re: VBA to copy data to a template file then save it as a new file.

    I have adapted this brilliant code, I do however have a question: is there away to get a hyperlink created within the following code. I have attempted it in Range G3 but to no avail.
    Sub CreateERFForms()
    
    Dim xlBook As Workbook
    Dim xlNewBook As Workbook
    Dim xlSheet As Worksheet
    Dim LastRow As Long
    Dim i As Long
    
    Const strPath As String = "C:\Users\Donald\Documents\Excel Files\Data\" 'Change value to new location
    Const strFileB As String = "C:\Users\Donald\Documents\Excel Files\Template.xlsx" 'Change value to new location
        Set xlBook = ActiveWorkbook
        Set xlSheet = ActiveSheet
        Application.DisplayAlerts = False
        Application.Calculation = xlCalculationManual
        Application.ScreenUpdating = False
        
        LastRow = xlSheet.Range("A" & xlSheet.Rows.Count).End(xlUp).Row
        For i = 2 To LastRow
            Set xlNewBook = Workbooks.Add(Template:=strFileB)
            With xlNewBook.Sheets(1)
                .Range("A3") = xlSheet.Range("A" & i) 'Change values to destination cell
                .Range("B3") = xlSheet.Range("B" & i) 'Change values to destination cell
                .Range("C3") = xlSheet.Range("C" & i) 'Change values to destination cell
                .Range("D3") = xlSheet.Range("D" & i) 'Change values to destination cell
                .Range("E3") = xlSheet.Range("E" & i) 'Change values to destination cell
                .Range("F3") = xlSheet.Range("F" & i) 'Change values to destination cell
                .Range("G3") = xlSheet.Range("H" & i).Hyperlinks.Add Anchor:=("G3"), Address:=("H" & i), TextToDisplay:=("G" & i)
                .Range("C6") = xlSheet.Range("K2") 'Set to single value
                .Range("C5") = xlSheet.Range("L2") 'Set to single value
            End With
            xlNewBook.SaveAs strPath & Format(Now, "yyyymmdd") & "-ID-" & CStr(xlSheet.Range("I" & i)) & "-" & CStr(xlSheet.Range("A" & i)) & "-O.xlsx" 'Naming convention
            xlNewBook.Close 0
            Set xlNewBook = Nothing
        Next i
        Set xlBook = Nothing
        Set xlSheet = Nothing
    Application.ScreenUpdating = True
    End Sub
    Hoping somebody can help.
    Last edited by CDexel2011; 12-20-2019 at 12:14 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 09-05-2013, 02:39 PM
  2. [SOLVED] Copy row from active file to another formated excell file and save as one cell value
    By snsuvarna in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-16-2013, 04:18 AM
  3. Replies: 5
    Last Post: 05-08-2013, 06:23 AM
  4. [SOLVED] Copy two page from doc file and save in new doc file with excel creteria.
    By visha_1984 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-25-2013, 08:21 AM
  5. How to: Open file, format data, save file, close file and repeat.
    By thexeber in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-11-2010, 12:56 PM

Tags for this Thread

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