简体   繁体   English

下标超出范围错误9 vba

[英]Subscript out of range error 9 vba

I am getting a subscript out of range error when another user runs my add in but have no problems when running the same code myself. 当另一个用户运行我的添加时我得到一个超出范围错误的下标但是在我自己运行相同的代码时没有问题。 This happens when setting a workbook value. 设置工作簿值时会发生这种情况。 The filename is being generated by getting the current date and stored as gendate. 通过获取当前日期生成文件名并存储为gendate。 From this, the filename is created and saved based on the filepath that the user has made. 由此,根据用户创建的文件路径创建并保存文件名。 In this example, the value of gv.Range("b2").text is C:\\Users\\username\\Desktop\\ReportGeneration. 在此示例中,gv.Range(“b2”)。text的值为C:\\ Users \\ username \\ Desktop \\ ReportGeneration。 fp is therefore C:\\Users\\dmulhausen\\Desktop\\ReportGeneration\\TSReports9_6_201615h5m32s.xlsx This is not generating an error for me, but it is generating an error for another user of the script. 因此,fp是C:\\ Users \\ dmulhausen \\ Desktop \\ ReportGeneration \\ TSReports9_6_201615h5m32s.xlsx这不会为我生成错误,但它会为脚本的其他用户生成错误。

Dim ai As Workbook  'add in data ---Initialized in Report Setup
Dim dwb As Workbook 'destination workbook ---Initialized in Report Setup
Dim ss As Worksheet 'source sheet
Dim ds As Worksheet 'destination sheet or writing sheet
Dim rv As Worksheet 'reporting variables sheet ---Initialized in Report Setup
Dim pv As Worksheet 'ts variables sheet ---Initialized in Report Setup
Dim gv As Worksheet 'global ai variables ---Initialized in Report Setup
Dim tempstr As String
Dim fp As String 'file path ---Initialized in Report Setup
Dim gendate As Date
Dim reportscreated As Integer
Dim initialized As Boolean
Dim sheetnames(1 To 12) As String

Sub reportsetup()
    Set ai = Workbooks("TSReports add in.xlam")
    Set rv = ai.Worksheets("ReportVars")
    Set pv = ai.Worksheets("TS1_2Vars")
    Set gv = ai.Worksheets("globalVars")
    If (IsEmpty(gv.Range("b2").Value)) Then
        MsgBox ("Please select a designated folder for reports")
        With Application.FileDialog(msoFileDialogFolderPicker)
            .AllowMultiSelect = False
            .Show
            If .SelectedItems.Count > 0 Then
                gv.Range("b2").Value = .SelectedItems(1)
            End If
            ai.Save
        End With

    End If
    initialized = True
    gendate = Now()
    tempstr = "TSReports" & Month(gendate) & "_" & Day(gendate) & "_" & Year(gendate) & Hour(gendate) & "h" & Minute(gendate) & "m" & Second(gendate) & "s"
    fp = gv.Range("b2").Text & "\" & tempstr & ".xlsx"
    Workbooks.Add
    Application.DisplayAlerts = False
    Application.AlertBeforeOverwriting = False
    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs Filename:=fp
    Set dwb = Workbooks(tempstr) '*******Error occurs here*******

See: Windows().Activate works on every computer except one 请参阅: Windows()。激活适用于除一个以外的每台计算机

This should fix the issue. 这应该解决问题。

tempstr = "TSReports" & Month(gendate) & "_" & Day(gendate) & "_" & _
            Year(gendate) & Hour(gendate) & "h" & Minute(gendate) & "m" & _
            Second(gendate) & "s" & ".xlsx"

fp = gv.Range("b2").Text & "\" & tempstr 

Workbooks.Add
Application.DisplayAlerts = False
Application.AlertBeforeOverwriting = False
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:=fp
Set dwb = Workbooks(tempstr) 

However this would be more robust: 然而,这将更加强大:

Set dwb = Workbooks.Add
Application.DisplayAlerts = False
Application.AlertBeforeOverwriting = False
Application.DisplayAlerts = False
dwb.SaveAs Filename:=fp

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

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