简体   繁体   English

LBound和UBound下标超出了已经工作了三年的阵列

[英]LBound and UBound Subscript out of range on an array that has worked for three years

I have four of these almost identical to each other, they all worked this morning, now they don't. 我有四个这几个几乎相同,今天早上他们都工作,现在他们没有。

I'm really at a loss. 我真的很茫然。 The only thing different was that someone other than me ran it. 唯一不同的是,除了我以外的其他人。

Code stops at First = LBound(list) Hovering over First it reads " First = 0 " Over LBound(list) it reads " LBound(list)= <Subscript out of range> " Over Last it reads " Last = 0 " Over UBound(list) it reads " UBound(list = <Subscript out of range> " 代码停在First = LBound(list)悬停在First上它读取“ First = 0 ”Over LBound(list)它读取“ LBound(list)= <Subscript out of range> ”Over Last它读取“ Last = 0 ”Over UBound(list)它读取“ UBound(list = <Subscript out of range> ”)

Option Explicit
Private Sub Workbook_Open()

ActiveSheet.Unprotect Password:="Operator"

MsgBox "This will compile all the operator rounds in the Fire Pump Folder. Enjoy!" & vbNewLine & "Make Sure Your Macros Are Enabled."

Dim fPATH As String, fNAME As String
Dim LR As Long, NR As Long
Dim wbGRP As Workbook, wsDEST As Worksheet
Dim fileNames() As String, i As Long


Set wsDEST = ThisWorkbook.Sheets("Summary")
NR = wsDEST.Range("B" & Rows.Count).End(xlUp).Row + 1

fPATH = "\\SMRT01-FPS-15\plant_information\Operator_Required_Rounds\FirePump\"       'remember the final \ in this string

fNAME = Dir(fPATH & "*.xls")        'get the first filename in fpath
i = 0
Do While Len(fNAME) > 0
        ReDim Preserve fileNames(i)
        fileNames(i) = fNAME
        i = i + 1
        fNAME = Dir
    Loop

If i >= 0 Then

    BubbleSort fileNames
    For i = 0 To UBound(fileNames)
        Set wbGRP = Workbooks.Open(fPATH & fileNames(i))   'open the file
        LR = wbGRP.Sheets("Fire Pump (Monday)").Range("B" & Rows.Count).End(xlUp).Row  'how many rows of info?
    If LR > 3 Then
        wsDEST.Range("A" & NR) = Replace(Range("A1"), "Group ", "")
        wbGRP.Sheets("Fire Pump (Monday)").Range("B3:F" & LR).Copy
        wsDEST.Range("B" & NR).PasteSpecial xlPasteAll
        NR = wsDEST.Range("B" & Rows.Count).End(xlUp).Row + 1
    End If

    wbGRP.Close False   'close data workbook
Next

Range("A3:A" & NR - 1).SpecialCells(xlBlanks).FormulaR1C1 = "=R[-1]C"
With Range("A3:A" & NR - 1)
    .Value = .Value
End With
 Else

        'fileNames array is empty
        MsgBox "No .xls files found in " & fPATH
End If

End Sub
Sub BubbleSort(list() As String)
'   Sorts an array using bubble sort algorithm
    Dim First As Integer, Last As Long
    Dim i As Long, j As Long
    Dim Temp

    First = LBound(list)
    Last = UBound(list)
    For i = First To Last - 1
        For j = i + 1 To Last
            If list(i) > list(j) Then
                 Temp = list(j)
                list(j) = list(i)
                list(i) = Temp
            End If
        Next j
    Next i
End Sub

The problem is in the BubbleSort declaration: 问题出在BubbleSort声明中:

Sub BubbleSort(list() As String)

Which is saying to treat list() as a String type variable whereas an array is typically of type Variant. 这就是将list()视为String类型变量,而数组通常是Variant类型。 A Variant can hold a String or an array of Strings - a String can never hold an array and therefore won't have a Ubound. Variant可以包含String或Strings数组 - String永远不能保存数组,因此不会有Ubound。

Changing the BubbleSort declaration to: 将BubbleSort声明更改为:

Sub BubbleSort(list as Variant)

will work! 将工作!

That said, elements of the fileNames() array, read into it using Dir , will be in ascending order anyway. 也就是说,使用Dir读入的fileNames()数组元素无论如何都将按升序排列。 Sorting fileNames() serves no purpose in this context and sorting them on every loop pass probably makes your loop imperceptibly slower. 排序fileNames()在此上下文中没有用处,并且在每个循环传递上对它们进行排序可能会使您的循环在不知不觉中变慢。

If you still want to use BubbleSort, move it to before entering the loop so that it's only called once. 如果您仍想使用BubbleSort,请在进入循环之前将其移至,以便仅调用一次。

Based on your problem description and the code logic, I believe your issue is that 根据您的问题描述和代码逻辑,我相信您的问题就在于此
fNAME = Dir(fPATH & "*.xls")
is returning an empty string. 正在返回一个空字符串。

From Excel's built-in help: 从Excel的内置帮助:

Dir returns the first file name that matches pathname. Dir返回与路径名匹配的第一个文件名。 To get any additional file names that match pathname, call Dir again with no arguments. 要获取与路径名匹配的任何其他文件名,请再次调用Dir,不带参数。 When no more file names match, Dir returns a zero-length string (""). 当没有更多文件名匹配时,Dir返回零长度字符串(“”)。 Once a zero-length string is returned, you must specify pathname in subsequent calls or an error occurs. 返回零长度字符串后,必须在后续调用中指定pathname或发生错误。

While the documentation does not explicitly state that calling Dir with a non-existent pathname will return an empty string, it is implied by the phrase "no more file names match". 虽然文档没有明确声明使用不存在的路径名调用Dir将返回一个空字符串,但短语“不再存在文件名匹配”暗示了这一点。

Because of this, the array fileNames is left in an un-dimensioned state. 因此,数组fileNames处于未维度状态。 An un-dimensioned array will result in the error you are observing from the LBound and UBound functions. 未定义的数组将导致您从LBoundUBound函数中观察到的错误。 Because of faulty code logic, the BubbleSort is being called with nothing to sort. 由于错误的代码逻辑,正在调用BubbleSort ,无需排序。

Change the following statements: 更改以下声明:

i = 0
Do While Len(fNAME) > 0
   ReDim Preserve fileNames(i)
   fileNames(i) = fNAME
   i = i + 1
   fNAME = Dir
Loop

to: 至:

fNAME = Dir(fPATH & "*.xls")        'get the first filename in fpath
i = -1
Do While Len(fNAME) > 0
   i = i + 1
   ReDim Preserve fileNames(i)
   fileNames(i) = fNAME
   fNAME = Dir
Loop

This will allow the remainder of the code logic to function properly because the variable i will be -1 if no files are found when the following statement is evaluated. 这将允许代码逻辑的其余部分正常运行,因为如果在评估以下语句时没有找到文件,则变量i将为-1。
If i >= 0 Then

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

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