2016-12-02 111 views
-3

有人可以合併這兩個功能 我試圖合併這兩個功能合併兩個VBA函數

第一功能

Function findimage(Path As String, ImageList As String) 
Dim results 
Dim x As Long 
Dim dc 'double comma 
results = Split(ImageList, ",") 
If Not Right(Path, 1) = "\" Then Path = Path & "\" 
For x = 0 To UBound(results) 
results(x) = Len(Dir(Path & results(x))) > 0 
Next 
dc = InStr(ImageList, ",,") 
If dc = 0 Then 
findimage = Join(results, ",") 
Else 
findimage = ("Double_comma") 
End If 
End Function 

謝勝利功能低於... 在此模式變量可以是從第一個函數的ImageList變量藉助這個...

Patterns = Mid(ImageList, 1, Application.WorksheetFunction.Search("_", ImageList, 1)) & "#.jpg" 

第二個函數是...

Function getFileCount(DirPath As String, ParamArray Patterns() As Variant) As Integer 
Dim MyFile As String 
Dim count As Integer, x As Long 
If Not Right(DirPath, 1) = "\" Then DirPath = DirPath & "\" 
MyFile = Dir(DirPath, vbDirectory) 
Do While MyFile <> "" 
For x = 0 To UBound(Patterns) 
If MyFile Like Patterns(x) Then 
count = count + 1 
Exit For 
End If 
Next 
MyFile = Dir() 
Loop 
getFileCount = count 
End Function 

回答

2
Function findimage(Path As String, ImageList As String) 
    Dim results 
    Dim x As Long 
    Dim dc 'double comma 
    Dim extension As String 
    results = Split(ImageList, ",") 
    If Not Right(Path, 1) = "\" Then Path = Path & "\" 
    For x = 0 To UBound(results) 
     If Len(Dir(Path & results(x))) > 0 Then 
      results(x) = True 
     Else 
      extension = Right(results(x), Len(results(x)) - InStrRev(results(x), ".")) 
      results(x) = "False(" & getFileCount(Path, "*." & extension) & ")" 
     End If 
    Next 
    dc = InStr(ImageList, ",,") 
    If dc = 0 Then 
     findimage = Join(results, ",") 
    Else 
     findimage = ("Double_comma") 
    End If 
End Function 

Function getFileCount(DirPath As String, ParamArray Patterns() As Variant) As Integer 
    Dim MyFile As String 
    Dim count As Integer, x As Long 
    If Not Right(DirPath, 1) = "\" Then DirPath = DirPath & "\" 
    MyFile = Dir(DirPath, vbDirectory) 
    Do While MyFile <> "" 
     For x = 0 To UBound(Patterns) 
      If MyFile Like Patterns(x) Then 
       count = count + 1 
       Exit For 
      End If 
     Next 
     MyFile = Dir() 
    Loop 
    getFileCount = count 
End Function