I am creating a Excel report from a table. I keep getting requests to change the report about every 2 months so it is frustrating me. It is a financial report. If I redesign the table the next time they make a change request I will be reinventing the wheel again. I was hoping to use the available data in the table to write directly to the Excel spreadsheet. The same process to populate a new table with the given data elements should be able to be used to send to Excel, it is only calculations based on dates.
I have created a function to return the number of months which I will apply to the Monthly charge and write it to Excel.
Here is what I have and it seems to be working fine.
Public Function GetNumberOfMonths(Startdt As Date, Enddt As Date, Year As Integer)
'determine if Year is between FDOC and LDOC, if not value is zero
If Format(Startdt, "yyyy") > Year Then
GetNumberOfMonths = 0
Exit Function
End If
If Format(Startdt, "yyyy") = Year Then
GetNumberOfMonths = DateDiff("m", Startdt, Format(Year, "12/31/" & Year))
Exit Function
End If
If Format(Startdt, "yyyy") < Year And Format(Enddt, "yyyy") > Year Then
GetNumberOfMonths = 12
Exit Function
End If
If Format(Enddt, "yyyy") = Year Then
GetNumberOfMonths = DateDiff("m", Format(Year, "1/1/" & Year), Enddt) + 1
Exit Function
End If
If Format(Enddt, "yyyy") < Year Then
GetNumberOfMonths = 0
Exit Function
End If
End Function
I call the funtion as I am writing to Excel.
xlSheet.Cells(lngRowCount, intColCount) = ![Lease Payment]
intColCount = intColCount + 1
xlSheet.Cells(lngRowCount, intColCount) = ![Lease Payment] * GetNumberOfMonths(!FDOC, !LDOC, 2006)
intColCount = intColCount + 1
xlSheet.Cells(lngRowCount, intColCount) = ![Lease Payment] * GetNumberOfMonths(!FDOC, !LDOC, 2007)
intColCount = intColCount + 1
xlSheet.Cells(lngRowCount, intColCount) = ![Lease Payment] * GetNumberOfMonths(!FDOC, !LDOC, 2008)
intColCount = intColCount + 1
xlSheet.Cells(lngRowCount, intColCount) = ![Lease Payment] * GetNumberOfMonths(!FDOC, !LDOC, 2009)
intColCount = intColCount + 1
xlSheet.Cells(lngRowCount, intColCount) = ![Lease Payment] * GetNumberOfMonths(!FDOC, !LDOC, 2010)
intColCount = intColCount + 1
xlSheet.Cells(lngRowCount, intColCount) = ![Lease Payment] * GetNumberOfMonths(!FDOC, !LDOC, 2011)
intColCount = intColCount + 1
xlSheet.Cells(lngRowCount, intColCount) = ![Lease Payment] * GetNumberOfMonths(!FDOC, !LDOC, 2012)
intColCount = intColCount + 1
xlSheet.Cells(lngRowCount, intColCount) = ![Lease Payment] * GetNumberOfMonths(!FDOC, !LDOC, 2013)
intColCount = intColCount + 1
xlSheet.Cells(lngRowCount, intColCount) = ![Lease Payment] * GetNumberOfMonths(!FDOC, !LDOC, 2014)
Which I can probably fit into a loop.
Thank you for letting me bounce ideas off you. I apologize for the confusion.
I'm sorry if I was unclear. In 2 months they will probably want quarterly amounts...LOL