Mattyastill
New Member
- Joined
- Nov 27, 2017
- Messages
- 23
Hi,
I'm currently trying to write a macro which collates data each day into a masterdata file. I have managed to get the initial copy and paste from one file to the other.
What im currently struggling with is adding todays date in a different column so i know from which date the data was added to the master file.
The StartRange and EndRange essentially give me the start and end point of a specific days data. What im trying to do is select that range and insert todays data in Column AG of that row?
Appretiate any help on this.
I'm currently trying to write a macro which collates data each day into a masterdata file. I have managed to get the initial copy and paste from one file to the other.
What im currently struggling with is adding todays date in a different column so i know from which date the data was added to the master file.
The StartRange and EndRange essentially give me the start and end point of a specific days data. What im trying to do is select that range and insert todays data in Column AG of that row?
Appretiate any help on this.
VBA Code:
Sub Collate_Data()
Dim LastRowBaseFile, LastRowTargetFile, EndRow As Long
Dim BaseFile, TargetFile As Workbook
Dim BaseSheet, TargetSheet As Worksheet
Dim StartRange, EndRange As Range
Set TargetFile = ThisWorkbook
Set BaseFile = Workbooks.Open(Filename:="x")
Set TargetSheet = TargetFile.Sheets("MasterData")
Set BaseSheet = BaseFile.Sheets("Sheet1")
LastRowBaseFile = BaseSheet.Range("A" & Rows.Count).End(xlUp).Row
LastRowTargetFile = TargetSheet.Range("A" & Rows.Count).End(xlUp).Row + 1
EndRow = LastRowTargetFile + LastRowBaseFile - 1
Set StartRange = BaseSheet.Range("AG" & LastRowTargetFile)
Set EndRange = BaseSheet.Range("AG" & EndRow)
BaseSheet.Range("A2:AF" & LastRowBaseFile).SpecialCells(xlCellTypeVisible).Copy
TargetSheet.Range("A" & LastRowTargetFile).PasteSpecial xlPasteAll, xlPasteSpecialOperationNone, False, False
TargetSheet.Activate
TargetSheet.Range(StartRange, EndRange).Select
ActiveCell.Value = Date
End Sub