VBA Code to copy and paste data to a form via a loop

Barry NP

New Member
Joined
Jul 18, 2017
Messages
24
Hi.
I need some VBA help to copy data from an excel worksheet to a preformatted excel form, the, save the form to my C drive with the code looped to repeat this sequence for each line of data to be copied.

I have the data set in an excel sheet called "DATA" (below) and the form in same excel workbook in the tab called "Form" (bottom table)

Data sheet - Data starts in Cell B6 (row 5 are headers)
DATA:
ABCDEFG
5
NumberShipping Required DateCommon NameDestination AddresssFWDRet ReferencePick Slip/Move Order Numbers
6
Test 1
13/10/2021​
A0135ATBC address 1Ref 1M0-123456
M0-123457
7
Test2
31/08/2021​
A0135FTBC address 1Ref 2M0-123458
M0-123459
8
Test3
11/10/2021​
A0135JTBC address 1Ref 3M0-123460
M0-123461
9
Test4
20/08/2021​
A0202ATBC address 1Ref 4M0-123462
M0-123463
M0-123464
10
Test5
16/08/2021​
A0329ATBC address 1Ref 5M0-123465

What I need is code to copy the data in each row from the Data tab and paste the data into the form below in the tab called "Form" in cells B3, B8, B11, B12, B13, B14. In cell B7 the date needs to be the current date.

FORM:
AB
1
2
3Number:Test1
4Senders Name
5Telephone Number
6
7Date of RequestNeeds to be todays date
8Shipping Required Date13/10/2021
9Recipients Name
10Telephone Number
11Common Name:A0135A
12Destination AddressTBC address 1
13FwdRet ReferenceRef 1
14Pick Slip/Move Order NumberM0-123456
M0-123457
15Full Item Description
16Individual Item Value
17Dimensions in Centimetres
18Weight (Kgs)
19Do these items contain any information?
20Are these Items designed or modified forUse or are they Commercial off the Shelf?
21Programme Name/ Cost Code (for billing purposes)
22Any specific delivery requirements?
(Timed delivery, sameday etc pls note these do incur extra charges)
23Signature
24Print Name

Once the form has been populated, I need code to then copy only the sheet "Form" to a new excel workbook and save the file on my C drive in folder FORMS/Current with the filename as in the form cell B3 (above it would be the filename Test1)
I then need to loop the code to repeat the process for each line of data in the "Data" tab, so in this instance it would create 5 separate workbooks each with the Form and each with the filename as in cell B3, i.e. Test1, Test2, Test3, Test4 & Test5.

Any help to achieve this would be gratefully received.

Many thanks,
Barry.
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Try this
VBA Code:
Sub FillForm()

Dim cell As Range, rngData As Range
Dim wsData As Worksheet, wsForm As Worksheet
Dim wb As Workbook

Const strPath As String = "C:\FORMS\Current\"

Application.ScreenUpdating = False

Set wsData = ActiveWorkbook.Sheets("DATA")
Set wsForm = ActiveWorkbook.Sheets("FORM")

Set rngData = wsData.Range("A6", wsData.Cells(Rows.Count, "A").End(xlUp))

For Each cell In rngData
    wsForm.Range("B3") = wsData.Range("A" & cell.Row)
    wsForm.Range("B7") = Format(Date, "dd/mm/yyy")
    wsForm.Range("B8") = wsData.Range("C" & cell.Row)
    wsForm.Range("B11") = wsData.Range("D" & cell.Row)
    wsForm.Range("B12") = wsData.Range("E" & cell.Row)
    wsForm.Range("B13") = wsData.Range("F" & cell.Row)
    wsForm.Range("B14") = wsData.Range("G" & cell.Row)
    
    Set wb = Workbooks.Add
    wsForm.Cells.Copy wb.Sheets(1).Range("A1")
    wb.SaveAs strPath & wsForm.Range("B3"), 51
    wb.Close False
Next

Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Try this
VBA Code:
Sub FillForm()

Dim cell As Range, rngData As Range
Dim wsData As Worksheet, wsForm As Worksheet
Dim wb As Workbook

Const strPath As String = "C:\FORMS\Current\"

Application.ScreenUpdating = False

Set wsData = ActiveWorkbook.Sheets("DATA")
Set wsForm = ActiveWorkbook.Sheets("FORM")

Set rngData = wsData.Range("A6", wsData.Cells(Rows.Count, "A").End(xlUp))

For Each cell In rngData
    wsForm.Range("B3") = wsData.Range("A" & cell.Row)
    wsForm.Range("B7") = Format(Date, "dd/mm/yyy")
    wsForm.Range("B8") = wsData.Range("C" & cell.Row)
    wsForm.Range("B11") = wsData.Range("D" & cell.Row)
    wsForm.Range("B12") = wsData.Range("E" & cell.Row)
    wsForm.Range("B13") = wsData.Range("F" & cell.Row)
    wsForm.Range("B14") = wsData.Range("G" & cell.Row)
   
    Set wb = Workbooks.Add
    wsForm.Cells.Copy wb.Sheets(1).Range("A1")
    wb.SaveAs strPath & wsForm.Range("B3"), 51
    wb.Close False
Next

Application.ScreenUpdating = True
   
End Sub
Greetings, @Zot this code was very, very helpful. I have a similar issue. Unlike @Barry NP. instead of saving the results as a string to the C drive. What I would like to do is have my "form" sheet copy over so the data in my "data" range can continue to be copied. I am not sure if that is making sense. Essentially I have a row with data in FY2025, I would like that information to copy over into the LIT Form for as many rows as I have. This would range from 75-100 rows. This is my modified code based on the above.

Sub FillLITForm()

Dim cell As Range, rngFY2025 As Range
Dim wsFY2025 As Worksheet, wsLITForm As Worksheet
Dim wb As Workbook

Const strPath As String = "C:\LITforms\Current\"

Application.ScreenUpdating = False

Set wsFY2025 = ActiveWorkbook.Sheets("FY2025")
Set wsLITForm = ActiveWorkbook.Sheets("LITForm")

Set rngFY2025 = wsFY2025.Range("A3", wsFY2025.Cells(Rows.Count, "A").End(xlUp))

For Each cell In rngFY2025
wsLITForm.Range("a1") = wsFY2025.Range("B" & cell.Row)
wsLITForm.Range("B2") = wsFY2025.Range("C" & cell.Row)
wsLITForm.Range("B6") = wsFY2025.Range("A" & cell.Row)
wsLITForm.Range("B10") = wsFY2025.Range("D" & cell.Row)
wsLITForm.Range("B4") = wsFY2025.Range("E" & cell.Row)
wsLITForm.Range("B5") = wsFY2025.Range("F" & cell.Row)
wsLITForm.Range("B8") = wsFY2025.Range("G" & cell.Row)
wsLITForm.Range("B7") = wsFY2025.Range("H" & cell.Row)

Set wb = Workbooks.Add
wsLITForm.Cells.Copy wb.Sheets(1).Range("A1")
wb.SaveAs strPath & wsLITForm.Range("a1"), 51
wb.Close False
Next

Application.ScreenUpdating = True

End Sub

Do you have any suggestions on how I can change this code to allow that to happen?
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,187
Members
452,616
Latest member
intern444

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