【问题标题】:Build Excel from visual ms access from function从函数的可视 ms 访问构建 Excel
【发布时间】:2020-07-12 18:27:55
【问题描述】:

我正在尝试使用以下代码从 MS-Access 的 Visual Basic 6 在 Excel 中构建一个文档,该代码调用函数 TraeDatosCorteAnterior,该函数在数据库中执行查询,生成 Excel 应用程序并添加一个工作表。但是,通过继续从函数 ** ConstruyeTablayCampo ** 生成文档,应用错误 424 并显示以下消息 **“需要一个对象”**:

** Excel 对象声明 **

Option Compare Database
Dim ba As Excel.Application

初始功能

  Public Sub TraeDatosCorteAnterior()

    RstName = "SELECT conc.CP, conc.etapa, conc.total FROM t_conclusion as conc;"
    Dim dbs As Database
    Set dbs = CurrentDb
    Dim iRec As Long
    Dim i As Long
    Dim RstOrig As Recordset
    Dim nTotalReg As Integer
    Dim nCP As Integer
    Dim nEtapa As Integer 
    Dim nTotal As Integer

    Set RstOrig = CurrentDb.OpenRecordset(RstName, dbOpenDynaset)
    RstOrig.Sort = fldName
    Dim RstSorted As Recordset
    Set RstSorted = RstOrig.OpenRecordset()
    RstOrig.Sort = fldName
    Dim RstSorted As Recordset
    Set RstSorted = RstOrig.OpenRecordset()
    RstSorted.MoveLast
    RstSorted.MoveFirst

    nTotalReg = RstSorted.RecordCount

    nCP = RstSorted("CP")

    nEtapa = IIf(IsNull(RstSorted("etapa")) = True, 0, RstSorted("etapa"))
    nTotal = IIf(IsNull(RstSorted("Total")) = True, 0, RstSorted("Total"))

    RstSorted.MoveNext


    Set ba = New Excel.Application
    Set xlwbook = ba.Workbooks.Add
    ba.Visible = True

    ConstruyeTablayCampo "Cuadro I", "Cuenta Pública", "A", "A", ba, False, 1

  End Sub

** 构建文档的函数**,从第一行开始标记错误。

** Excel 对象声明 **

 Sub ConstruyeTablayCampo(ByRef sHoja As String, ByRef sTexto As String, ByRef col1 As String, ByRef col2 As String, ByRef ba As Excel.Application, Optional bCreaHoja As Boolean, Optional nHoja As Integer)

        Set xlsheet = xlwbook.Sheets.Item(nHoja)
        xlwbook.Sheets.Item(nHoja).Name = sHoja

        xlsheet.Cells.Range("A11:A11") = sTexto
        Worksheets("Cuadro I").Range("B:B,D:D,H:H,J:J,N:N,Q:Q").ColumnWidth = 1

        With xlsheet.Cells.Range(col1 & "11:" & col2 & "12")
                .HorizontalAlignment = xlCenter
                .VerticalAlignment = xlCenter
                .WrapText = True
                .Select
                .Font.Bold = True
                .MergeCells = True
                .Interior.Color = RGB(244, 244, 244)
        End With

End Sub

先谢谢了,希望你能帮助我!

【问题讨论】:

  • 我认为您的意思不是用于独立程序的 VB6,而是 VBA。
  • xlwbook 在哪里声明?首先在每个模块的顶部添加Option Explicit,然后修复任何标记的内容。
  • xlSheet 也没有声明。

标签: excel vba ms-access


【解决方案1】:

终于解决了,解决办法是这样的:

首先声明这个

Option Compare Database

Dim ba As Excel.Application
Dim xlwsheet As Excel.Worksheet

第二行初始化对象:

 Sub ConstruyeTablayCampo(ByRef sHoja As String, ByRef sTexto As String, ByRef col1 As String, ByRef sRango1 As String, ByRef col2 As String, ByRef sRango2 As String, ByRef ba As Excel.Application, ByRef color1 As String, ByRef color2 As String, ByRef color3 As String, ByRef nHoja As Integer)

        Set xlwsheet = ba.Worksheets.Item(nHoja)
        xlwsheet.Name = sHoja
        xlwsheet.Range("B:B,D:D,H:H,J:J,N:N,Q:Q").ColumnWidth = 1

        With xlwsheet.Cells.Range(col1 & "11" & ":" & col2 & "12")
                .HorizontalAlignment = xlCenter
                .VerticalAlignment = xlCenter
                .WrapText = True
                .Select
                .Font.Bold = True
                .MergeCells = True
                .Interior.Color = RGB(color1, color2, color3)
        End With

        xlwsheet.Range(col1 & "11:" & col2 & "12").Cells = sTexto

End Sub

感谢您的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多