如何在Excel VBA中创建字符串数组并将其传递给sub?

时间:2014-12-31 01:23:26

标签: arrays excel vba excel-vba

VBA数组对我来说是新的,似乎有多种方法可以创建字符串数组。

我知道用户范围的数量需要在阵列中有多少项(所以我可能不需要动态数组?)。我将数组传递给另一个子程序时遇到了麻烦。

思考过程如下:

  1. 遍历用户名列表
  2. 为每个
  3. 创建一个工作表
  4. 在迭代
  5. 时将每个用户名保存在数组中
  6. 在另一个子例程中,选择我创建的所有工作表并另存为PDF
  7. 以下是我的代码。我得到运行时错误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
    

3 个答案:

答案 0 :(得分:2)

数组参数不是正确传递给方法CreatePDF(...)的问题。参数类型可以更改为SheetNames() As String,但SheetNames As Variant也可以。

然后Run-time error 9 - Subscript out of rangeThisWorkbook.Sheets(SheetNames).Select处被引发,因为数组SheetNames包含无效的工作表名称,这是第一项。此项为空字符串,空字符串无效作为工作表名称。

enter image description here

For Next循环索引中以值1开头,但数组以0开头。所以数组SheetNames的第一项保持不变,最后是一个空字符串。要解决此问题,请将ReDim中的下限明确设置为1。 HTH

(注意:如果省略下限,则使用Option Base,如果未指定Option Base,则使用0。)

'Cyle through users
For i = 1 To GetUserNameRange().Count
    'Some code
    ReDim Preserve SheetNames(1 To i)
    SheetNames(i) = UserNameRangeStart.Offset(i, 0).value
Next i

答案 1 :(得分:0)

我会改变这一点:
Sub CreatePDF(FileDate As Date, ByRef SheetNames As Variant)

对此:
Sub CreatePDF(FileDate As Date, SheetNames() As String)

但问题出在这一行:
ThisWorkbook.Sheets(SheetNames).Select

来自dee的评论: 您可以在.Sheets()中添加一系列工作表名称,但不带空行。所以在你的子" CreateAllDashboards"这样做:

ReDim Preserve SheetNames(i - 1)
SheetNames(i - 1) = UserNameRangeStart.Offset(i, 0).Value

您可以在VBA中阅读有关数组的that

答案 2 :(得分:-1)

我使用一个名为Users的范围和另一个名为FileDate的工作簿测试了以下内容。它做你所要求的。

Run-time error 9 - Subscript out of range错误的原因是您必须引用数组元素。 ThisWorkbook.Sheets(SheetNames).Select会抛出错误但ThisWorkbook.Sheets(SheetNames(x)).Select赢了(只要x被初始化并且在数组的范围内)

Sub PopulateArray()
Dim user As Range
Dim SheetNames As Variant

    ReDim SheetNames(1 To 1) 'Initialise the array
    For Each user In [Users]
        ThisWorkbook.Sheets.Add After:=Worksheets(Worksheets.Count)
        With Worksheets(Worksheets.Count)
            .Name = user.Value2
            .[A1] = user.Value2 'you can't print a blank sheet!
        End With
        SheetNames(UBound(SheetNames)) = user.Value2
        ReDim Preserve SheetNames(1 To UBound(SheetNames) + 1)
    Next user
    ReDim Preserve SheetNames(1 To UBound(SheetNames) - 1) 'Delete the last element
    Call CreatePDF([FileDate], SheetNames)
End Sub

Sub CreatePDF(FileDate As Date, ByRef SheetNames As Variant)

Dim FilePath As String, FileName As String
Dim x As Long

    FilePath = Application.ActiveWorkbook.Path & "\" 'Note backslash added to path.
    FileName = "Amtec Production Dashboards - " & Format(FileDate, "mmddyy")

    For x = 1 To UBound(SheetNames)
        ThisWorkbook.Sheets(SheetNames(x)).ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
            FileName & SheetNames(x) & ".pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, _
             IgnorePrintAreas:=False, OpenAfterPublish:=True
    Next x
End Sub

上面演示了如何根据请求将数组解析为另一个子,但是你也可以很容易地将CreatePDF代码集成到调用子中。