Hi,
I'm trying to find a file in a specific folder. The file name consists of a unique variable and then some random numbers and will always be in .XLS format.
The unique variable can be found in the file from where the macro will be run.
The folder will be selected by the user via a dialog screen.
These are the declarations used in my code:
Dim wbIT As Workbook
Dim wbSAP As Workbook
Dim wbPGTL As Workbook
Dim DataFile As String
Dim LastRow As Long
Dim LastCol As Long
Dim LastRowIO As Long
Dim LastColIO As Long
Dim LastRowIOP As Long
Dim LastRowFY As Long
Dim i As Long
Dim w As Long
Dim x As Long
Dim y As Long
Dim z As Long
Dim Folder As String
Dim StrFile As String
Dim findrow As Range
Dim findrownumber As Long
Dim LastRowPGTL As Long
Dim LastRowPO As Long
Dim LastRowUnique As Long
Dim LastRowBewerking As Long
Dim LastRowUqPO As Long
Dim LastRowUqMat As Long
Dim LastRowUnq As Long
Dim LastRowUnq2 As Long
Dim FirstRowSum As Long
Dim LastRowSum As Long
Dim PotentialIssue As String
Dim rngDupl() As Range
Dim rngCell As Range
Dim rngCheck As Range
Dim lduplicates As Long
Dim myPath As String
Dim myFile As String
Dim fileType As String
The code itself (I have copied up to the point where all activities regarding the 'to be found' file stops):
LastRowIO = wbIT.Sheets("Invoice overview").Range("G" & Rows.Count).End(xlUp).Row
For x = 4 To LastRowIO
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Select Source Folder"
.AllowMultiSelect = False
.Show
myPath = .SelectedItems(1) & "\" & wbIT.Sheets("Invoice overview").Range("G" & x).Value & "*"
End With
fileType = "*.xls*"
myFile = Dir(myPath)
While (myFile <> "")
If InStr(myFile, wbIT.Sheets("Invoice overview").Range("G" & x).Value) > 0 Then
Set wbPGTL = Workbooks.Open(myFile)
End If
myFile = Dir
Wend
wbPGTL.Sheets(1).Range("A1:AL30000").Copy
wbIT.Sheets("PGTL").Range("A1").PasteSpecial
delay (10)
Application.DisplayAlerts = False
wbPGTL.Close SaveChanges:=False
Application.DisplayAlerts = True
With every try, the MyPath variable gets filled correctly, but when using the Dir(MyPath), the MyFile variable remains empty, which is strange as the file starting with that value is present in the selected folder (and unopened).
If somebody can assist me with this issue?
Should you require any clarification, I would be more than happy to provide any information I can give.
Thank you in advance!
What I have tried:
I have tried setting up the variables in different ways, adding wildcards via different variables, tried other folders, other files etc ... but never managed to get it rolling.