【问题标题】:Export and format multiple sheets from Access to Excel将多个工作表从 Access 导出并设置为 Excel 格式
【发布时间】:2020-07-13 05:06:27
【问题描述】:

我正在尝试将多个摘要报告从 Access 导出到 Excel。源数据在 Access 中。最终用户通过单击按钮创建这些报告的表单也在 Access 中。我面临 2 个困难,想知道您是否可以提供帮助:

  1. 我成功地导出了我的报告,但由于某种原因我的格式未实现 - 请告诉我原因并提出如何解决此问题的想法。

  2. 除了我的报告之外,我还想添加图表。请您帮助我了解我该如何开始。

*** 到目前为止,我所做的是 excel 中的宏 vba 并且不知何故想出了如何在访问中翻译它。为什么它如此不同,为什么我不能使用相同的语言语法?我对编程相当陌生,但从逻辑上讲,因为 Microsoft Office 创建了这两种环境 - 语言不应该相同吗?我的意思是我会假设为什么许多用户更喜欢购买他们的产品 - 具有讽刺意味的是 - 我的理论不支持我的假设......请帮助

这是我的代码:

Private Sub cmdREPORT_GenerateUWReport_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)

On Error GoTo cmdREPORT2_err

   Dim appExcel As Variant
   Dim wbkExcel As Object
   Dim wstExcel As Object
   Dim dblFormattedStartDate As Double
   Dim dblFormattedEndDate As Double
   Dim strFileSavePath As String
   Dim strFilter As String


   If (IsNull(comboREPORT_StartDate.Value) Or comboREPORT_StartDate.Value = "") Then
      MsgBox ("No Start Date selected.")
      Exit Sub

   ElseIf (IsNull(comboREPORT_EndDate.Value) Or comboREPORT_EndDate.Value = "") Then
      MsgBox ("No End Date selected.")
      Exit Sub

   End If
   dblFormattedStartDate = Right(comboREPORT_StartDate.Value, 4) & _
                           Left(comboREPORT_StartDate.Value, 2)
   dblFormattedEndDate = Right(comboREPORT_EndDate.Value, 4) & _
                           Left(comboREPORT_EndDate.Value, 2)

   If (dblFormattedStartDate > dblFormattedEndDate) Then
      MsgBox ("Start Date is greater than End Date.")
      Exit Sub

   End If

   strFilter = ahtAddFilterItem(strFilter, "Excel Files (*.XLS)", "*.XLS")
   strFilter = ahtAddFilterItem(strFilter, "All Files (*.*)", "*.*")

   strFileSavePath = ahtCommonFileOpenSave( _
      OpenFile:=False, _
      InitialDir:="C:\Documents And Settings\" & fOSUserName() & "\Desktop\", _
      Filter:=strFilter, _
      DialogTitle:="Save file as:", _
      Flags:=ahtOFN_OVERWRITEPROMPT Or ahtOFN_READONLY, _
      Filename:="URC_Reports.xls")

   DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "D60: DetailReportDonna", strFileSavePath, True, "Detail_Report"
   DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "D24: FA_Month", strFileSavePath, True, "FA_Month"
   DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "D34: FA_Quarter", strFileSavePath, True, "FA_Quarter"
   DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "D40: Policy_Month_Count", strFileSavePath, True, "Policy_Month_Count"
   DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "D50: Policy_Quarter_Count", strFileSavePath, True, "Policy_Quarter_Count"
   DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "D10: Risk_Issue_Details", strFileSavePath, True, "Risk_Issue_Details"


   Set appExcel = CreateObject("Excel.Application")
   appExcel.Visible = True
   Set wbkExcel = appExcel.Workbooks.Open(strFileSavePath)
   Set wstExcel = wbkExcel.ActiveSheet

   With appExcel
    .ActiveWorkbook.Sheets("Detail_Report").Cells.Font.Name = "Times New Roman"
    .ActiveWorkbook.Sheets("Detail_Report").Cells.Font.Size = 11
    .ActiveWorkbook.Sheets("Detail_Report").Rows("2:2").Select
    .ActiveWorkbook.Sheets("Detail_Report").ActiveWindow.FreezePanes = True
    .ActiveWorkbook.Sheets("Detail_Report").Rows("1:1").Font.Bold = True
    .ActiveWorkbook.Sheets("Detail_Report").Rows("1:1").Font.ColorIndex = 2
    .ActiveWorkbook.Sheets("Detail_Report").Rows("1:1").Interior.ColorIndex = 12
    .ActiveWorkbook.Sheets("Detail_Report").Rows("1:1").ColumnWidth = 15
    .ActiveWorkbook.Sheets("Detail_Report").Rows("1:1").RowHeight = 40
    .ActiveWorkbook.Sheets("Detail_Report").Rows("1:1").HorizontalAlignment = xlHAlignCenter
    .ActiveWorkbook.Sheets("Detail_Report").Rows("1:1").WrapText = True
    .ActiveWorkbook.Sheets("Detail_Report").Rows("1:1").AutoFilter
    .ActiveWorkbook.Sheets("Detail_Report").Tab.Color = 1

    .ActiveWorkbook.Sheets("FA_Month").Tab.Color = 92
    .ActiveWorkbook.Sheets("FA_Month").Rows("1:1").RowHeight = 40
    .ActiveWorkbook.Sheets("FA_Month").Rows("1:1").Font.ColorIndex = 2
    .ActiveWorkbook.Sheets("FA_Month").Rows("1:1").Interior.ColorIndex = 14
    .ActiveWorkbook.Sheets("FA_Month").Rows("1:1").Font.Bold = True
    .ActiveWorkbook.Sheets("FA_Month").Columns("A:M").EntireColumn.AutoFit
    .ActiveWorkbook.Sheets("FA_Month").Columns("C:H").NumberFormat = "$#,##0"
    .ActiveWorkbook.Sheets("FA_Month").Cells.Font.Name = "Times New Roman"
    .ActiveWorkbook.Sheets("FA_Month").Cells.Font.Size = 11
    .ActiveWorkbook.Sheets("FA_Month").Cells.HorizontalAlignment = xlHAlignRight

    .ActiveWorkbook.Sheets("FA_Quarter").Tab.Color = 92
    .ActiveWorkbook.Sheets("FA_Quarter").Rows("1:1").RowHeight = 40
    .ActiveWorkbook.Sheets("FA_Quarter").Rows("1:1").Font.ColorIndex = 2
    .ActiveWorkbook.Sheets("FA_Quarter").Rows("1:1").Interior.ColorIndex = 14
    .ActiveWorkbook.Sheets("FA_Quarter").Rows("1:1").Font.Bold = True
    .ActiveWorkbook.Sheets("FA_Quarter").Columns("A:M").EntireColumn.AutoFit
    .ActiveWorkbook.Sheets("FA_Quarter").Columns("C:H").NumberFormat = "$#,##0"
    .ActiveWorkbook.Sheets("FA_Quarter").Cells.Font.Name = "Times New Roman"
    .ActiveWorkbook.Sheets("FA_Quarter").Cells.Font.Size = 11
    .ActiveWorkbook.Sheets("FA_Quarter").Cells.HorizontalAlignment = xlHAlignRight

    .ActiveWorkbook.Sheets("Policy_Month_Count").Tab.Color = 246
    .ActiveWorkbook.Sheets("Policy_Month_Count").Rows("1:1").RowHeight = 40
    .ActiveWorkbook.Sheets("Policy_Month_Count").Rows("1:1").Font.ColorIndex = 2
    .ActiveWorkbook.Sheets("Policy_Month_Count").Rows("1:1").Interior.ColorIndex = 49
    .ActiveWorkbook.Sheets("Policy_Month_Count").Rows("1:1").Font.Bold = True
    .ActiveWorkbook.Sheets("Policy_Month_Count").Columns("A:M").EntireColumn.AutoFit
    .ActiveWorkbook.Sheets("Policy_Month_Count").Cells.Font.Name = "Times New Roman"
    .ActiveWorkbook.Sheets("Policy_Month_Count").Cells.Font.Size = 11
    .ActiveWorkbook.Sheets("Policy_Month_Count").Cells.HorizontalAlignment = xlHAlignRight

    .ActiveWorkbook.Sheets("Policy_Quarter_Count").Tab.Color = 246
    .ActiveWorkbook.Sheets("Policy_Quarter_Count").Rows("1:1").RowHeight = 40
    .ActiveWorkbook.Sheets("Policy_Quarter_Count").Rows("1:1").Font.ColorIndex = 2
    .ActiveWorkbook.Sheets("Policy_Quarter_Count").Rows("1:1").Interior.ColorIndex = 49
    .ActiveWorkbook.Sheets("Policy_Quarter_Count").Rows("1:1").Font.Bold = True
    .ActiveWorkbook.Sheets("Policy_Quarter_Count").Columns("A:M").EntireColumn.AutoFit
    .ActiveWorkbook.Sheets("Policy_Quarter_Count").Cells.Font.Name = "Times New Roman"
    .ActiveWorkbook.Sheets("Policy_Quarter_Count").Cells.Font.Size = 11
    .ActiveWorkbook.Sheets("Policy_Quarter_Count").Cells.HorizontalAlignment = xlHAlignRight

   End With

cmdREPORT2_err:

   Exit Sub

End Sub

【问题讨论】:

  • 您可能需要在 access 中添加对 Excel 的引用。在代码窗口转到toolsreferences... 并检查Microsoft Excel 14.0 Object Library 这将为您提供Excel.ApplicationWorkbookWorksheet 等对象,以便与Excel 文件进行交互
  • 将你的声明更改为Dim appExcel As Excel.Application Dim wbkExcel As Excel.Workbok Dim wstExcel As Excel.Worksheet 你应该会很好
  • 谢谢埃文。这对我帮助很大。请告诉我我还需要在格式中添加什么才能获得图表。
  • 这是我的想法 - 但它无法正常工作... .ActiveWorkbook.Sheets("FA_Month").Charts.Add.ChartType = xlCylinderColStacked .ActiveWorkbook.Sheets("FA_Month") .ActiveChart.SetSourceData Source:=Range("FA_Month!$A$1:$D$15")
  • 提出一个新问题并在此处链接,我会看看它,但注释格式对于多行代码不容易阅读

标签: excel


【解决方案1】:

您知道如何在 Excel 中录制宏,因为这会为您生成 VBA 代码。网上有很多关于这个的信息。确实,“记录器”创建的代码应该进行相当大的编辑,再次在网络上提供帮助。

此外,作为了解更多有关 excel 和 VBA 的指南,您可以查看 ExcelIsFun you tube 频道,请参阅here

我建议在上面的代码中注意 使用 appExcel.ActiveWorkbook.Sheets("Detail_Report") .Cells.Font.Name = "新罗马时代" ETC ..... 以

结尾
With appExcel.ActiveWorkbook.Sheets("FA_Month")
     .Tab.Color = 92
     .Rows("1:1").RowHeight = 40
     etc ....
End with

而且您不需要使用“appExcel.ActiveWorkbook”。在表格前面

您也可以考虑使用嵌套的 With 语句:

With appExcel.ActiveWorkbook.Sheets("Detail_Report")
     .Cells.Font.Name = "Times New Roman"
     .Tab.Color = 92

    With .Rows("1:1")
         .RowHeight = 40
         etc...
    End With

end with

With appExcel.ActiveWorkbook.Sheets("FA_Month")
     .Tab.Color = 92

    With .Rows("1:1")
         .RowHeight = 40
         etc...
    End With

    With .cells
         .font.size = 24
         etc...
    End With

End with

哈维

【讨论】:

  • 感谢哈维的意见。只是想澄清你的最后评论。由于我有多张工作表,我必须... 使用 appExcel.ActiveWorkbook.Sheets("FA_Month") ... 以结尾
  • 感谢哈维的意见。只是想澄清你的最后评论。由于我有多张工作表,我必须... 以 appExcel.ActiveWorkbook.Sheets("FA_Month") ... 以 appExcel.ActiveWorkbook.Sheets("FA_Quarter") ... 以每个月的等结尾或在所有 5 张纸的末尾写上 End?我知道这个问题有点傻,是的,你是对的,网上有很多帖子,但所有帖子都认为格式相同,因此它们会循环——这就是我发疯的地方——因为我不想循环。我想以不同的方式对待每张纸。
  • 你的第一句话是对的。 “With”用于告诉 VBA 如何处理以“.”开头的任何代码。例如 .Tab.Color 的“.”被 VBA “转换”为最近的前一个 with 语句之后的文本。即appExcel.ActiveWorkbook.Sheets("FA_Month").Tab.Color
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多