+ Reply to Thread
Results 1 to 10 of 10

Path of File Picked Up From Cells In Spreadsheet

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-20-2008
    Location
    Virginia
    MS-Off Ver
    2003
    Posts
    129

    Path of File Picked Up From Cells In Spreadsheet

    Hi All,

    Is there a way to make the contents of a particular cell in a worksheet show where to pick up another file. Here is my code to pick up a file:

    Sub workOnSheet()
    
        Dim FileName As Variant
        Dim Filt As String, Title As String
        Dim FilterIndex As Integer, Response As Integer 
        Dim NameOnly As String
        '   Set Drive letter
            ChDrive "E:\"
        '   Set to Specified Path\Folder
            ChDir "E:\PATH HERE"
        '   Set File Filter
            'Filt = "PID files (*.pid), *.pid"
            Filt = "PIW files (*.xls), *.xls"
        '   Set *.* to Default
            FilterIndex = 5
        '   Set Dialogue Box Caption
            Title = "Please select a different File"
        '   Get FileName
            FileName = Application.GetOpenFilename(FileFilter:=Filt, _
                FilterIndex:=FilterIndex, Title:=Title)
        '   Exit if Dialogue box cancelled
            If FileName = False Then
                Response = MsgBox("No File was selected", vbOKOnly & vbCritical, "Selection Error")
                End
                Exit Sub
            End If
        '   Display Full Path & File Name
            Response = MsgBox("You selected " & FileName, vbInformation, "Proceed")
        
            NameOnly = GetFileName(CStr(FileName))
            
            DEFName = NameOnly
            
        '   Open Selected Workbook
            Workbooks.Open FileName
    Then, I need to be able to pickup the path and drive from a cell out of a particular worksheet so I user can change it when they move the project around.
    Lets say I want to put the drive and path under A1 and A2 in workbook named KittyCat where a user can go in and change it. How would I alter the code above to make it accept this. Thank you for any help you can provide on this!!!

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Do you mean like this/

      '   Set to Specified Path\Folder
        If Not IsEmpty(ActiveSheet.Cells(1, 1)) Then
            ChDir ActiveSheet.Cells(1, 1).Value
        Else: MsgBox "no path entered", vbCritical, "Error"
            Exit Sub
            End If
    Does your posted code work? It needs a Function - GetFileName
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Forum Contributor
    Join Date
    05-20-2008
    Location
    Virginia
    MS-Off Ver
    2003
    Posts
    129

    Roy--> It works....Here is the function you asked about

    Public DEFName As String
    
    Public Function GetFileName(flname As String) As String
        
        'Get the filename without the path or extension.
        'Input Values:
        '   flname - path and filename of file.
        'Return Value:
        '   GetFileName - name of file without the extension.
        
        Dim posn As Integer, i As Integer
        Dim fName As String
        
        posn = 0
        'find the position of the last "\" character in filename
        For i = 1 To Len(flname)
            If (Mid(flname, i, 1) = "\") Then posn = i
        Next i
    
        'get filename without path
        fName = Right(flname, Len(flname) - posn)
    
        'get filename without extension
       ' posn = InStr(fName, ".")
        '    If posn <> 0 Then
         '       fName = Left(fName, posn - 1)
          '  End If
        GetFileName = fName
    End Function
    Do you know how I can make it accept the path and drive out of a worksheet cell??? P

  4. #4
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573
    Cells(1,1) is the same as Range("A1"). So, to put and get a value for cell A1:
    Range("A1").Value=ThisWorkbook.FullPath
    MsgBox Range("A1").Value

  5. #5
    Forum Contributor
    Join Date
    05-20-2008
    Location
    Virginia
    MS-Off Ver
    2003
    Posts
    129

    Still Working on Problem

    Kenneth Hobson - This looks like what I need. How do I specify a specific sheet of a workbook with the code you just gave me??

    Roy- I am still trying to solve. Thank you for all the help so far though!!

  6. #6
    Forum Contributor
    Join Date
    05-20-2008
    Location
    Virginia
    MS-Off Ver
    2003
    Posts
    129

    Kenneth Hobson -

    Besides picking a specific worksheet to pull A1 from out of workbook. I would like to know where would I put this code, too? Could you show me the right spot to make it work - SEOT

  7. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Glad it worked. I just wasn't sure if you knew that the Function was needed.

    Please mark your Thread Solved.

  8. #8
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    I thought that you said it worked, the code that i gave you will pick the path up from A1. You need to type the path into it.

    Sub workOnSheet()
    
        Dim FileName As Variant
        Dim Filt As String, Title As String
        Dim FilterIndex As Integer, Response As Integer
        Dim NameOnly As String
        '   Set Drive letter
        ChDrive "E:\"
        '   Set to Specified Path\Folder
        'Path entered into A1
        If Not IsEmpty(ActiveSheet.Cells(1, 1)) Then
            ChDir ActiveSheet.Cells(1, 1).Value
        Else: MsgBox "no path entered", vbCritical, "Error"
            Exit Sub
        End If
        '   Set File Filter
        'Filt = "PID files (*.pid), *.pid"
        Filt = "PIW files (*.xls), *.xls"
        '   Set *.* to Default
        FilterIndex = 5
        '   Set Dialogue Box Caption
        Title = "Please select a different File"
        '   Get FileName
        FileName = Application.GetOpenFilename(FileFilter:=Filt, _
                                               FilterIndex:=FilterIndex, Title:=Title)
        '   Exit if Dialogue box cancelled
        If FileName = False Then
            Response = MsgBox("No File was selected", vbOKOnly & vbCritical, "Selection Error")
            End
            Exit Sub
        End If
        '   Display Full Path & File Name
        Response = MsgBox("You selected " & FileName, vbInformation, "Proceed")
    
        NameOnly = GetFileName(CStr(FileName))
    
        DEFName = NameOnly
    
        '   Open Selected Workbook
        Workbooks.Open FileName

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. macro to import text file
    By maacmaac in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-24-2008, 09:08 AM
  2. Use one file to accumulate cells on a second file(closed).
    By nousibona in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-23-2008, 12:36 AM
  3. Determining relative file path macro
    By ctmurray in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-18-2007, 12:25 AM
  4. Replies: 3
    Last Post: 12-20-2006, 08:17 AM
  5. Access one spreadsheet in file from another in the same file
    By doRodrigo in forum Excel - New Users/Basics
    Replies: 4
    Last Post: 12-10-2006, 06:39 PM

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