Prompt user to browse & Select file and hardcode values into Workbook

JonRowland

Active Member
Joined
May 9, 2003
Messages
416
Office Version
  1. 365
Platform
  1. Windows
Hi,

I'm trying to do the folllowing with my VBA code. I want to prompt the user to browse for a file and then hard code the PATH and FileName into that workbook for another procedure that would be used at various times.

At the moment I'm doing this manually. In the code below the variable FileToOpen captures the full path & filename as one. The issue with this, unless anyone can advise different , is that another produced I add an XLOOKUP based on
VBA Code:
    Cells(2, 4).FormulaR1C1 = _
                            "=XLOOKUP(RC[-1],'" & FilePath & "[" & File & "]" & ShtName & "'!C1,'" _
                         & FilePath & "[" & File & "]" & ShtName & "'!C35,""NOT IN LIST"",FALSE)"

So the [ & ] is the issue in having a one variable with the path & filename.

So ideally I'd like to tweak the code below to get separate path and filename.

VBA Code:
Sub Get_Data_From_File()

Dim FileToOpen  As Variant
Dim Path As String
Dim File As String

FileToOpen = Application.GetOpenFilename("Excel Files (*.xls*), *xls*", , "Select File")
        
        If FileToOpen <> False Then

' Here I'd like to hard code the path & filename.  FileToOpen get the full path & filename in one string

Path = xxxxxxxx
FileName =  xxxxxxxxxxxx

End If
    
End Sub

Hope that makes sense.

Thanks
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Hi
Try making your code a function & pass back the values to your declared variables

See if this update to your code does what you want

Place in STANDARD module

VBA Code:
Function Get_Data_From_File(ByRef FolderPath As String, ByRef FileName As String) As Boolean

    Dim FileToOpen  As Variant
    
    FileToOpen = Application.GetOpenFilename("Excel Files (*.xls*), *xls*", , "Select File")
    If FileToOpen = False Then Exit Function
    
    FolderPath = Left$(FileToOpen, InStrRev(FileToOpen, "\"))
    FileName = Mid$(FileToOpen, InStrRev(FileToOpen, "\") + 1)
    Get_Data_From_File = True
End Function

and to call it

VBA Code:
Sub MyProg()
    Dim FilePath As String, File As String
    
    If Not Get_Data_From_File(FilePath, File) Then Exit Sub
    
    'rest of code
    
    MsgBox FilePath & File
    
End Sub

Dave
 
Upvote 0
Solution
dmt32 - that's just what I wanted. Will work perfect with the rest of my code.

Thank you so much.
 
Upvote 0
dmt32 - that's just what I wanted. Will work perfect with the rest of my code.

Thank you so much.

Glad suggestion does what you want & appreciate your feedback

Dave
 
Upvote 0

Forum statistics

Threads
1,214,982
Messages
6,122,580
Members
449,089
Latest member
Motoracer88

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