Can I pass in this list() array object into a private function, and then use the length property, will this result in an error?

Joined
Aug 6, 2014
Messages
11
Code:
Private Function FilterTheFileList(list() As Variant, t As String) As Variant

Dim sht As Worksheet
Dim c As Range
Dim wkBookInfo() As Variant
Dim filteredWkbInfo() As Variant
Dim filteredWksInfo() As Variant
Dim i As Long
Dim wksCnt As Long
Dim wkBookNum As Long
Dim confirmation As String
Dim fileName As String
'Dim workBookInfo As String

Application.ScreenUpdating = False
'according to my compiler error the error occurs here because apparently list isn't techincally an array...
wkBookNum = list.Length
wksCnt = 1
ReDim filteredWkbInfo(1 To wkBookNum)
For i = 1 To wkBookNum
    fileName = "labInventory" & list(wkBookNum)
    Workbooks.Open fileName
    For Each sht In ActiveWorkbook.Worksheets
        For Each c In sht.UsedRange.Rows(1).Cells
            If InStr(c.Value, t) > 0 Then
            filteredWksInfo(wksCnt) = ActiveWorkbook.Name + "" + sht.Name
            wksCnt = wksCnt + 1
            End If
        Next c
    Next sht
    filteredWkbInfo(i) = filteredWksInfo
Next i
If IsArray(filteredWkbInfo) Then
    ReDim Preserve filteredWkbInfo(1 To wkBookNum)
    'workBookInfo = Join(wkBookInfo, vbNullString)
    FilterTheFileList = filteredWkbInfo
Else
    Application.MsgBox ("There were no matches in any of the workbooks")
End If

End Function
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Please check if this does what you want:

Code:
Sub callfunc()
Dim list()
list = Array("\ActiveX.xls", "\Drawing.xls")
MsgBox UBound(FilterTheFileList(list, "greg")), vbInformation
End Sub

Private Function FilterTheFileList(list(), t$) As Variant
Dim sht As Worksheet, c As Range, wkBookInfo()
Dim filteredWkbInfo(), filteredWksInfo(), i As Long, wksCnt&
Dim wkBookNum As Long, fileName$
wkBookNum = UBound(list)
wksCnt = 1
ReDim filteredWkbInfo(0 To wkBookNum)
ReDim filteredWksInfo(1 To 1)
For i = 0 To wkBookNum
    fileName = ThisWorkbook.Path & list(i)
    Workbooks.Open fileName
    For Each sht In ActiveWorkbook.Worksheets
        For Each c In sht.UsedRange.Rows(1).Cells
            If InStr(c.Value, t) > 0 Then
            filteredWksInfo(wksCnt) = ActiveWorkbook.Name & " " & sht.Name
            wksCnt = wksCnt + 1
            ReDim Preserve filteredWksInfo(1 To wksCnt)
            End If
        Next c
    Next sht
    filteredWkbInfo(i) = filteredWksInfo
Next i
If IsArray(filteredWkbInfo) Then
    MsgBox "wksCnt = " & wksCnt
    FilterTheFileList = filteredWkbInfo
Else
    Application.MsgBox ("There were no matches in any of the workbooks")
End If
End Function
 
Upvote 0
VBA doesn't use length, to obtain the dimensions of an array use LBound and UBound.
 
Upvote 0

Forum statistics

Threads
1,216,743
Messages
6,132,455
Members
449,729
Latest member
davelevnt

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top