Just abit more context on what I require help with. I need to create a VBA macro that will copy a specific row from a worksheet and paste it into another workbook sheet. Thereafter saving the file as a new workbook. I need to ensure that this VBA is an array, meaning that this will need to be done on many workbooks in a folder. That is. For each source workbook copy the row of data, paste it in the master workbook and save the workbook as a new workbook. that being said I would require 10 master workbooks as there are 10 source workbooks.
This is where my workbooks reside
This is the sample of a source workbook file as shown below. I need to copy just the data without headers so row 2. This needs to be done for all files within the folder above. All file have the same layout with just row 2 as where the data resides.
The master/destination workbook is shown below and the row that data should be pasted in is row 9. This templated workbook resides in a different folder.
Below is the code i use that currently adds the rows of data from multiple workbooks within the source folder to the master workbook, however this increments the rows. I need help with how to create a new master workbook for each source workbook and thereafter save the master workbook with a source workbook name as a suffix Example "Master workbook-AAAA".xlsx
Option Explicit
Const FOLDER_PATH = "C:\Users\\Desktop\Split Files\" 'REMEMBER END BACKSLASH'
Sub ImportWorksheets()
'Process all Excel files in specified folder'
Dim sFile As String 'file to process
Dim wsTarget As Worksheet
Dim wbSource As Workbook
Dim wsSource As Worksheet
Dim rowTarget As Long 'output row
rowTarget = 9
'check the folder exists
If Not FileFolderExists(FOLDER_PATH) Then
MsgBox "Specified folder does not exist, exiting!"
Exit Sub
End If
'reset application settings in event of error'
On Error GoTo errHandler
Application.ScreenUpdating = False
'set up the target worksheet'
Set wsTarget = Sheets("DATABASE")
'loop through the Excel files in the folder'
sFile = Dir(FOLDER_PATH & "*.xls*")
Do Until sFile = ""
'open the source file and set the source worksheet - ASSUMED WORKSHEET(1)
Set wbSource = Workbooks.Open(FOLDER_PATH & sFile)
Set wsSource = wbSource.Worksheets(1)
'import the data'
With wsTarget
.Range("A" & rowTarget).Value = wsSource.Range("A2").Value
.Range("B" & rowTarget).Value = wsSource.Range("B2").Value
.Range("C" & rowTarget).Value = wsSource.Range("C2").Value
.Range("D" & rowTarget).Value = wsSource.Range("D2").Value
.Range("E" & rowTarget).Value = wsSource.Range("E2").Value
.Range("F" & rowTarget).Value = wsSource.Range("F2").Value
.Range("G" & rowTarget).Value = wsSource.Range("G2").Value
.Range("H" & rowTarget).Value = wsSource.Range("H2").Value
.Range("I" & rowTarget).Value = wsSource.Range("I2").Value
End With
'close the source workbook, increment the output row and get the next file'
wbSource.Close SaveChanges:=False
rowTarget = rowTarget + 1
sFile = Dir()
Loop
errHandler:
On Error Resume Next
Application.ScreenUpdating = True
'tidy up'
Set wsSource = Nothing
Set wbSource = Nothing
Set wsTarget = Nothing
End Sub
Private Function FileFolderExists(strPath As String) As Boolean If Not Dir(strPath, vbDirectory) = vbNullString Then FileFolderExists = True End Function
Just an update..
I tried a different approach as shown below. However the workbook is crashing. Any idea what am i doing wrong ?