VBA - Open files from folder

Yulyo

Board Regular
Joined
Jul 17, 2017
Messages
94
Hello all,

I am searching for a vba code to open all xls files in a folder if the name of these files contains a specific text. It would be great to ask me when name i am searching, when run the vba.

I made one that opens all files, but...

Code:
Sub OpenFiles()
<code spellcheck="false" class="listtype-code listindent1 list-code1">Dim MyFolder As String</code>
<code spellcheck="false" class="listtype-code listindent1 list-code1">Dim MyFile As String</code>
<code spellcheck="false" class="listtype-code listindent1 list-code1">
</code>
<code spellcheck="false" class="listtype-code listindent1 list-code1">MyFolder = "C:\Users\xxx\Desktop\New folder\"</code>
<code spellcheck="false" class="listtype-code listindent1 list-code1">MyFile = Dir(MyFolder & "\*.xls")</code>
<code spellcheck="false" class="listtype-code listindent1 list-code1">
</code>
<code spellcheck="false" class="listtype-code listindent1 list-code1">Do Until MyFile = ""</code>
<code spellcheck="false" class="listtype-code listindent1 list-code1">    Workbooks.Open Filename:=MyFolder & "" & MyFile</code>
<code spellcheck="false" class="listtype-code listindent1 list-code1">    MyFile = Dir</code>
<code spellcheck="false" class="listtype-code listindent1 list-code1">Loop</code>
End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Perhaps something like this.
Code:
Sub OpenFiles()
Dim MyFolder As String
Dim MyFile As String
Dim MyName As String

    MyFolder = "C:\Users\xxx\Desktop\New folder\"
    MyName = InputBox("Please enter name to search for:")
    MyFile = Dir(MyFolder & "\*" & MyName & "*.xls")
    
    
    Do Until MyFile = ""
        Workbooks.Open Filename:=MyFolder & "" & MyFile
        MyFile = Dir
    Loop
    
End Sub
 
Upvote 0
Thank you for your answer Norie. It's working beautifully.
Is there any chance you could help me copy cell A5 and B5 from every workbook it opens, paste it on this workbook and then close all files that vba just opened?

Thank you in advance
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top