简体   繁体   English

下标超出范围运行时错误9

[英]Subscript out of range run time error 9

I am creating a function that reads column titles from two excel files and then place those column titles in checkboxes so the user can check which columns he will work with. 我正在创建一个函数,该函数从两个excel文件中读取列标题,然后将这些列标题放在复选框中,以便用户可以检查将使用的列。 It works when I do it with one file but when I modify it to work with two files I get the "run time error 9: Subscript out of range" and highlights the line => Set wks2 = ActiveWorkbook.Worksheets(SheetName2). 当我使用一个文件执行此操作时,它起作用,但是当我修改它以使用两个文件时,出现“运行时错误9:下标超出范围”,并突出显示了该行=> Set wks2 = ActiveWorkbook.Worksheets(SheetName2)。

Even with this error message still works for the first file but it does not work with the second file. 即使出现此错误消息,它仍然适用于第一个文件,但不适用于第二个文件。 Can anybody help me to find the reason of this error message? 有人可以帮助我找到此错误消息的原因吗? Thank you very much in advance. 提前非常感谢您。

 Function CallFunction(SheetName1 As Variant, SheetName2 As Variant) As Long
' This is a function used to retrieve column titles and place them as checkboxes in a listBox

 Dim jTitles(200) As String
 Dim sTitles(200) As String
 Dim titless As Integer
 Dim titlesj As Integer
 Dim wks1 As Worksheet
 Dim wks2 As Worksheet
 Dim Item(200) As String

 SPathName = Range("F18").Value
 SFilename = Range("F19").Value

 JPathName = Range("F22").Value
 JFilename = Range("F23").Value

 Workbooks.Open Filename:=SPathName & "\" & SFilename
 Workbooks.Open Filename:=JPathName & "\" & JFilename

 Set wks1 = ActiveWorkbook.Worksheets(SheetName1)

 For j = 1 To 199
     If Trim(wks1.Cells(4, j).Value) = "" Then
        titlesj = j - 1
        Exit For
    End If
        jTitles(j - 1) = wks1.Cells(4, j).Value
 Next

 j = 1

 ' Add column titles from files into the listbox as checkboxes
 For j = 0 To titlesj
    Sheet1.ListBox1.AddItem jTitles(j)
    Sheet1.ListBox3.AddItem jTitles(j)
 Next

 Set wks2 = ActiveWorkbook.Worksheets(SheetName2)  ' <=== HERE POPS THE ERROR MESSAGE

 For s = 1 To 199
    If Trim(wks2.Cells(1, s).Value) = "" Then
        titless = s - 1
        Exit For
    End If
        sTitles(s - 1) = wks2.Cells(1, j).Value
 Next

 s = 1

 For s = 0 To titless
    Sheet1.ListBox2.AddItem sTitles(s)
    Sheet1.ListBox4.AddItem sTitles(s)
 Next

    Workbooks(JFilename).Close
    ' Workbooks(SFilename).Close

End Function

Subscript out of Range error arises in these circumstances when the specified sheetname does not exist in that workbooks Worksheets collection. 在这种情况下,当该工作簿Worksheets集合中不存在指定的工作Worksheets时,将出现下标超出范围错误。

I notice you have two open workbooks specified by: 我注意到您有两个指定的打开的工作簿:

Workbooks.Open Filename:=SPathName & "\" & SFilename
Workbooks.Open Filename:=JPathName & "\" & JFilename

However, both of your worksheet assignments refer only to the ActiveWorkbook . 但是,您的两个工作表分配都仅引用ActiveWorkbook

The cause of the error is certainly that SheetName2 does not exist in the ActiveWorkbok (which is specified by JFilename ) 错误的原因肯定是SheetName2ActiveWorkbok中不存在(由JFilename指定)

Especially when working with multiple books or worksheets, it is always preferable to avoid using Activate/Select methods -- otherwise you need to keep track of which workbook/worksheet/etc. 尤其是在处理多本书或工作表时, 最好避免使用Activate / Select方法 -否则,您需要跟踪哪个工作簿/工作表/等。 is "Active", and that makes for spaghetti code and lots of unnecessary calls to the .Activate method. 是“活动的”,这将产生意粉代码以及对.Activate方法的许多不必要的调用。

I know that SheetName1 exists in JFilename , and I am assuming that SheetName2 exist in the workbook SFileName . 知道SheetName1存在于JFilename ,和我假设SheetName2工作簿中存在SFileName

Instead, define two Workbook variables: 而是,定义两个Workbook变量:

Dim wb1 as Workbook
Dim wb2 as Workbook

Assign the results of the Workbooks.Open method to these workbooks: Workbooks.Open方法的结果分配给这些工作簿:

Set wb2 = Workbooks.Open(Filename:=SPathName & "\" & SFilename)
Set wb1 = Workbooks.Open(Filename:=JPathName & "\" & JFilename)

Now, wb1 is the "Active" workbook, so with the worksheet assignments: 现在, wb1是“活动”工作簿,因此具有工作表分配:

Set wks1 = wb1.Worksheets(SheetName1)

And later for the Sheetname2 : 然后是Sheetname2

Set wks2 = wb2.Worksheets(Sheetname2)

Otherwise , there is a typo in your worksheet names or the string parameters you're sending to this function. 否则 ,您的工作表名称或要发送给此函数的字符串参数中会有错字。 Doublecheck/debug that the value of SheetName2 is correct and that it exists. SheetName2检查/调试SheetName2的值正确并且存在。

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

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