473,385 Members | 1,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Export to Excel

40
Hello

I've created an Excel spreadsheet to export word count and project count per week, per language, for a Translation company. This code works, however, only for one language at a time. The report must include 8 separate languages in separate data ranges on the same spreadsheet, and I'm at a standstill of how to set it up so that I don't have to copy the code 8 different times. I thought about using varLanguage to pass through from a separate module, but I can't get Excel to save the data new each time. Any ideas?
Expand|Select|Wrap|Line Numbers
  1. Public Function MonthlyReportExport(varLanguage As Variant)
  2.  
  3.    On Error GoTo err_Handler
  4.  
  5.    Dim appExcel As Excel.Application
  6.    Dim wbk As Excel.Workbook
  7.    Dim wks As Excel.Worksheet
  8.    Dim sTemplate As String
  9.    Dim sOutput As String
  10.  
  11.    Dim dbs As dao.Database
  12.    Dim rst As dao.Recordset
  13.    Dim sSQL As String
  14.    Dim iWeeks As Integer
  15.    Dim lWords As Long
  16.    Dim lProjects As Long
  17.    Dim sRange As String
  18.    Dim vStartDate As Variant
  19.    Dim vEndDate As Variant
  20.  
  21.    Set vStartDate = Forms!frmReports!StartDate
  22.    Set vEndDate = Forms!frmReports!EndDate
  23.  
  24.    DoCmd.Hourglass True
  25.  
  26.    Application.SetOption "Error Trapping", 0
  27.  
  28.    sTemplate = "C:\Test\Test.xls"
  29.    sOutput = "C:\Test\Export.xls"
  30.  
  31.    Set appExcel = Excel.Application
  32.    Set wbk = appExcel.Workbooks.Open(sOutput)
  33.    Set wks = appExcel.Worksheets(1)
  34.  
  35. sSQL = "SELECT Count(tblProject.ProjectID) AS CountOfProjectID, Sum(tblProject.WordCount) AS SumOfWordCount FROM (tblProject INNER JOIN tblOffice ON tblProject.OfficeID = tblOffice.OfficeID) INNER JOIN tblTypes ON tblOffice.OfficeTypeID = tblTypes.TypeID WHERE (((tblProject.CombinedRequestDate) Between #" & vStartDate & "# And #" & vEndDate & "#) AND ((tblTypes.Option)='school') AND ((tblProject." & varLanguage & ")=-1)) ORDER BY Count(tblProject.ProjectID);"
  36.  
  37.    Set dbs = CurrentDb
  38.    Set rst = dbs.OpenRecordset(sSQL)
  39.  
  40.   iWeeks = (DateDiff("ww", vStartDate, vEndDate, , vbFirstJul1))
  41.   lProjects = rst.Fields("CountOfProjectID").Value / iWeeks
  42.   lWords = rst.Fields("SumOfWordCount").Value / iWeeks
  43.   rst.Close
  44.   dbs.Close
  45.  
  46.   sSQL = "SELECT " & lProjects & ", " & lWords & ";"
  47.  
  48.   Set dbs = CurrentDb
  49.   Set rst = dbs.OpenRecordset(sSQL)
  50.   sRange = "School_" & varLanguage & ""
  51.  
  52. wks.Range(sRange).CopyFromRecordset rst
  53.  
  54. exit_Here:
  55.    ' Cleanup all objects  (resume next on errors)
  56.    On Error Resume Next
  57.    Set wks = Nothing
  58.    Set wbk = Nothing
  59.    Set appExcel = Nothing
  60.    Set rst = Nothing
  61.    Set dbs = Nothing
  62.    DoCmd.Hourglass False
  63.    Exit Function
  64.  
  65. err_Handler:
  66.    ExportRequest = Err.Description
  67.    Resume exit_Here
  68.  
  69. End Function
Aug 21 '07 #1
3 1383
MMcCarthy
14,534 Expert Mod 8TB
Assuming this passes to just one row in the spreadsheet ...

Pass a second parameter to the code incrementing the row position and call the function in a loop incrementing the row position each time.
Sep 1 '07 #2
RobinAG
40
Assuming this passes to just one row in the spreadsheet ...

Pass a second parameter to the code incrementing the row position and call the function in a loop incrementing the row position each time.

Thanks, that's just what I did.
Sep 5 '07 #3
MMcCarthy
14,534 Expert Mod 8TB
Thanks, that's just what I did.
Glad it worked for you.
Sep 5 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Matt | last post by:
I have an ASP page that calls ASP routines that I created that execute a database query and return the results to a recordset. I then iterate through the recordset and display the data in a table....
5
by: Maria L. | last post by:
Hi, I need to export the content of a DataGrid (in Windows application in C#), into an Excel spreadsheet. Anyone knows how to do this? Any code snippets would help! thanks a lot, Maria
2
by: Siu | last post by:
Hi, I use the following code to export and import a file Excel from resp. into a Web page with the following code: //EXPORT Response.Clear(); Response.Buffer = true; Response.ContentType =...
6
by: Elena | last post by:
I'm trying to export data to an Excel worksheet. I can export the data in the cell values perfectly. I need the code to change a header and footer for the worksheet, not for the columns. Is...
13
by: Hemant Sipahimalani | last post by:
The following piece of code is being used to export HTML to excel. HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"...
5
by: Simon | last post by:
Dear reader, With the export command you can export a query to Excel. By activate this command a form pop's up with the following text:
1
by: smaczylo | last post by:
Hello, I've recently been asked to work with Microsoft Access, and while I feel quite comfortable with Excel, I'm at a complete loss with databases. If someone could help me with this issue I'm...
1
by: CoolFactor | last post by:
MY CODE IS NEAR THE BOTTOM I want to export this Access query into Excel using a command button on an Access form in the following way I describe below. Below you will find the simple query I am...
3
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, I have a question for you. I have a .csv file which has many lines of data. Each line has many data fields which are delimited by ",". Now I need to extract part of data from this...
2
hemantbasva
by: hemantbasva | last post by:
Note We need to have a template on server for generating report in multiple sheet as we do not had msoffice on server moreover this require a batch job to delete excel file created by the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.