In the VBA code below, if I confirm Yes to the folder location being correct, how do I…
- User
In the VBA code below, if I confirm Yes to the folder location being correct, how do I close the explorer window in each instance: 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
' 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 = "*" & TargetCell.Value & "*" fileFound = False Dim fso As Object Dim objFile As Object Set fso = CreateObject("Scripting.FileSystemObject") If fso.FolderExists(folderPath) And fso.FolderExists(destinationFolder) Then For Each objFile In fso.GetFolder(folderPath).files If InStr(1, objFile.Name, fileNamePattern, vbTextCompare) > 0 Then fileName = folderPath & "" & objFile.Name ' Perform file moving operations If Not fso.FileExists(destinationFolder & "" & objFile.Name) Then fso.CopyFile fileName, destinationFolder & "" & objFile.Name fso.DeleteFile fileName MsgBox "File moved successfully: " & fileName fileFound = True Exit For Else MsgBox "A file with the same name already exists in the destination folder." Exit For End If End If Next objFile Else MsgBox "Source or destination folder not found." End If If Not fileFound Then MsgBox "File not found matching the pattern." End IfEnd Sub
- Assistant
To close the explorer window in each instance, you can use the objExplorer object to iterate through all open explorer windows and close them. Here's how you can modify your code to achieve this:
' Close all open Windows Explorer instances For Each obj In objExplorer If TypeName(obj) = "Explorer" Then obj.Quit End If Next objYou can add this snippet of code after the confirmation of the destination folder is correct. This will loop through all open explorer windows and close them one by one. This way, you can ensure that the explorer windows opened during the execution of your VBA code are closed automatically.
- User
In my VBA code, I have a line that reads: fileNamePattern = "Quote " & TargetCell.Value & "*". After this line, I want to add a Msg that revals the files found in 'folderPath' that comfirm to the fileNamePattern
- Assistant
You can add a message box after setting the
fileNamePatternto display the files found in thefolderPaththat match thefileNamePattern. Here's how you can modify your code to achieve this:fileNamePattern = "Quote " & TargetCell.Value & "*" fileFound = False ' Display files found in the folder that match the fileNamePattern Dim filesFound As String For Each objFile In fso.GetFolder(folderPath).Files If InStr(1, objFile.Name, TargetCell.Value, vbTextCompare) > 0 Then filesFound = filesFound & vbCrLf & objFile.Name fileFound = True End If Next objFile If fileFound Then MsgBox "Files found in the folder that match the pattern:" & vbCrLf & filesFound Else MsgBox "No files found in the folder that match the pattern." End IfYou can add this piece of code after setting the
fileNamePatternvariable. It will loop through the files in thefolderPathand check if they match thefileNamePattern. It will then display a message box listing the files found in the folder that match the pattern. - User
Error: Variable not dwfinws - objFile
- Assistant
I apologize for the oversight. It seems like the
objFilevariable might not be declared in your VBA code. You can declare theobjFilevariable as an object before using it in the loop. Here's the updated code snippet:fileNamePattern = "Quote " & TargetCell.Value & "*" fileFound = False ' Display files found in the folder that match the fileNamePattern Dim objFile As Object Dim filesFound As String For Each objFile In fso.GetFolder(folderPath).Files If InStr(1, objFile.Name, TargetCell.Value, vbTextCompare) > 0 Then filesFound = filesFound & vbCrLf & objFile.Name fileFound = True End If Next objFile If fileFound Then MsgBox "Files found in the folder that match the pattern:" & vbCrLf & filesFound Else MsgBox "No files found in the folder that match the pattern." End IfBy adding the line
Dim objFile As Objectbefore the loop, you are declaring theobjFilevariable as an object, allowing it to be used within the loop to iterate through the files in the folder. This should resolve the error you encountered. - User
error: Object variable or with block variable not set, on the line - For Each objFile In fso.GetFolder(folderPath).files
- Assistant
The error "Object variable or With block variable not set" occurs when you try to use an object that hasn't been initialized or set to any specific object. In this case, you need to verify that the
fsoobject has been successfully created before using it to get the files from the folder.Here's an updated snippet of code that includes the initialization of the
fsoobject and checks if it is set properly:fileNamePattern = "Quote " & TargetCell.Value & "*" fileFound = False ' Display files found in the folder that match the fileNamePattern Dim fso As Object Dim objFile As Object Dim filesFound As String Set fso = CreateObject("Scripting.FileSystemObject") If Not fso Is Nothing Then For Each objFile In fso.GetFolder(folderPath).Files If InStr(1, objFile.Name, TargetCell.Value, vbTextCompare) > 0 Then filesFound = filesFound & vbCrLf & objFile.Name fileFound = True End If Next objFile If fileFound Then MsgBox "Files found in the folder that match the pattern:" & vbCrLf & filesFound Else MsgBox "No files found in the folder that match the pattern." End If Else MsgBox "Error creating FileSystemObject." End If