Solved

Add header fields to an excel spreadsheet that is created using code

Posted on 2008-06-20
12
383 Views
Last Modified: 2013-11-27
Hi, I have an Access module that parses data and creates individual spreadsheets.  I'd like to add on the first 4 rows of the Excel spreadsheets some header information (i.e. company name and report name on row 1, the specific agents name on row 2, the month and date of the report on row 3 and "Company Confidential" on row 4.  Then I'd like the individual parsed records (and column headings) to begin on row 5.  I've included the module code below.  Thanks in advance for your help.

Option Compare Database

Sub exp2XL2()
Dim rs As DAO.Recordset, rsDir As DAO.Recordset
Dim ssql As String, iCol
Dim xlObj As Object
Dim Sheet As Object

Set rsDir = CurrentDb.OpenRecordset("select distinct AgentName from tblAgentCommissionReportOutput")

If rsDir.EOF Then Exit Sub
rsDir.MoveFirst

Do Until rsDir.EOF
Set xlObj = CreateObject("Excel.Application")
xlObj.Workbooks.Add

ssql = "SELECT [tblAgentCommissionReportOutput].AgentName, "
ssql = ssql & " [tblAgentCommissionReportOutput].AgentTerritory, "
ssql = ssql & " [tblAgentCommissionReportOutput].strAcctMgrLogin, "
ssql = ssql & " [tblAgentCommissionReportOutput].CustomerTerritory, "
ssql = ssql & " [tblAgentCommissionReportOutput].dtClientStartDate AS StartDate, "
ssql = ssql & " [tblAgentCommissionReportOutput].tDate AS TransDate, "
ssql = ssql & " [tblAgentCommissionReportOutput].[Description], "
ssql = ssql & " [tblAgentCommissionReportOutput].TransAmount, "
ssql = ssql & " [tblAgentCommissionReportOutput].AgentComm, "
ssql = ssql & " [tblAgentCommissionReportOutput].AgentPay, "
ssql = ssql & " [tblAgentCommissionReportOutput].FranchiseeName"
ssql = ssql & " FROM [tblAgentCommissionReportOutput]"
ssql = ssql & " Where AgentName='" & rsDir("AgentName") & "'"

Set rs = CurrentDb.OpenRecordset(ssql, dbOpenDynaset)

Set Sheet = xlObj.activeworkbook.Sheets("sheet1")

'rename the sheet, you can use any of the recordset field
Sheet.Name = rsDir("AgentName")

'copy the headers
For iCol = 0 To rs.Fields.Count - 1
Sheet.cells(1, iCol + 1).Value = rs.Fields(iCol).Name
Next

Sheet.Range("A2").CopyFromRecordset rs 'copy the data

xlObj.activeworkbook.SaveAs "C:\Documents and Settings\All Users\Desktop\" & rsDir("AgentName") & ".xls", FileFormat:=-4143
Set Sheet = Nothing
xlObj.Quit
Set xlObj = Nothing
rsDir.MoveNext
Loop
rsDir.Close
rs.Close
Set rsDir = Nothing
Set rs = Nothing
End Sub
0
Comment
Question by:divehunter
  • 6
  • 5
12 Comments
 

Author Comment

by:divehunter
ID: 21834388
I just realized there is another related item I need help with.  In the spreadsheets the code creates there are 2 columns.  One column is amount and the other is commission amount.  Because each agent will have a different number of records (rows) in their specific spreadsheet, is there a way in the code to specify to sum the 2 columns at the bottom of the spreadsheet?  As an example, record one might be for John Smith with an Amount of $100 and a commission amount of $20, record two (also for John) might show a record with an amount of $50 and a commission amount of $10.  I'd like to have a sum total at the bottom of each of these columns (i.e. total amount $150, total commission amount $30).  Thanks again and please let me know if you need additional information.  
0
 

Author Comment

by:divehunter
ID: 21835168
This is the last item to finish this project so I thought I'd add some more points in the hope someone can help.  Thanks in advance.
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 21835366
Hello divehunter,

1) It's difficult to read VBA code that is not indented properly.  I think you may get more Expert involvement
if you'd indent.

2) Please post a mockup of what the final Excel output should look like.  Fake the data if need be to protect
sensitive information.

Regards,

Patrick
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 21835376

better if you can attach a db with table tblAgentCommissionReportOutput
and an example of the output excel file
0
 

Author Comment

by:divehunter
ID: 21835719
Hi, Thanks for the response.  I've attached 1 workbook (3 worksheets).  One is called sample and it shows what the current output spreadsheet looks like for each individual agent.  The second spreadsheet (desired) is what I'd like to get it to look like.  If it's a lot of work to bold the header fields and center them, don't worry about it.  The 3rd is an excel (trunc) version of the tblAgentCommissionReportOutput.  Hope this helps. Sorry about not indenting.  Still learning the ropes.  I'll make sure I indent in the future.  Thanks for the heads up and for all of your help.  
projectsample.xls
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 21835726
where is the db with the table
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 21836084


Sub exp2XL2()
Dim rs As DAO.Recordset, rsDir As DAO.Recordset
Dim ssql As String, iCol
Dim xlObj As Object
Dim Sheet As Object

Set rsDir = CurrentDb.OpenRecordset("select distinct AgentName from tblAgentCommissionReportOutput where AgentName='All Mountain Technologies'")

If rsDir.EOF Then Exit Sub
rsDir.MoveFirst

Do Until rsDir.EOF
Set xlObj = CreateObject("Excel.Application")
xlObj.Workbooks.Add
'Stop
ssql = "SELECT [tblAgentCommissionReportOutput].AgentName, "
ssql = ssql & " [tblAgentCommissionReportOutput].AgentTerritory, "
ssql = ssql & " [tblAgentCommissionReportOutput].strAcctMgrLogin, "
ssql = ssql & " [tblAgentCommissionReportOutput].CustomerTerritory, "
ssql = ssql & " [tblAgentCommissionReportOutput].dtClientStartDate AS StartDate, "
ssql = ssql & " [tblAgentCommissionReportOutput].tDate AS TransDate, "
ssql = ssql & " [tblAgentCommissionReportOutput].[Description], "
ssql = ssql & " [tblAgentCommissionReportOutput].TransAmount, "
ssql = ssql & " [tblAgentCommissionReportOutput].AgentComm, "
ssql = ssql & " [tblAgentCommissionReportOutput].AgentPay, "
ssql = ssql & " [tblAgentCommissionReportOutput].FranchiseeName"
ssql = ssql & " FROM [tblAgentCommissionReportOutput]"
ssql = ssql & " Where AgentName='" & rsDir("AgentName") & "'"

Set rs = CurrentDb.OpenRecordset(ssql, dbOpenDynaset)

Set Sheet = xlObj.activeworkbook.Sheets("sheet1")

'rename the sheet, you can use any of the recordset field
Sheet.Name = rsDir("AgentName")

'copy the headers
For iCol = 0 To rs.Fields.Count - 1
Sheet.cells(5, iCol + 1).Value = rs.Fields(iCol).Name
Next

Sheet.range("A6").CopyFromRecordset rs 'copy the data

Dim rowCnt, colCnt
Sheet.cells(1, 1).Value = "DataPreserve Agent Commission Report"
Sheet.cells(2, 1).Value = Format(Date, "mmm - yyyy")
Sheet.cells(3, 1).Value = rsDir("AgentName")
Sheet.cells(4, 1).Value = "Confidential"

rowCnt = Sheet.usedrange.rows.Count
colCnt = Sheet.usedrange.Columns.Count

xlObj.range("A1:" & Chr(64 + colCnt) & 1).merge
xlObj.range("A2:" & Chr(64 + colCnt) & 2).merge
xlObj.range("A3:" & Chr(64 + colCnt) & 3).merge
xlObj.range("A4:" & Chr(64 + colCnt) & 4).merge
xlObj.range("A1:" & Chr(64 + colCnt) & 4).Font.Bold = True
xlObj.range("A1:" & Chr(64 + colCnt) & 4).HorizontalAlignment = -4108


xlObj.range("A5:" & Chr(64 + colCnt) & 5).Font.Bold = True
xlObj.range("A5:" & Chr(64 + colCnt) & rowCnt).Columns.AutoFit

xlObj.range("H" & rowCnt + 1).formula = "=Sum(" & "H6:H" & rowCnt & ")"
xlObj.range("J" & rowCnt + 1).formula = "=Sum(" & "J6:J" & rowCnt & ")"


xlObj.range("A" & rowCnt + 1).Value = "Total"
xlObj.range("A" & rowCnt + 1).entirerow.Font.Bold = True

xlObj.activeworkbook.SaveAs "C:\Documents and Settings\All Users\Desktop\" & rsDir("AgentName") & ".xls", FileFormat:=-4143
Set Sheet = Nothing
xlObj.Quit
Set xlObj = Nothing
rsDir.MoveNext
Loop
rsDir.Close
rs.Close
Set rsDir = Nothing
Set rs = Nothing
End Sub
0
 

Author Closing Comment

by:divehunter
ID: 31469306
I have to Capricorn1 that it's obvious why you're #1.  Everytime you help me I'm amazed by your knowledge.  Thank you so much for all your help.  This worked perfectly.  I had to change the 'AllMountainTechnologies' back to AgentName but it couldn't work any better.
0
 

Author Comment

by:divehunter
ID: 21848645
PS - My boss asked me to let me know if you do any side contract work, we would be very interested in talking to you about some development work we need done.  Thanks again.
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 21848681
look in my profile. send me a note.
0
 

Author Comment

by:divehunter
ID: 21849880
Thanks.  I've forwarded your email address to my boss.  He should be contacting you soon.  His name is Carl Landis.  Thanks again for all the help.
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 21849965
u r welcome!!!
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question