Excel VBA查找数组中的最后一个条目(但不在电子表格中)

时间:2017-12-22 20:34:20

标签: arrays excel vba excel-vba excel-2016

我有一些Excel代码,它汇编了一个文件名数组,然后循环并从中提取一些数据。数据不在电子表格中 - 它完全在VBA中组装。每个月都会添加新文件,因此数量会有所不同。

我的问题是正在运行的代码不再有效,而我正试图找出一种解决方法。 (相关:Error: Microsoft Excel has stopped working - But I didn't change anything

UBound找到数组的大小。但是阵列并没有完全充满数据。如何找到数组中包含内容的最后一项?

我正在搜索并找到与查找电子表格中的项目数相关的答案,但这并不真正使用工作表。看起来似乎CountA可能就是我想要的,但是Excel: Find last value in an array没有一个我能想出来的例子。

换句话说,我想在下面的代码中使用除了UBound之外的东西,所以我不会过去那些包含其中某些内容的条目。

    FName = Array("april2010.xls", "feb2010.xls", "jan2010.xls", "july2010.xls", "june2010.xls", _
            "mar2010.xls", "may2010.xls", "sep2010.xls", "..\FINAL-MO-BAL-2011\APRIL2011.xls", _
            "..\FINAL-MO-BAL-2011\AUG2011.xls", "..\FINAL-MO-BAL-2011\DEC2011.xls", _
            "..\FINAL-MO-BAL-2011\FEB2011.xls", "..\FINAL-MO-BAL-2011\JAN2011.xls", _
            "..\FINAL-MO-BAL-2011\JULY2011.xls", "..\FINAL-MO-BAL-2011\JUNE2011.xls", _
            "..\FINAL-MO-BAL-2011\MARCH2011.xls", "..\FINAL-MO-BAL-2011\MAY2011.xls", _
            "..\FINAL-MO-BAL-2011\NOV2011.xls", "..\FINAL-MO-BAL-2011\OCT2011.xls", _
            "..\FINAL-MO-BAL-2011\SEP2011.xls", FName2, FName3, FName4, FName5, FName6, _
            FName7, FName8, FName9, FName10, FName11, FName12, FName13, FName14, FName15, _
            FName16, FName17, FName18, FName19, FName20, FName21, FName22, FName23, FName24, _
            FName25, FName26, FName27, FName28, FName29, FName30, FName31, FName32, FName33, _
            FName34, FName35, FName36, FName37, FName38, , FName39, FName40, FName41, FName42, _
            FName43, FName44, FName45, FName46, FName47, FName48, FName49)

If IsArray(FName) Then
    Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
    WorkbookName = ThisWorkbook.Name
    rnum = 1
    For Fnum = LBound(FName) To UBound(FName)
        Set mybook = Nothing
        On Error Resume Next
        Set mybook = Workbooks.Open(Filename:=FName(Fnum), ReadOnly:=True)

        With Application
            CalcMode = .Calculation
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
            .EnableEvents = False
            .CutCopyMode = False
            .DisplayAlerts = False
            .Visible = False
        End With

        On Error GoTo 0

        If Not mybook Is Nothing Then
            On Error Resume Next

3 个答案:

答案 0 :(得分:1)

使用Filter功能

Dim fName As Variant
Dim fltName As Variant

fName = Array("april2010.xls", "feb2010.xls", "jan2010.xls", "july2010.xls", "june2010.xls", _
            "mar2010.xls", "may2010.xls", "sep2010.xls", "..\FINAL-MO-BAL-2011\APRIL2011.xls", _
            "..\FINAL-MO-BAL-2011\AUG2011.xls", "..\FINAL-MO-BAL-2011\DEC2011.xls", _
            "..\FINAL-MO-BAL-2011\FEB2011.xls", "..\FINAL-MO-BAL-2011\JAN2011.xls", _
            "..\FINAL-MO-BAL-2011\JULY2011.xls", "..\FINAL-MO-BAL-2011\JUNE2011.xls", _
            "..\FINAL-MO-BAL-2011\MARCH2011.xls", "..\FINAL-MO-BAL-2011\MAY2011.xls", _
            "..\FINAL-MO-BAL-2011\NOV2011.xls", "..\FINAL-MO-BAL-2011\OCT2011.xls", _
            "..\FINAL-MO-BAL-2011\SEP2011.xls", "FName2", "FName3", "FName4", "FName5", "FName6", _
            "FName7", "FName8", "FName9", "FName10", "FName11", "FName12", "FName13", "FName14", "FName15", _
            "FName16", "FName17", "FName18", "FName19", "FName20", "FName21", "FName22", "FName23", "FName24", _
            "FName25", "FName26", "FName27", "FName28", "FName29", "FName30", "FName31", "FName32", "FName33", _
            "FName34", "FName35", "FName36", "FName37", "FName38", "", "FName39", "FName40", "FName41", "FName42", _
            "FName43", "FName44", "FName45", "FName46", "FName47", "FName48", "FName49")

fltName = Filter(fName, ".")

Debug.Print LBound(fltName), UBound(fltName)

我不确定FName2FName3等应该是什么。你说有数组元素没有数据,所以也许它们只是占位符来展示。

无论如何,我过滤了一个点,因此过滤掉了不是文件名的内容。您可能希望过滤不同的内容。

答案 1 :(得分:0)

在开始循环遍历remove数组之前,您可以执行类似的操作:

FName

然后将Dim LastFilled As Integer 'a variable to hold the last location filled in the array 'This is checking the array from the last to first For i = UBound(FName) To 1 Step -1 If FName(i) Is Not Empty Then LastFilled = i Exit For End If Next i 替换为UBound(FName)

答案 2 :(得分:0)

不要跳过错误,而是检查数组中的条目是否为空并且路径是否存在。

如果ChDir没有返回预期的目录,您可能还需要使用CurDir更改当前目录。

类似的东西:

Dim i As long, fname As String

' set the current directory with the directory of this workbook '
ChDir ThisWorkbook.Path

For i = LBound(FNames) To UBound(FNames)
    fname = FNames(i)

    ' if has entry '
    If Len(fname) Then

        ' if file exists '
        If Len(Dir(fname)) Then

            ' open workbook '
            Set wb = Workbooks.Open(Filename:=fname, ReadOnly:=True)


        End If
    End If
Next