Hello.
I am quite new here in this forum and I hope we can help each other.
I recently have a hard time in making a code focusing on searching a file in a folder and if possible to open it.
Please help me on the codings.
Thanks guys.
Hello.
I am quite new here in this forum and I hope we can help each other.
I recently have a hard time in making a code focusing on searching a file in a folder and if possible to open it.
Please help me on the codings.
Thanks guys.
See next code
Adjust
to your file extension![]()
.Filters.Add "Classeurs Excel", "*.xlsx"
![]()
Option Explicit Sub Search_File_Picker2() Dim WkFile As Workbook Dim WS As Worksheet With Application.FileDialog(msoFileDialogFilePicker) .Title = "Choisir le fichier Excel" .AllowMultiSelect = False .InitialFileName = "Fichier_Par_Defaut.xlsx" .Filters.Add "Classeurs Excel", "*.xlsx" .FilterIndex = 1 .InitialFileName = ActiveWorkbook.Path ' Select present folder .Show If .SelectedItems.Count > 0 Then Set WkFile = Workbooks.Open(.SelectedItems(1)) Set WS = WkFile.Sheets(1) Else MsgBox "No file selected", , "INFORMATION" Exit Sub End If End With End Sub
- Battle without fear gives no glory - Just try
what if I will add a search in which if something is type there, that words will be search as folder file names
Administrative Note:
Welcome to the forum.
Is your forum profile up-to-date and showing ONLY the oldest Excel PRODUCT that you need this to work for?
Members will tailor the solutions they offer to the Office PRODUCT (Excel, NOT Windows) that you have. Please check that your forum profile is up-to-date in this respect. If you aren't sure, in Excel go to File | Account and report what it says below the MS logo at the top of that page. If your product is for Mac, please also state this.
The four most recent Excel products are Excel 2019, Excel 2021, Excel 2024 and MS365 - if you are using MS365, please give this name along with the version number in your profile (e.g. MS365 Version 2306). This is in the About Excel section further down the Account page.
Thanks.
Ali
Enthusiastic self-taught user of MS Excel who's always learning!
Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.
NB: as a Moderator, I never accept friendship requests.
Forum Rules (updated August 2023): please read them here.
Thank you. I have already update my infos
Maybe this will get you started.
Change directory path in A1. Press button and type keyword in TextBox. Enter or Tab.
Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.
The code works but when I try to open the file a run time error occured.
Just click on a name in the ListBox to open the file because I'm not getting any errors.
FORUM.png
Please see picture for the error. I'm not well familiar onto what i need todo next
Which code you were using ?
HI PCI,
I am currently using Bakermans code.
I try to use your code, but I have a question regarding what if I will add a search in which if something is type there, that words will be search as folder file names
Please help guys, I'm not familiar on this error and on what to do
Did you try it on a different directory ?
Try this also.
![]()
Private Sub ListBox1_Click() Dim mybook As Workbook With ListBox1 If .ListIndex > -1 Then Set mybook = Workbooks.Open(myDir & .List(.ListIndex, 0)) End If End With End Sub
Same error as the first error i have screenshot
Show image of folder myDir (Sheet1.Range("A1").Value). This is C:\Users\cfederiz\OneDrive - DPDHL\Desktop\?
Show image of ListBox1 with file names.
I am currently have this code
![]()
Dim myDir As String Private Sub UserForm_Initialize() myDir = Sheet1.Range("A1").Value End Sub Private Sub TextBox1_AfterUpdate() textsearch = TextBox1.Text sn = Split(CreateObject("wscript.shell").exec("cmd /c dir """ & myDir & "\*" & textsearch & "*.*"" /b").StdOut.ReadAll, vbCrLf) ListBox1.List = sn End Sub Private Sub ListBox1_Click() Dim mybook As Workbook With ListBox1 If .ListIndex > -1 Then Set mybook = Workbooks.Open(myDir & .List(.ListIndex, 0)) End If End With End Sub
OK, but did you try on a different directory because on my part files open without a problem.
Want to exclude the fact that it might have something to do with the filepath.
We know which code you use.
Please answer the questions asked to you.
Have you tried another directory ? Show screenshots of cell A1 with filepath. Show screenshot of ListBox1 with filenames.
We ask all of these questions because you seem to be the only one who has problems with getting the code to work properly.
Here it is.
Attachment 894195Attachment 894196
Invalid attachment posted.
Also this is the 3rd time I ask you if you have tried another directory. Please do and reply.
Oh I know where my mistake is. just like on all what you are pointing it is on my directory.
I overlooked the Backslash, my mistake. Thank you for all your help.
You're welcome and thanks for rep+.![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks