简体   繁体   English

VBA Excel:在从模板创建的单独工作簿中使用动态数组

[英]vba excel: using dynamic array in seperated workbook which is created from template

I grabbed some information from my db, showed them in listbox in a userform and populated them in array named lsarr() which has dynamic rows and static columns. 我从数据库中获取了一些信息,以用户窗体的形式在列表框中显示了这些信息,并将其填充到名为lsarr()的数组中,该数组具有动态行和静态列。 Now I want to use this array in report workbook which is separately saved as in reports folder. 现在,我要在报表工作簿中使用此数组,该工作簿将另存为报表文件夹中。 firstly a template is prepared by dynamic rows of array then report is saved as to new workbook by specific name. 首先,由数组的动态行准备一个模板,然后按特定名称将报告保存到新工作簿中。 everything works fine until lsarr() wants to copied to a range in exported workbook. 一切正常,直到lsarr()想要复制到导出的工作簿中的某个范围。 When i call lsarr() i receive a message 当我致电lsarr()时,我收到一条消息

sub or function not defined 子或功能未定义

codes are as following: 代码如下:

Private Sub cusbas_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
...............
...............
lsarr = soldtable.List
...............
...............
end sub

this works fine 这很好

Private Sub sales2templ_Click()

    Dim outpath As String
    Dim curdate As String
    Dim repno As String
    Dim xl3 As Object
    Dim twb2, wb3 As Workbook
    Dim i, j, k As Integer

    Set xl3 = CreateObject("Excel.Application")
    xl3.Visible = True
    xl3.Workbooks.Open ActiveWorkbook.Path & "\Templates\Report-Sales.xltx"
    Set twb2 = xl3.ActiveWorkbook
    twb2.Sheets("Sales-Report").Activate
    twb2.Worksheets("Sales-Report").Range("C1").value = custlbl

        If totalinvpercust > 2 Then
                For i = 1 To totalinvpercust - 2
                    twb2.Sheets("Sales-Report").Range("A7:G7").EntireRow.Offset(1, 0).Insert
                Next i

                    For j = 1 To totalinvpercust
                    twb2.Sheets("Sales-Report").Range("A" & j + 6).value = j
                Next j

                For k = 0 To totalinvpercust - 1
                twb2.Sheets("Sales-Report").Range("B" & j + 7).value = lsarr(0, k)

                Next k

        Else
            twb2.Sheets("Sales-Report").Range("A7").value = 1
            twb2.Sheets("Sales-Report").Range("A8").value = 2
        End If

    'On Error Resume Next
    repno = frm_salesrev.cusbas.Text
    curdate = Format(Now(), "yyyymmddHhNnSs")
    outpath = ActiveWorkbook.Path & "\Reports\report" & "-" & repno & "-" & curdate & ".xlsx"
    twb2.SaveAs Filename:=outpath, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False, AccessMode:=xlExclusive

    xl3.ActiveWindow.WindowState = xlMaximized


     Set twb2 = Nothing
     Set xl3 = Nothing
     Set xl2 = Nothing
     curdate = ""
     outpath = ""
end sub

the problem occurs in 问题发生在

twb2.Sheets("Sales-Report").Range("B" & j + 7).value = lsarr(0, k)

* both subs are in the same module* *两个子都在同一个模块中*

Dim the variable outside of the subroutine to make it a form/module level variable instead like this: 在子例程外部调暗变量,使其成为表单/模块级变量,而不是像这样:

Dim lsarr() as Variant

Private Sub cusbas_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
  Redim lsarr(cuschk,6)

  ...............
  ...............
  lsarr = soldtable.List
  ...............
  ...............

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM