Hi All,
I have a short macro which opens 2 files, copies data from one to the other and saves it by a certain name (save as).
So far it works,it takes the first line of my data table and updates the other document correctly.
However, I need to create several new files with the data of each row in the original file.
Can I get excel to identify the column headers by name and take the data from the cells underneath row by row?
For example, this is the table in the first document:
[TABLE="width: 413"]
<tbody>[TR]
[TD]ID[/TD]
[TD]Supplyer[/TD]
[TD]Case owner[/TD]
[TD]month of purchase[/TD]
[TD]Cost[/TD]
[/TR]
[TR]
[TD]IT2[/TD]
[TD]Kia[/TD]
[TD]Joe[/TD]
[TD]2018-01[/TD]
[TD="align: right"]10[/TD]
[/TR]
[TR]
[TD]FM4[/TD]
[TD]Daewoo[/TD]
[TD]Mike[/TD]
[TD]2018-02[/TD]
[TD="align: right"]20[/TD]
[/TR]
[TR]
[TD]MK1[/TD]
[TD]Hyundai[/TD]
[TD]Judy[/TD]
[TD]2018-03[/TD]
[TD="align: right"]30[/TD]
[/TR]
</tbody><colgroup><col span="2"><col><col><col></colgroup>[/TABLE]
Then I need new document 1 containing:
IT2, Kia, Joe, 2018-01, 10 - in certain cells
Next, I need another document containing:
FM4, Daewoo, Mike, 2018-02, 20 - in the same cells
The code I have now is unfortunately very simple:
Sub TransferData()
'transfer stuff from Saving tracker to Business case template
Dim strPath1 As String
Dim strPath2 As String
Dim wbkWorkbook1 As Workbook
Dim wbkWorkbook2 As Workbook
'define paths and filenames
strPath1 = "C:\Business cases\Savings Tracker.xlsx"
strPath2 = "C:\Business cases\Business case template.xlsx"
'open files
Set wbkWorkbook1 = Workbooks.Open(strPath1)
Set wbkWorkbook2 = Workbooks.Open(strPath2)
'copy the values across
'saving number
wbkWorkbook2.Worksheets("Supplier").Range("E7").Value = _
wbkWorkbook1.Worksheets("Saving tracker").Range("B7").Value
wbkWorkbook2.Worksheets("Supplier").Range("E6").Value = _
wbkWorkbook1.Worksheets("Saving tracker").Range("G7").Value
wbkWorkbook2.Worksheets("Supplier").Range("E9").Value = _
wbkWorkbook1.Worksheets("Saving tracker").Range("W7").Value
wbkWorkbook2.Worksheets("Supplier").Range("E10").Value = _
wbkWorkbook1.Worksheets("Saving tracker").Range("V7").Value
wbkWorkbook2.Worksheets("Supplier").Range("E11").Value = _
wbkWorkbook1.Worksheets("Saving tracker").Range("AD7").Value
wbkWorkbook2.Worksheets("Supplier").Range("E12").Value = _
wbkWorkbook1.Worksheets("Saving tracker").Range("X7").Value
wbkWorkbook2.Worksheets("Supplier").Range("E13").Value = _
wbkWorkbook1.Worksheets("Saving tracker").Range("I7").Value
wbkWorkbook2.Worksheets("Supplier").Range("J12").Value = _
wbkWorkbook1.Worksheets("Saving tracker").Range("E7").Value
wbkWorkbook2.Worksheets("Supplier").Range("J13").Value = _
wbkWorkbook1.Worksheets("Saving tracker").Range("F7").Value
'define new filename and save
FNAME = wbkWorkbook2.Name
FNAME = Left(FNAME, Len(FNAME) - 4)
currDate = Format(Now(), "yyyy-mm-dd")
wbkWorkbook2.SaveAs Filename:="C:\Business cases" & FNAME & wbkWorkbook2.Worksheets("Supplier").Range("E7").Value & " " & currDate & ".xlsx", _
FileFormat:=51, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False
'close the two workbooks
wbkWorkbook1.Close (False)
wbkWorkbook2.Close (True)
End Sub
I would appreciate your help on this
I have a short macro which opens 2 files, copies data from one to the other and saves it by a certain name (save as).
So far it works,it takes the first line of my data table and updates the other document correctly.
However, I need to create several new files with the data of each row in the original file.
Can I get excel to identify the column headers by name and take the data from the cells underneath row by row?
For example, this is the table in the first document:
[TABLE="width: 413"]
<tbody>[TR]
[TD]ID[/TD]
[TD]Supplyer[/TD]
[TD]Case owner[/TD]
[TD]month of purchase[/TD]
[TD]Cost[/TD]
[/TR]
[TR]
[TD]IT2[/TD]
[TD]Kia[/TD]
[TD]Joe[/TD]
[TD]2018-01[/TD]
[TD="align: right"]10[/TD]
[/TR]
[TR]
[TD]FM4[/TD]
[TD]Daewoo[/TD]
[TD]Mike[/TD]
[TD]2018-02[/TD]
[TD="align: right"]20[/TD]
[/TR]
[TR]
[TD]MK1[/TD]
[TD]Hyundai[/TD]
[TD]Judy[/TD]
[TD]2018-03[/TD]
[TD="align: right"]30[/TD]
[/TR]
</tbody><colgroup><col span="2"><col><col><col></colgroup>[/TABLE]
Then I need new document 1 containing:
IT2, Kia, Joe, 2018-01, 10 - in certain cells
Next, I need another document containing:
FM4, Daewoo, Mike, 2018-02, 20 - in the same cells
The code I have now is unfortunately very simple:
Sub TransferData()
'transfer stuff from Saving tracker to Business case template
Dim strPath1 As String
Dim strPath2 As String
Dim wbkWorkbook1 As Workbook
Dim wbkWorkbook2 As Workbook
'define paths and filenames
strPath1 = "C:\Business cases\Savings Tracker.xlsx"
strPath2 = "C:\Business cases\Business case template.xlsx"
'open files
Set wbkWorkbook1 = Workbooks.Open(strPath1)
Set wbkWorkbook2 = Workbooks.Open(strPath2)
'copy the values across
'saving number
wbkWorkbook2.Worksheets("Supplier").Range("E7").Value = _
wbkWorkbook1.Worksheets("Saving tracker").Range("B7").Value
wbkWorkbook2.Worksheets("Supplier").Range("E6").Value = _
wbkWorkbook1.Worksheets("Saving tracker").Range("G7").Value
wbkWorkbook2.Worksheets("Supplier").Range("E9").Value = _
wbkWorkbook1.Worksheets("Saving tracker").Range("W7").Value
wbkWorkbook2.Worksheets("Supplier").Range("E10").Value = _
wbkWorkbook1.Worksheets("Saving tracker").Range("V7").Value
wbkWorkbook2.Worksheets("Supplier").Range("E11").Value = _
wbkWorkbook1.Worksheets("Saving tracker").Range("AD7").Value
wbkWorkbook2.Worksheets("Supplier").Range("E12").Value = _
wbkWorkbook1.Worksheets("Saving tracker").Range("X7").Value
wbkWorkbook2.Worksheets("Supplier").Range("E13").Value = _
wbkWorkbook1.Worksheets("Saving tracker").Range("I7").Value
wbkWorkbook2.Worksheets("Supplier").Range("J12").Value = _
wbkWorkbook1.Worksheets("Saving tracker").Range("E7").Value
wbkWorkbook2.Worksheets("Supplier").Range("J13").Value = _
wbkWorkbook1.Worksheets("Saving tracker").Range("F7").Value
'define new filename and save
FNAME = wbkWorkbook2.Name
FNAME = Left(FNAME, Len(FNAME) - 4)
currDate = Format(Now(), "yyyy-mm-dd")
wbkWorkbook2.SaveAs Filename:="C:\Business cases" & FNAME & wbkWorkbook2.Worksheets("Supplier").Range("E7").Value & " " & currDate & ".xlsx", _
FileFormat:=51, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False
'close the two workbooks
wbkWorkbook1.Close (False)
wbkWorkbook2.Close (True)
End Sub
I would appreciate your help on this