File Search and Select capability in Excel2000

m_lny

New Member
Joined
Feb 24, 2005
Messages
34
I would like to provide a button on my spreadsheet to allow the user to search down thru folders on the c drive to select a file somewhere on the computer. I'm not sure how to reference it, but it should work like "windows explorer" where you can click on a folder to open - then maybe a sub folder until you find the file you would like to select.
The purpose is to import a text file into excel without using a macro, and import text files with different names ( one at a time ).
I think I can mke it work after selecting the fle, but need help in making that selection possible. I have seen references to comdlg32 and msofilepicker but can't do anything with these.

any help will be appreciated

Thank you
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

jindon

MrExcel MVP
Joined
Aug 21, 2004
Messages
16,995
try
Code:
Sub test()
Dim myDir As String, myFileName As String, x As String
myDir = "c:\temp"
myFileName = "myText.txt"
If Dir(myDir, vbDirectory) = "" Then
     MsgBox "Directory is not exist"
     Exit Sub
End If
x = SearchFile(myDir,myFileName)
If x <> "" Then
     ChDir x
     Application.Dialogs(1).Show
End If
End Sub

Function SearchFile(myDir As String, myFileName As String) As String
Dim fso As Object, myFile As Object, myFolder As Object
Set fso = CreateObject("Scripting.FileSystemObject")
For Each myFile In fso.GetFoler(myDir).Files
     If myFile.Name = myFileName Then
          SearchFile = myDir
          Exit Function
     End If
Next
For Each myFolder In fso.GetFolder(myDir).Subfolders
     SearchFile = SearchFile(myDir & "\" & myFolder.Name)
Next
End Function
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,344
Office Version
  1. 365
Platform
  1. Windows
Why not use GetOpenFileName?
 

m_lny

New Member
Joined
Feb 24, 2005
Messages
34
Thank you for replying !

I have tried:
fname = Application.GetOpenFilename
Workbooks.Open fname

This opens the selected file in a new workbook. I have an existing workbook and sheet that I would like to add the contents of the file to. I would like to insert the first data into "A3" and then on down thru the A column. The GetOpenFile inserts the data starting at cell A1.

I have searched the Excel help files and can't find anything on GetOpenFile. It does not show up in the index of the help files. I have tried different ways of using workbooks.open, activesheet.open, application.whatever.open, and can't find what to use to insert the data into the active worksheet starting at cell A3.

What I am trying to do is to use something like GetFileOpen to select a text file somewhere on my computer, then inserting the comma delimited data into column A starting at cell A3.

Again, any help would be greatly appreciated.
Thanks
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,279
Messages
5,836,363
Members
430,424
Latest member
ALHK022

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
Top