VBA pulling data from a dataset to generate many templates pre-filled

Fiddling4033

New Member
Joined
Jul 20, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hey all, it's my first time properly using VBA and at a bit of a loss on how to conceptualize this task. Basically I have an excel workbook with a large dataset on one sheet, containing unique names by Row and 10 columns with 'responses' provided by the names.
On the same workbook I have a template file that I want to generate a unique excel file for each name in the dataset, prefilling the 10 columns into the template. (Though it will be transposing the data from one row and 10 columns into one column and 10 rows).

VBA Code:
Sub generateSMEResponses()
Dim wb As Workbook, sh1 As Worksheet, lr As Long, rng As Range

'Variables
Set sh1 = Sheets("data") 'dataset
Set sh2 = Sheets("Instructions") 'Copy over
Set sh3 = Sheets("2022 Questions")'Copy over
Set Sh4 = Sheets("Additional Info") 'Copy over

lr = sh1.Cells(Rows.Count, "A").End(xlUp).Row
Set rng = Sheets("data").Range("A2:A" & lr)
    For Each c In rng
        sh2.Copy 
        sh3.Copy After:=Sheets("Instructions")
        Sh4.Copy After:=Sheets("2022 Questions")
        sh1.Copy After:=Sheets("Additional Info")
        Set wb = ActiveWorkbook
        wb.Sheets(1).Range("B9") = c.Value
        

        Table1 = wb.Sheets(2).Range("C11:C23") ' Range of column headers for code to lookup and identify in dataset
        Table2 = wb.Sheets(4).Range("A2:E") ' Range of names and 10 response columns
        TempRow = wb.Sheets(2).Range("F11").Row
        TempClm = wb.Sheets(2).Range("F11").Column 'Column to begin inputting identified data from above
        For Each cl In Table1
            Set tempValue = Application.WorksheetFunction.VLookup(c.Value, Table2, 2, False)
            'TempRow = TempRow + 1
        Next cl
        

        wb.SaveAs c.Value & ".xlsx"
        wb.Close False
        
    Next
End Sub

So far, the code works in copying my needed sheets, and placing the c.Value in B9. Without the loop, it also saves and generates the excel files as needed. My only struggle is working out how to actually lookup the data I need relative to each name, and input it back onto the correct sheet. Is a VLookup the correct method here?
For reference I receive errors at the VLookup line stating "Run time error 1004: Unable to get the VLookup property".

Many thanks in advance :)
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
I also couldn't work out how to lookup the data from the workbook running the macro - So my workaround was to copy the dataset onto each new workbook, have the lookup take the necessary info, then delete the dataset sheet before saving and closing.
 
Upvote 0

Forum statistics

Threads
1,214,790
Messages
6,121,608
Members
449,038
Latest member
apwr

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top