从访问行创建excel工作簿

时间:2012-01-19 17:06:22

标签: excel ms-access vba

这是我想要做的。我正在尝试基于标题命名的模板创建工作簿,并为每行创建工作簿。并且让宏循环直到所有行都耗尽。

我最终想要的可交付成果是3个名为(Alpha.xlsx,Beta.xlsx,Gamma.xlsx)的excel文档,其中来自访问的相应值插入到相应工作簿中的相应单元格中。随后的数学是因为我需要能够在excel中操作值。

以下是我发现的一些研究,由于我缺乏vba编码经验,因此我无法理解。

链接

(我不能发布超过2个,所以我会保持文章的数量简洁):

研究: databasejournal.com/features/msaccess/article.php/3563671/Export-Data-To-Excel.htm

示例数据库/电子表格 http://www.sendspace.com/file/iy62c0

图片相册(如果您不想下载,请提供数据库和模板的图片): http://imgur.com/pytPK,PY8FP#0

任何帮助将不胜感激!我一直在阅读并试图找出如何让这个工作@。@

1 个答案:

答案 0 :(得分:0)

这不完整,但应该可以帮助你开始......

Option Compare Database
Option Explicit

'Enter Location of your Template Here
Const ExcelTemplate = "C:\MyTemplate.xltx"
'Enter the Folder Directory to save results to
Const SaveResutsFldr = "C:\Results\"
Sub CreateWorkbook()
Dim SaveAsStr As String
Dim ExcelApp, WB As Object

'Create Reference to Run Excel
Set ExcelApp = CreateObject("Excel.Application")

'Create Reference to your Table
Dim T As Recordset
Set T = CurrentDb.OpenRecordset("tblData")

'Loop through all Record on Table
While Not T.BOF And T.EOF
    'Open Your Excel Template
    Set WB = ExcelApp.Workbooks.Open(ExcelTemplate)

    'Enter your data from your table here to the required cells
    WB.Worksheets("NameOfYourWorkSheet").Range("A1") = T("numValue1")
    'Repeat this line for each piece of data you need entered
    'Changing the Sheet name, cell range, a field name as per your requirements
    'WB.Wor...
    'WB.Wor...

    'Save and Close the Workbook
    SaveAsStr = SaveResutsFldr & T("Title") & ".xlsx"
    WB.SaveAs SaveAsStr
    WB.Close
    Set WB = Nothing

    'Move to the Next Record
    T.MoveNext
Wend

'Close down the Excel Application
ExcelApp.Quit
Set ExcelApp = Nothing
End Sub