+ Reply to Thread
Results 1 to 4 of 4

Browse to file and place filename in a cell

Hybrid View

  1. #1
    Registered User
    Join Date
    02-17-2008
    Posts
    32

    Browse to file and place filename in a cell

    Hey people,

    I wrote this code a while back to browse to a directory:

    Sub BrowseToDir()
      
      'Declare variables
      Dim strOriginalPath As String
      Dim x As Variant
      Dim strSelectedPath As String
      
      'Remember oroginal path
      strOriginalPath = CurDir
      
      
      'Show dialog and let user browse to a directory
      x = Application.GetSaveAsFilename(InitialFileName:="_", Title:="Browse to desired directory and click Save")
      
      'Get current directory, and show it
      strSelectedPath = CurDir
      Range("C2") = strSelectedPath
      
      'Change back to original drive and path
      On Error Resume Next
      ChDrive strOriginalPath
      ChDir strOriginalPath
      MsgBox strOriginalPath
      On Error GoTo 0
    
    End Sub
    Can anybody help me with instead of inputting the directory into cell 'C2' it puts the actual filename so for example scenario:

    You browse for test.xls in C\:Desktop and this places "C\:Desktop\test.xls" in cell "c2" at the moment i can only display the path

    And also if there is a way that when when u click to open the browse, if you click cancel it leaves the cell intact, at the moment it always puts a directory and overwrites, cant work it out

    Thanks in advance!
    Last edited by dntel123; 09-24-2009 at 12:23 PM. Reason: Extra detail added

  2. #2
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Browse to file and place filename in a cell

    Just change this line of code:

     Range("C2") = strSelectedPath
    to this:

    Range("C2") = strSelectedPath & x
    Right?
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  3. #3
    Registered User
    Join Date
    02-17-2008
    Posts
    32

    Re: Browse to file and place filename in a cell

    Yer that kinda works, if i select C:\File.doc it replaces whats in the cell with C:\C:\file.doc it adds an extra path sorta :S

  4. #4
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Browse to file and place filename in a cell

    Try just:

    Range("C2") = x

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1