Friday, March 30, 2012

Return Array from Function

I have the following function that works fine, unless I add arguements.
When I add the arguments I get a Run-Time error '13': Type mismatch.
Any help would be greatly appreciated.
Option Base 1
Function FileSearch(Directory As String, Criteria As String)
Dim aFiles()
With Application.FileSearch
.NewSearch
.LookIn = Directory 'ActiveWorkbook.Worksheets("Control").Cells(3,
2) 'Directory
.SearchSubFolders = True
.Filename = Criteria '
ActiveWorkbook.Worksheets("Control").Cells(4, 2) 'Criteria
.Execute
ReDim aFiles(.FoundFiles.Count)
For i = LBound(aFiles) To UBound(aFiles)
aFiles(i) = .FoundFiles(i)
Debug.Print i, aFiles(i)
Next i
End With
'FileSearch = Application.WorksheetFunction.Transpose(aFiles)
FileSearch = aFiles
End Function
Does this have something do to with SQL Server? If not, you should consider
posting this in a VB forum.
Adam Machanic
Pro SQL Server 2005, available now
http://www.apress.com/book/bookDisplay.html?bID=457
"j...greig...davis@.gmail.com" <jgreigdavis@.gmail.com> wrote in message
news:1136905451.257413.287960@.g49g2000cwa.googlegr oups.com...
>I have the following function that works fine, unless I add arguements.
> When I add the arguments I get a Run-Time error '13': Type mismatch.
> Any help would be greatly appreciated.
> Option Base 1
> Function FileSearch(Directory As String, Criteria As String)
> Dim aFiles()
>
> With Application.FileSearch
> .NewSearch
> .LookIn = Directory 'ActiveWorkbook.Worksheets("Control").Cells(3,
> 2) 'Directory
> .SearchSubFolders = True
> .Filename = Criteria '
> ActiveWorkbook.Worksheets("Control").Cells(4, 2) 'Criteria
> .Execute
> ReDim aFiles(.FoundFiles.Count)
> For i = LBound(aFiles) To UBound(aFiles)
> aFiles(i) = .FoundFiles(i)
> Debug.Print i, aFiles(i)
> Next i
> End With
> 'FileSearch = Application.WorksheetFunction.Transpose(aFiles)
> FileSearch = aFiles
> End Function
>

No comments:

Post a Comment