In the VBA code below, every thing works perfectly up to the point where I have placed…
- User
In the VBA code below, every thing works perfectly up to the point where I have placed the comment ' CODE WORKS ACCURATELY UP TO THIS POINT. After this point, I can not get the code to copy the 'fileFound' to the destinationFolder. : Option Explicit
Sub MoveFileQuote(ByVal TargetCell As Range) Dim folderPath As String Dim fileNamePattern As String Dim destinationFolder As String Dim fileName As String Dim fileFound As Boolean Dim objShell As Object Dim objExplorer As Object Dim obj As Object
code' Define source and destination folders folderPath = ThisWorkbook.Path & "\" & "z Quotes On Hold" destinationFolder = ThisWorkbook.Path & "\" & TargetCell.Offset(0, -4).Value Set objShell = CreateObject("Shell.Application") Set objExplorer = objShell.Windows ' Open the source folder in Windows Explorer and confirm Shell "explorer.exe " & folderPath, vbNormalFocus Application.Wait (Now + TimeValue("0:00:02")) Dim confirmSource As VbMsgBoxResult confirmSource = MsgBox("Is the source folder correct?", vbYesNo + vbQuestion) ' If source folder is incorrect, exit the code If confirmSource = vbNo Then MsgBox "Please verify the source folder and run the code again.", vbInformation, "SOURCE" Exit Sub End If ' Open the destination folder in Windows Explorer and confirm Shell "explorer.exe " & destinationFolder, vbNormalFocus Application.Wait (Now + TimeValue("0:00:02")) Dim confirmDest As VbMsgBoxResult confirmDest = MsgBox("Is the destination folder correct?", vbYesNo + vbQuestion) ' If destination folder correct, proceed with file processing If confirmDest = vbNo Then MsgBox "Please verify the destination folder and run the code again.", vbInformation, "DESTINATION" Exit Sub End If fileNamePattern = "Quote " & TargetCell.Value & "*" fileFound = False Dim fso As Object Dim objFile As Object ' Display files found in the folder that match the fileNamePattern Dim filesFound As String - Assistant
The issue in your code is that you are trying to use the
fileFoundvariable as the file name to copy in the line:fso.CopyFile folderPath & "\" & fileFound, destinationFolderHowever,
fileFoundis a boolean variable, not the actual file name that you found earlier in the code.You should replace that line with the following code to copy the file that was found earlier:
vbIf fileFound Then For Each objFile In fso.GetFolder(folderPath).files If InStr(1, objFile.Name, TargetCell.Value, vbTextCompare) > 0 Then fso.CopyFile folderPath & "\" & objFile.Name, destinationFolder MsgBox objFile.Name & " moved successfully." End If Next objFile Else MsgBox "No files found in the folder that match the pattern." End IfThis code loops through all the files in the source folder, checks if the file name matches the pattern, and then copies the file to the destination folder.
- User
There is still nothing happening after where I placed the comment ’ CODE WORKS ACCURATELY UP TO THIS POINT. File is not moved, no Pop ups and no errors
- Assistant