简体   繁体   中英

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. 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. When i call lsarr() i receive a message

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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