I have an extensive Excel report that gets populated with data every month. The data comes from user-entered records in my Access database. The Excel report is required in its current format and the powers that be will not accept an Access report with the same data.
The Excel report has some 400 rows of data and it is divided into sections (Facilities, Number of People, Miles, Minutes, Ages, etc.)
I have an Access Report (that is much prettier) that compiles the data for each of those sections where it can be reviewed easily. Once it is reviewed, it is exported to the Excel report at the click of the button.
The data is compiled by month. The dates are selected in a central report compilation form.
I will use the Facilities section as my example as it is the most extensive section of the Excel report. My module looks something like this:
The problem is, sometimes I have to add new rows to the Excel sheet to add another facility in this instance. This means that I have to go back in the module, and update the row numbers (colValue & rowNumber).
I want to be able to change the row numbers in a table rather than do so in the code. This will make it much friendlier to work with and less prone to mistakes.
What I can't figure out is how I can relate the row number in the .Cells.Range(colValue & rowNumber).Value = Reports....etc. lines to the row number on the Excel Report
Here's what I did so far, and it works, but it looks very redundant and I think there is surely a better way to do it.
I've also considered drawing the data from the queries themselves but I'm not sure how to do that for this scenario.
Anybody want to give it a shot?
The Excel report has some 400 rows of data and it is divided into sections (Facilities, Number of People, Miles, Minutes, Ages, etc.)
I have an Access Report (that is much prettier) that compiles the data for each of those sections where it can be reviewed easily. Once it is reviewed, it is exported to the Excel report at the click of the button.
The data is compiled by month. The dates are selected in a central report compilation form.
I will use the Facilities section as my example as it is the most extensive section of the Excel report. My module looks something like this:
Code:
strRpt = "CAMTS_AgencyMisType"
DestName = "Excel Report File Here"
Set xlApp = New Excel.Application
With xlApp
.visible = True
Set xlWB = .Workbooks.Open(DestName)
With xlWB
With .Sheets("2003TC")
'COLUMN DETERMINATION, BASED ON DATES SELECTED IN THE CENTRAL REPORT COMPILATION FORM
Select Case Forms!Reports!txtStart
Case "Jul"
colValue = "C"
Case "Aug"
colValue = "D"
Case "Sep"
colValue = "E"
Case "Oct"
colValue = "F"
Case "Nov"
colValue = "G"
Case "Dec"
colValue = "H"
Case "Jan"
colValue = "I"
Case "Feb"
colValue = "J"
Case "Mar"
colValue = "K"
Case "Apr"
colValue = "L"
Case "May"
colValue = "M"
Case "Jun"
colValue = "N"
End Select
'HOW I GET THE DATA FROM THE REPORT TO THE EXCEL SHEET. colValue = Month and correlates to the column
'40's
.Cells.Range(colValue & 43).Value = Reports(strRpt)!txtFirstFacility
.Cells.Range(colValue & 44).Value = Reports(strRpt)!txtSecondFacility
.Cells.Range(colValue & 45).Value = Reports(strRpt)!txtThirdFacility
.Cells.Range(colValue & 46).Value = Reports(strRpt)!txtFourthFacility
.Cells.Range(colValue & 47).Value = Reports(strRpt)!txtFifthFacility
etc. etc. etc about 300+ more times
I want to be able to change the row numbers in a table rather than do so in the code. This will make it much friendlier to work with and less prone to mistakes.
What I can't figure out is how I can relate the row number in the .Cells.Range(colValue & rowNumber).Value = Reports....etc. lines to the row number on the Excel Report
Here's what I did so far, and it works, but it looks very redundant and I think there is surely a better way to do it.
Code:
strSQL(0) = "SELECT rowNumber FROM RptCardDataCells WHERE ctlName = ""SumofTotMiles"""
strSQL(1) = "SELECT rowNumber FROM RptCardDataCells WHERE ctlName = ""SumofLdMiles"""
strSQL(2) = "SELECT rowNumber FROM RptCardDataCells WHERE ctlName = ""SumoffltMinutes"""
Set db = CurrentDb
Set rs = db.OpenRecordset(strSQL(0))
rowValue = rs!rowNumber
.Cells.Range(colValue & rowValue).Value = Reports!(strRpt)!txtFirstFacility
Set rs = db.OpenRecordset(strSQL(1))
rowValue = rs!rowNumber
.Cells.Range(colValue & rowValue).Value = Reports!(strRpt)!txtSecondFacility
Set rs = db.OpenRecordset(strSQL(2))
rowValue = rs!rowNumber
.Cells.Range(colValue & rowValue).Value = Reports!(strRpt)!txtThirdFacility
etc. etc. 300+ times
rs.Close
Set rs = Nothing
Anybody want to give it a shot?