Let me start off with I know very little about VBA, but I've been tasked at work to learn more. I've bought a few books, so I'm in the process but still not enough to fully understand in depth code.
I have an upload file that when a user presses the button, a menu comes up for them to select a folder, and then all files in that folder that being with a 6-digit number are copied into the original upload file. Well this process used FileSearch, so when the user upgraded Office, the import no longer worked. In researching, I read that it is best to replace the no longer available FileSearch with a FileSystemObject; however, I am still getting an error and not sure what I've done incorrect.
This is the import code that I think is the only thing that needs corrections/causing errors:
Here is some other code in the same worksheet that may be affecting this one. I'm not entirely sure at the moment.
I really wish I could figure this out better myself, but until I actually learn VB, I'm stuck.
I will say, I had tried adding a Dir function instead of the FileSystemObject which ran the code with no errors, but it didn't find any files that I know exist.
I have an upload file that when a user presses the button, a menu comes up for them to select a folder, and then all files in that folder that being with a 6-digit number are copied into the original upload file. Well this process used FileSearch, so when the user upgraded Office, the import no longer worked. In researching, I read that it is best to replace the no longer available FileSearch with a FileSystemObject; however, I am still getting an error and not sure what I've done incorrect.
This is the import code that I think is the only thing that needs corrections/causing errors:
Code:
'Imports Worksheets from the passed folder path, matching the passed pattern.
Private Sub Import_Worksheets(FilePath As String, sPattern As String)
Dim FS As Object
Dim FileSpec As String
Dim Index As Integer
Dim sPath As String
Dim tWorkbook As Workbook
Dim tWorksheet As Worksheet
Dim bFoundDepts As Boolean
On Error GoTo ErrorHandler:
'Toggle off screen refreshing and application level events
StealthMode True
'Specify path and file spec
FileSpec = "*.xls"
'Create a FileSearch object
Set FS = CreateObject("Scripting.FileSystemObject")
With FS
.LookIn = FilePath
.Filename = FileSpec
.Execute
'Exit if no files are found
If FS.FoundFiles.Count = 0 Then
MsgBox "No files were found", vbOKOnly, "No Files"
Exit Sub
End If
'Loop through the files and process them
For Index = 1 To .FoundFiles.Count
If Get_FileName(FS.FoundFiles.Item(Index)) <> ThisWorkbook.Name Then
Workbooks.Open FS.FoundFiles.Item(Index)
For Each tWorkbook In Workbooks
If tWorkbook.path = FilePath Then
'Copy the department sheet to ThisWorkbook
For Each tWorksheet In tWorkbook.Worksheets
If IsLike(tWorksheet.Name, sPattern) = True Then
'Copy the worksheet with a number for the name to ThisWorkbook
tWorksheet.Copy Before:=ThisWorkbook.Sheets(1)
'We found at least one department in the folder
bFoundDepts = True
End If
Next tWorksheet
tWorkbook.Close
End If
Next tWorkbook
End If
Next Index
End With
'Notify the user that the departments were successfully imported
If bFoundDepts = True Then
MsgBox "Departments Imported", vbOKOnly, "Import Departments"
Else
MsgBox "In the folder you selected, no department sheets could be found for any workbook.", vbOKOnly, "No Departments Found"
End If
'Toggle on screen refreshing and application level events
StealthMode True
Exit Sub
ErrorHandler:
MsgBox Err.Description
Err.Clear
End Sub
Here is some other code in the same worksheet that may be affecting this one. I'm not entirely sure at the moment.
Code:
Option Explicit
Private Type BROWSEINFO ' used by the function GetFolderName
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
Private Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
Private Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
Function GetFolderName(Msg As String) As String
' returns the name of the folder selected by the user
Dim bInfo As BROWSEINFO, path As String, r As Long
Dim X As Long, pos As Integer
On Error GoTo ErrorHandler:
bInfo.pidlRoot = 0& ' Root folder = Desktop
If IsMissing(Msg) Then
bInfo.lpszTitle = "Select a folder."
' the dialog title
Else
bInfo.lpszTitle = Msg ' the dialog title
End If
bInfo.ulFlags = &H1 ' Type of directory to return
X = SHBrowseForFolder(bInfo) ' display the dialog
' Parse the result
path = Space$(512)
r = SHGetPathFromIDList(ByVal X, ByVal path)
If r Then
pos = InStr(path, Chr$(0))
GetFolderName = Left(path, pos - 1)
Else
GetFolderName = ""
End If
Exit Function
ErrorHandler:
MsgBox Err.Description
Err.Clear
End Function
Private Function Get_Folder() As String
Dim FolderName As String
On Error GoTo ErrorHandler:
FolderName = GetFolderName("Select a folder containing Excel workbooks with Department tabs, and click the OK button below.")
If FolderName <> "" Then
Get_Folder = FolderName
End If
Exit Function
ErrorHandler:
MsgBox Err.Description
Err.Clear
End Function
Private Function Get_FileName(sPath As String) As String
On Error GoTo ErrorHandler:
Get_FileName = Split(sPath, "\")(UBound(Split(sPath, "\")))
Exit Function
ErrorHandler:
MsgBox Err.Description
Err.Clear
End Function
I really wish I could figure this out better myself, but until I actually learn VB, I'm stuck.
I will say, I had tried adding a Dir function instead of the FileSystemObject which ran the code with no errors, but it didn't find any files that I know exist.