Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

match part of item with whole file name in directory

0

Hi

first  thanks  for John  what  provide  assistance for  the  members .

second I  modified  the  item in  column B 

I  need match  the  last part  of  item in  column B  with  file  name in directory within folders and subfolders.

for  instance when  click the  row in listbox   INVOICE PURCHASE PR_000001 should  open as long  the  last part of item in listbox for  selected row is  matched with  file  name .

so the  selected row contains PR_000001  and file  name is PR_000001  then is  matched  and  should  open  regardless contain INVOICE PURCHASE words in selected row in listbox.

I  no  know  what's exaclty  function  does  that.

thank 

Answer
Discuss

Answers

0
Selected Answer

Mussala

Revised Answer 27 July 2023

The first file ending with the last part of the ListBox selection (e,g. PR_000001) can be found by

  1. making the variable Sk become the last part of theListBox selection AFTER the last space ( & .pdf)
  2. changing the If test  to look at the end of the filename only, which would get PR_000001.pdf and Inv PR_000001 (say).

Changes are shown in bold in the code extract below:

Private Sub ListBox1_Click()

<< existting code>>
        ' set public file name to selection
        Sk = LCase(.List(.ListIndex, 1))
        ' trim to last part (after last space) & .pdf
        Sk = Right(Sk, Len(Sk) - InStrRev(Sk, " ")) & ".pdf"
        ' loop through top level folder
        For Each objSubFile In objFolder.Files
            'If LCase(objSubFile.Name) = Sk Then
            'see if end of filename matches
            If Right(objSubFile.Name, Len(Sk)) = Sk Then
                ' if found, write file path to found variable
                Fnd = objSubFile.Path
                'stop looking
                Exit For
            End If
        Next objSubFile
<< existing code>>

The If test in Function LoopEachFolder(fldFolder As Object, Sk As String) changes likewise.

I've done that in the attached revised file.

Hope this is what you meant in your question.

Discuss

Discussion

Hi john ,
I  think you  misunderstood !
the  file  name  will be PR_000001 , not INVOICE PURCHASE PR_000001
the  item in listbox will be INVOICE PURCHASE PR_000001 so  the  code  when  try  matching item in listbox with  file  name, then  should  search and match in directory  for PR_000001 and  then  should  open it. by  the  way  your  attachment  is different with  comparison  with my  new attachment, it   is  slight  different in column B for  two  sheets. thanks
Mussala (rep: 12) Jul 27, '23 at 6:30 pm
Mussala. I realised I made a mistake- please see my revised Answer and file
John_Ru (rep: 6142) Jul 27, '23 at 6:45 pm
Oops! Just replaced the new file (which had my test directory path in error)
John_Ru (rep: 6142) Jul 27, '23 at 6:54 pm
Excellent !
this  what I need  it.
thank  you  so  much .
Mussala (rep: 12) Jul 28, '23 at 6:51 am
Great! Thanks for selecting my Answer, Mussala
John_Ru (rep: 6142) Jul 28, '23 at 8:38 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login