Copy Value from new Workbook to existing Workbook

Posts
1
Registration date
Friday September 14, 2018
Last seen
September 14, 2018
- - Latest reply: ac3mark
Posts
11385
Registration date
Monday June 3, 2013
Status
Moderator
Last seen
February 19, 2019
- Sep 14, 2018 at 05:11 PM
Hello,

I have new workbook file which is generated every month (every 1st date of the month). The Workbook Name will hava a format like "Historical_92018" (for this month), and next month it will generate new workbook with name "Historical_102018" (For OCtober) and so on.

I want to copy all rows available on the last month workbook and paste it to one master workbook called "YearlyHistorical2018", this process will be execute on the 1st date every month. So on 1st October, all rows from "Historical_92018" will be copied to master file. And on 1st November, all rows from "Historical_102018" will be copied to master file.

So by 1st January 2019, i will have all last year data on master table called "YearlyHistorical2018".

Is it possible to do this? and please give example code.

Thanks
See more 

Your reply

1 reply

Posts
11385
Registration date
Monday June 3, 2013
Status
Moderator
Last seen
February 19, 2019
2143
0
Thank you
Yes, this is possible.

Below are some code examples. Cut and paste what you need and make your own routine.



Function sheetexist(whatsheet)
On Error GoTo NotExists

ThisWorkbook.Worksheets(whatsheet).Select
sheetexist = True
Exit Function

NotExists:
sheetexist = False

End Function

Function testsheet(whichsheet, rowNum)
nret = sheetexist(whichsheet)
If nret = False Then makesheet (whichsheet)
nret = copyrowX(whichsheet, rowNum)
End Function

Sub makesheet(whatsheet)
On Error GoTo ExitSub
With ThisWorkbook
.Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = whatsheet
End With

ExitSub:

End Sub


Function copyrowX(towhatsheet, whatrow)
ThisWorkbook.Worksheets("Sheet1").Select
ThisWorkbook.Worksheets("Sheet1").Range("A" & whatrow).EntireRow.Select
Selection.Copy

ThisWorkbook.Worksheets(towhatsheet).Select
cellcount = Cells(ThisWorkbook.Worksheets(towhatsheet).Rows.Count, 1).End(xlUp).Row
ThisWorkbook.Worksheets(towhatsheet).Range("A" & cellcount).EntireRow.Select
Selection.Insert

End Function


Sub ReadSheet()
cellcount = Cells(ThisWorkbook.Worksheets("Sheet1").Rows.Count, 1).End(xlUp).Row
For RowCount = 1 To cellcount
cellvalue = ThisWorkbook.Worksheets("Sheet1").Range("A" & RowCount).Value
nret = testsheet(cellvalue, RowCount)
ThisWorkbook.Worksheets("Sheet1").Select
Next
End Sub







Respond to ac3mark