Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
VB
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):
VB
'Ask user to define PGTL folder
LastRowIO = wbIT.Sheets("Invoice overview").Range("G" & Rows.Count).End(xlUp).Row
For x = 4 To LastRowIO
    'Get target folder path from user
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Select Source Folder"
        .AllowMultiSelect = False
        .Show
    myPath = .SelectedItems(1) & "\" & wbIT.Sheets("Invoice overview").Range("G" & x).Value & "*"
    End With
    'Specify file type
    fileType = "*.xls*"
    'target Path with file type
    myFile = Dir(myPath)
    While (myFile <> "")
        If InStr(myFile, wbIT.Sheets("Invoice overview").Range("G" & x).Value) > 0 Then
            'Define PGTL workbook and open it
            Set wbPGTL = Workbooks.Open(myFile)
        End If
        myFile = Dir
    Wend
    'Copy to invoice tracking workbook
    wbPGTL.Sheets(1).Range("A1:AL30000").Copy
    wbIT.Sheets("PGTL").Range("A1").PasteSpecial
    delay (10)
    'Close PGTL workbook
    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.
Posted
Updated 21-May-19 1:26am
Comments
Richard MacCutchan 21-May-19 4:32am    
You need to run it under the macro debugger to see what is happening. It is impossible to guess what is wrong without running the code on the actual system where the files are.

1 solution

Issue resolved. Solution included adding some IF statements for 'error handling'.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900