【发布时间】:2014-12-31 01:23:26
【问题描述】:
VBA 数组对我来说是新的,似乎有多种方法可以创建字符串数组。
- 我相信我需要创建一个dynamic array
- 但我找不到任何如何将dynamic arrays 传递给子例程的示例
通过用户范围的计数,我知道数组中需要多少项(所以也许我不需要动态数组??)。我无法将数组传递给另一个子例程。
思考过程如下:
- 遍历用户名列表
- 为每个人创建一个工作表
- 在我迭代时将每个用户名保存在一个数组中
- 在另一个子例程中,选择我创建的所有工作表并保存为 PDF
下面是我的代码。我收到运行时错误 9 - 下标超出范围(指数组对象)
感谢您的帮助!谢谢!
Sub CreateAllDashboards(StartDate As Date, EndDate As Date)
'Used to iterate through the list of users and call the Sub to create Dashboards
Dim UserNameRangeStart As Range
Set UserNameRangeStart = Range("UserName")
Dim SheetNames() As String
'Cyle through users
For i = 1 To GetUserNameRange().CounT
'Some code
ReDim Preserve SheetNames(i)
SheetNames(i) = UserNameRangeStart.Offset(i, 0).Value
Next i
Call CreatePDF(EndDate, SheetNames) 'Also tried SheetNames()
End Sub
Sub CreatePDF(FileDate As Date, ByRef SheetNames As Variant)
Dim FilePath As String, FileName As String
FilePath = Application.ActiveWorkbook.Path
FileName = "Production Dashboards - " & Format(FileDate, "mmddyy") & ".pdf"
ThisWorkbook.Sheets(SheetNames).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
FileName, Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub
【问题讨论】: