+ Reply to Thread
Results 1 to 10 of 10

Best way to obtain File name

  1. #1
    Registered User
    Join Date
    06-22-2016
    Location
    United States
    MS-Off Ver
    2003,2013
    Posts
    24

    Best way to obtain File name

    I am writing code that opens up other workbooks and copy/pastes the charts into my original workbook.

    Currently, my code looks like this

    Please Login or Register  to view this content.
    This is problematic, as if the length of the file name ever changes, my macro won't be able tot open the file properly. Is there a better way to do this?

    My thought was, maybe theres some way to associate the file location with a wildcard, e.g.

    Please Login or Register  to view this content.
    where the wildcard is the file name, then I could say

    Please Login or Register  to view this content.
    where the wildcard is the EXACT wildcard from GrabFileLocation.




    This is just a thought on how to best call a filename. If anyone has a better idea, please let me know. Thanks!
    Last edited by LewisGoose; 06-22-2016 at 01:47 PM. Reason: mark as solved

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Best way to obtain File name

    Why are you truncating what's returned from Applicaton.GetOpenFilename?

    You need the whole path and filename to open the file.
    If posting code please use code tags, see here.

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Best way to obtain File name

    Why not just open the file with the entire string returned from GetOpenFilename?
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Best way to obtain File name

    If you want to get the filename sans path then there are various ways, here's one.
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    06-22-2016
    Location
    United States
    MS-Off Ver
    2003,2013
    Posts
    24

    Re: Best way to obtain File name

    I get a "Subscript out of range" error if I try to open it with the whole file location.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Best way to obtain File name

    Where do you get that error?

    If it's in subsequent code then I would suggest you create a reference to the file when you open it and use that when you want to refer to the file rather than trying to use the name.

    For example.
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    06-22-2016
    Location
    United States
    MS-Off Ver
    2003,2013
    Posts
    24

    Re: Best way to obtain File name

    I do receive the error in subsequent code

    Please Login or Register  to view this content.
    whereas before I was using
    Please Login or Register  to view this content.
    which worked fine.

    Your first fix works very well! I don't fully understand your second solution though. (Sorry, I only started coding VBA about a week ago for my job)
    Please Login or Register  to view this content.
    The MRB in this code highlighted in red, should that be my file name?

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Best way to obtain File name

    Actually the (MRB) is a typo.

    This is how the code should look,
    Please Login or Register  to view this content.
    and with that you could then use the reference MRB like this in the rest of the code.
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    06-22-2016
    Location
    United States
    MS-Off Ver
    2003,2013
    Posts
    24

    Re: Best way to obtain File name

    That works great! Thank you so much!

    If I have an unrelated question about the same project, would it be best to ask you here or create a new thread?

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Best way to obtain File name

    Probably best to start a new thread.

+ 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] Is it possible obtain xlsxm from exe file ?
    By misalspi in forum Excel - New Users/Basics
    Replies: 5
    Last Post: 02-03-2013, 04:15 AM
  2. Obtain file extension
    By Jacques Grobler in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-22-2010, 03:05 AM
  3. Browse Button Code to obtain single file name
    By Macdave_19 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-29-2009, 05:34 AM
  4. Product key but no cd - how do i obtain new cd?
    By Joe Miller in forum Excel General
    Replies: 4
    Last Post: 11-25-2008, 08:47 AM
  5. Obtain All File names in a folder
    By Macdave_19 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-25-2008, 08:25 AM
  6. Replies: 1
    Last Post: 06-02-2006, 02:48 PM
  7. [SOLVED] how do i obtain a file name from a cell contents and use it to sa.
    By Paul Bond in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-19-2006, 06:10 PM
  8. Obtain imported file modified date
    By mikeburg in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-18-2006, 01:15 PM

Tags for this Thread

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