【发布时间】:2017-03-03 16:43:50
【问题描述】:
我有四个几乎相同,今天早上他们都工作了,现在他们没有了。
我真的很茫然。唯一不同的是我以外的人在运行它。
代码停在First = LBound(list)
将鼠标悬停在第一个上面显示“First = 0”
在LBound(list) 上面写着“LBound(list)= <Subscript out of range>”
最后是“Last = 0”
在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
【问题讨论】:
-
您是否尝试过将您的 BubbleSort() 代码直接移动到 Workbook_Open 中以查看是否存在解析问题?
-
我对它实际完成的工作有一个非常基本的了解,因为它是由其他人编写的。
-
我试图移动它,但我认为我遗漏了一些东西。
-
BubbleSort 只是按升序对解析到它的数组进行排序。 fileNames 数组默认按升序排列,无需对其进行排序。注释掉该行,您的 Workbook_Open 应该可以正常运行。