+ Reply to Thread
Results 1 to 14 of 14

Macro to display part of filename in cell

  1. #1
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Macro to display part of filename in cell

    Hi everybody.
    Is there a way to insert in the "text to display" only part of the filename as opposed to the entire filename? For Example if the filename is called "2014-02-20 order-no.465-Product.xlsm" I would like for the code to extract the number "465" and the word "Product" and display the text as "465 Product" on the selected cell.
    Thanks in advance:-)

    Please Login or Register  to view this content.

  2. #2
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Macro to display part of filename in cell

    do the formats of the file names change? Will it always be a 3 digit number and the word product?
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

  3. #3
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Re: Macro to display part of filename in cell

    Good question, the number will always have 6 digits but the word "product" was just an example because the product I am selling my customers will differ with each order. So I want that to be flexible.

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Macro to display part of filename in cell

    Can this help?

    Please Login or Register  to view this content.

  5. #5
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Macro to display part of filename in cell

    Hi, kosherboy,

    Please Login or Register  to view this content.
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  6. #6
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Re: Macro to display part of filename in cell

    Thanks for your reply but can you please incorporate your code into my code code?
    Also, please note that the file name is never consistent and the words in the filename don't always have the same amount of letters.

  7. #7
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Macro to display part of filename in cell

    Hi, kosherboy,

    Please Login or Register  to view this content.
    Ciao,
    Holger

  8. #8
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Re: Macro to display part of filename in cell

    Oh wow! That is amazing! Thanks!

  9. #9
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Re: Macro to display part of filename in cell


    Hi !
    Quote Originally Posted by kosherboy View Post
    For Example if the filename is called "2014-02-20 order-no.465-Product.xlsm"
    I would like for the code to extract the number "465" and the word "Product" and display the text as "465 Product"
    If it's always the last two parts separated by "-" to extract and
    if it's always a point before the number and a point after the name,
    it's really easy whatever the characters' number!

    Please Login or Register  to view this content.

  10. #10
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Macro to display part of filename in cell

    Hi, kosherboy,

    not so amazing as the name of the workbook must start with the date like "yyyy-mm-dd" followed by a blank and "order-no." which make up the 20 characters that we want to omit. A bit more stable would be to search for "order-no." with InStr and add the number of characters of the start of the position of the search string.

    Same goes for the extension: as long as you use the new files itīs okay, if you want to include the old format a well you should use InStrRev and search for the position of the last dot in the filename.

    And really the last gimmick: number and product must be divided by a dash.

    If all that is in the Name: no problem.

    Ciao,
    Holger

  11. #11
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Macro to display part of filename in cell


    Hi HaHoBe,

    have you read my previous post ?!

  12. #12
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Macro to display part of filename in cell

    Hi, Marc,

    there are a lot of different ways to solve a problem, I admit your way might be more elegant - but a) some minutes after mine and b) your sample isnīt incorporated into the original code as requested.

    Quote Originally Posted by David Coverdale
    So what?
    Ciao,
    Holger

  13. #13
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Re: Macro to display part of filename in cell

    I hear. So is there a way to always have the code search for the last two words in the filename? In this case the number "465" and the word "product"?

  14. #14
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb Re: Macro to display part of filename in cell


    Yes, just test my previous code ‼


    Another way to extract text based upon "order-no." :

    Please Login or Register  to view this content.
    Last edited by Marc L; 02-20-2014 at 02:54 PM.

+ 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. Using a Filename in a cell as part of a formula
    By HomersSpiceRack in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 10-08-2013, 02:52 PM
  2. Copy slection and paste it in ceratian workbook with PART filename as cell value
    By Dibbley247 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 11-15-2012, 09:56 AM
  3. Find Part number through Macro and output to associated part to certain cell
    By boylers75 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-09-2012, 04:32 PM
  4. [SOLVED] Adding part of a filename to a specific cell.
    By Kramxel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-16-2012, 03:11 AM
  5. Using a cell to form part of a filename
    By Anth in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-28-2005, 01:42 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