从Access到Excel导出和格式化多个工作表

时间:2015-07-15 18:02:15

标签: ms-access

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

  1. 我成功设法导出我的报告但由于某些原因我的格式没有实现 - 请您告诉我原因并提出如何解决这个问题的想法。

  2. 除了我的报告,我还想添加图表。你能帮我理解我怎么做到这一点。

  3. ***到目前为止,我所做的是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
    

1 个答案:

答案 0 :(得分:0)

您是否知道在Excel中录制宏,因为这将为您生成VBA代码。网上有很多关于此的信息。实际上,“记录器”创建的代码应该进行相当大的编辑,同样在网络上也有帮助。

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

请注意以上代码我会推荐     使用appExcel.ActiveWorkbook.Sheets(“Detail_Report”)          .Cells.Font.Name =“Times New Roman”          等......     

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

并且您不需要使用“appExcel.ActiveWorkbook”。在Sheets面前

您也可以考虑使用嵌套的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

哈维

相关问题