What I am trying to do:
- I have three excels - Master.xlsx (my source data), DiscrepencyReport (the one my macro will create and populate calculated values from Master.xlsx) and the ReportRunner (my active worksheet where I will give some input values and run the macro)
- I want to programmatically open the Master.xlsx and Create a new file called DiscepencyReport with the date range in filename.
- Copy certain values from source, manipulate it and then paste it to the target sheet.
- Close all workbooks.
What help I need:
- First, it give me a run-time error saying Object Required. The error is on this line "InputFileName = This.Workbook.Path & "\" & "Master.xlsx"". I googled and read previous errors and tried using / removing the set keyword yet it does not work. I am missing my basics here. No error when I compile.
- Is this the correct way to approach this problem? or is there a better way?
- There is a part where I am "Constructing the Report Sheet". I would like some guidance on the syntax on how to copy the column A from worksheet called as "Base" of Master.xlsx to the Target workbook's sheet1's A1 column. The below code does only copying and does not paste.
The code I have is given below:
Sub ReportGeneration()
'Declarations
Dim InputSheet As Workbook, AttendanceDiscrepencyReporter As Workbook
Dim Start As String
Dim Last As String
Dim InputFileName As String
Dim ADRFileName As String
Dim TWorkingDays As Integer
Dim EmpEmail As Range
Dim EmpID As Range
Dim TLeave As Range
Dim TFlexi As Range
Dim TAttendance As Range
Dim IsAtOnsite As Range
Dim CMEmail As Range
'Open Input Workbook
InputFileName = This.Workbook.Path & "\" & "Master.xlsx"
Set InputSheet = Workbooks.Open(InputFileName, True, True)
'Create the Report Workbook
Start = Replace((Range("F7").Value), "/", "_")
Last = Replace((Range("F8").Value), "/", "_")
TWorkingDays = Range("F10").Value
Set AttendanceDiscrepencyReporter = Workbooks.Add
ADRFileName = ThisWorkbook.Path & "\" & "DiscrepencyReport_from_" & Start & "_to_" & Last & ".xlsx"
With AttendanceDiscrepencyReporter
.Title = "Discrepency Report"
.Subject = "Discrepency Report"
.SaveAs Filename:=ADRFileName
End With
' Construct the Report Worksheet
Set EmpEmail = InputSheet.Worksheets("Base").Columns("A")
Set EmpID = InputSheet.Worksheets("Base").Columns("B")
' Close Workbooks
InpuSheet.Close
AttendanceDiscrepencyReporter.Close
End Sub