Používam macro na vyhľadávanie súboru na disku v Excel 2007 priradené k tlačidlu na forme, po dokončení vyhľadávania by som potreboval nejako zresetovať premenné aby po opätovnom stlačení tlačidla zapisalo do listboxu na userform1 nové výsledky vyhľadania, takto mi zapíše len začiatok vyhľadávania a koniec vzhľadávania.
Za akúkoľvek pomoc ďakujem..

Kód: Vybrat vše
'Find a File on a Drive-Directory
'To search a directory or drive for a specified file or pattern use the following routine. Note, a test routine can be found at the bottom of the post.
Option Explicit
'Purpose : Performs a recursive search starting from the specified directory
' to find the next matching file (uses the file scripting object)
'Inputs : sInitialDirectory The directory to begin the seach from
' sFilePattern The file pattern to seach for eg. "*.xls"
'Outputs : Returns the full path and name of the next matching file
'Notes : Can be called recursively to find all instances of the specified file pattern
' Requires a Reference to SCRRUN.DLL ("Microsoft Scripting Runtime")
Function FileFindFirst(ByVal sInitialDirectory As String, ByVal sFilePattern As String, Optional Reset As Boolean) As String
Static FSO As Scripting.FileSystemObject, oDirectory As Scripting.Folder, oThisDir As Scripting.Folder
Static ssLastPattern As String, ssLastFiles As String
Dim sThisPath As String, sResString As String, sTestFile As String
If (FSO Is Nothing) = True Then
Set FSO = New Scripting.FileSystemObject
End If
If Right$(sInitialDirectory, 1) <> "\" Then
sInitialDirectory = sInitialDirectory & "\"
End If
'Seach current directory
sThisPath = sInitialDirectory
sTestFile = Dir$(sThisPath & sFilePattern)
Do
If FileExists(sThisPath & sTestFile) Then
If InStr(1, ssLastFiles, "|" & sThisPath & sTestFile) = 0 Then
'Found next matching file
sResString = sThisPath & sTestFile
Exit Do
End If
Else
'No more matching files in this directory
Exit Do
End If
'Get next matching file
sTestFile = Dir$
Loop
If Len(sResString) = 0 Then
'File not found in sInitialDirectory, search sub directories...
Set oDirectory = FSO.GetFolder(sInitialDirectory)
For Each oThisDir In oDirectory.SubFolders
sThisPath = oThisDir.Path
If Right$(sThisPath, 1) <> "\" Then
sThisPath = sThisPath & "\"
End If
On Error Resume Next
sTestFile = Dir$(sThisPath & sFilePattern)
Do
If FileExists(sTestFile) Then
If InStr(1, ssLastFiles, "|" & sThisPath & sTestFile) = 0 Then
'Found next matching file
sResString = sInitialDirectory & sTestFile
End If
Else
'No more matching files in this directory, check it's subfolders
sTestFile = FileFindFirst(sThisPath, sFilePattern)
If FileExists(sTestFile) Then
If InStr(1, ssLastFiles, "|" & sThisPath & sTestFile) = 0 Then
'Found next matching file
sResString = sTestFile
Exit Do
End If
Else
'File not found in sub folder
Exit Do
End If
End If
sTestFile = Dir$
Loop
If Len(sResString) Then
'Found next matching file
Exit For
End If
Next
End If
If Len(sResString) Then
'Store search parameters
If sFilePattern = ssLastPattern Then
'Routine has been called with same parameters, store all previously matching files
ssLastFiles = ssLastFiles & "|" & sResString
Else
'Store matching file
ssLastFiles = "|" & sResString
End If
ssLastPattern = sFilePattern
'Return result
FileFindFirst = sResString
End If
End Function
'Purpose : Checks if a file exists
'Inputs : sFilePathName The path and file name e.g. "C:\Autoexec.bat"
'Outputs : Returns True if the file exists
Function FileExists(sFilePathName As String) As Boolean
On Error GoTo ExitFunction
If Len(sFilePathName) Then
If (GetAttr(sFilePathName) And vbDirectory) < 1 Then
'File Exists
FileExists = True
End If
End If
ExitFunction:
End Function
'Demonstration Routine
Sub Test1(Optional Reset As Boolean)
Dim sFile As String
Dim Hfile As String
UserForm1.ListBox1.Clear
Hfile = InputBox("Zadajte hladaný výraz.", "NÁZOV SÚBORU:")
If Hfile = vbNullString Then Exit Sub
'Find all instances
UserForm1.ListBox1.AddItem "Vyhľadávam ----------"
Debug.Print "Vyhľadávam ----------"
Do
sFile = FileFindFirst("c:\", Hfile)
If Len(sFile) Then
'Debug.Print sFile
UserForm1.ListBox1.AddItem (sFile)
UserForm1.Repaint
Else
Debug.Print "--------END OF SEARCH"
UserForm1.ListBox1.AddItem "Koniec vyhľadávania--------"
sFile = vbNullString
Exit Do
End If
Loop
End Sub