using macros to open a file whose path can be varied

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • teak
    New Member
    • May 2017
    • 1

    using macros to open a file whose path can be varied

    I apologize if this has been answered before but I'm new to this and still have lots to learn. Here is what I'm trying to do:

    I can run a macro that opens a file as long as that path and file is static:

    Workbooks.Open Filename:="C:\m ydoc\myfile\Dat a.xlsx", _
    UpdateLinks:=3

    suppose I have to move this file to a different path: C:\newdoc\newfi le\data.xlsx. My macro will not work unless I go in and edit the macro to reflect where the file is now located.

    I prefer to somehow have some sort of variable say in an inputs worksheet so that whatever that variable contains the macro can read it and thus automatically can open the file. thus all I need to do is change the variable and not have to edit the macro.

    thanks so much
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    Code:
        b = Application.FileDialog(msoFileDialogOpen).Show
        If (b <> 0) Then strPath = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)
        MsgBox "Your filename: " + strPath

    Comment

    Working...