+ Reply to Thread
Results 1 to 8 of 8

Using a cell number instead of a filepath in declaration

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    639

    Using a cell number instead of a filepath in declaration

    I had an idea a while back which I have been unable to resolve and wondered if anyone knew a solution.

    I create funky things that get passed around the office and different people store them in different locations, instead of having to walk around huge offices and changing the code for everyone that uses it I thought I could insert a user form that you can use Application.GetOpenFilename so they can save the document and then enter the file location themselves, which is saved on a hidden sheet in a cell which the code can refer to instead.

    E.g.Instead of declaring a folder location in the code (i.e. Set Filepath = "C:\My Computer\My Location\My Folder") is it possible to enter the filepath into an excel cell (for the example A5) and call A5 within the code rather than the location and still declare it as Filepath.

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,335

    Re: Using a cell number instead of a filepath in declaration

    Yes, you just refer to the range instead of a literal value:
    Dim FilePath as String
    Filepath = Sheets("Some sheet").range("A5").value
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    639

    Re: Using a cell number instead of a filepath in declaration

    I tried that and it wouldn't work.

    What if the filepath for the spreadsheet has also already been defined?

    i.e.

    Dim FPath as Workbook
    Dim StUp as Worksheet
    Dim Filepath as String
    
    Set FPath = Workbooks.Open("C:\Users\Scott\Documents\CMC\Database.xlsm")
    Set StUp = FPath.Worksheets("BAU") 
    
    Set Filepath = FPath.Range("A5").value

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,335

    Re: Using a cell number instead of a filepath in declaration

    You don't use Set with Strings and you have to specify a worksheet for the range. Note my code versus yours.

  5. #5
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    639

    Re: Using a cell number instead of a filepath in declaration

    So would something like this work?

    BA1 would contain "C:\Users\Scott\Documents\CMC\Database.xlsm"

    Dim FPath as String
    Dim HR1 as Worksheet
    
    FPath = Sheets("Sheet1").Range("BA1").Value
    Set HR1 = FPath.Worksheets("HR")
    
    With HR1.Range("C" & Rows.Count).End(xlUp).Offset(1)
        .Value = HR.Initials.Value & "001"
    Last edited by Sc0tt1e; 10-01-2014 at 05:23 AM.

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,335

    Re: Using a cell number instead of a filepath in declaration

    Not quite - this will fail because FPath is a String:
    Set HR1 = FPath.Worksheets("HR")
    You need:
    Set HR1 = Workbooks.Open(FPath).Worksheets("HR")

  7. #7
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    639

    Re: Using a cell number instead of a filepath in declaration

    Ahhhhhh, you star.

    Cheers Romper, you have just made my life a hell of a lot easier.

  8. #8
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,335

    Re: Using a cell number instead of a filepath in declaration

    Glad to help.

+ 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. [SOLVED] Trying to get the filepath on "Open", but get filepath of a temporary file instead
    By ianpwilliams in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 08-22-2013, 12:27 AM
  2. [SOLVED] Copying files from a selected filepath to a filepath mentioned in a worksheet's cell
    By subbby in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-10-2012, 03:59 PM
  3. [SOLVED] Copying filepath onto a cell
    By subbby in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-01-2012, 11:43 AM
  4. Replies: 3
    Last Post: 09-24-2010, 04:04 AM
  5. Filepath in cell problem.
    By Gus80 in forum Excel General
    Replies: 2
    Last Post: 03-31-2010, 07:36 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