+ Reply to Thread
Results 1 to 8 of 8

Extract Text from the Filename based on certain characters in the filename

  1. #1
    Registered User
    Join Date
    12-16-2017
    Location
    Reading
    MS-Off Ver
    2010
    Posts
    7

    Extract Text from the Filename based on certain characters in the filename

    Dear Forum Members,

    I have a requirement where I copy content from one excel sheet and transpose it to another excel sheet. While pasted data into one of the column example Column A - i want to paste the text from the soure filename. The rule of this text extraction (from the source filename) is below:

    I receive files with three different naming convention. I want to extract string from the filename to identify the from where orders are coming to me.
    a. For Filename "ORD Load ORDERS - ORD-DL-LOND-003.xls" -- (If the filename contains DL, I need to extract “ORD-DL-LOND-003” from the filename and put into column A)
    b. For Filename "ORD-OL-003 (SWA 10.16).xls" -- (If the filename contains OL, I need to extract “ORD-OL-LOND-003(SWA 10.16)” from the filename and put into column A)
    c. For Filename "ORD Load ORDERS - ORD-OFF-002.xls" -- (If the filename contains OFF, I need to extract “ORD-OFF-002” from the filename and put into column A)
    At the moment, I am using below code to populate the Column A with the filename (Fname). But the problem is - it is pasting the whole directory path of the path.


    Please Login or Register  to view this content.
    Fname - is the variable which contains the whole path of the source file.

    Any help would be appreciated. Thanks.

  2. #2
    Registered User
    Join Date
    12-16-2017
    Location
    Reading
    MS-Off Ver
    2010
    Posts
    7

    Re: Extract Text from the Filename based on certain characters in the filename

    I receive files with three different naming convention. I want to extract string from the filename to identify the from where orders are coming to me.
    a. For Filename "ORD Load ORDERS - ORD-DL-LOND-003.xls" -- (If the filename contains DL, I need to extract “ORD-DL-LOND-003” from the filename and put into column A)
    b. For Filename "ORD-OL-003 (SWA 10.16).xls" -- (If the filename contains OL, I need to extract “ORD-OL-LOND-003(SWA 10.16)” from the filename and put into column A)
    c. For Filename "ORD Load ORDERS - ORD-OFF-002.xls" -- (If the filename contains OFF, I need to extract “ORD-OFF-002” from the filename and put into column A)

  3. #3
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Extract Text from the Filename based on certain characters in the filename

    To extract basename you can use this.

    Please Login or Register  to view this content.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  4. #4
    Registered User
    Join Date
    12-16-2017
    Location
    Reading
    MS-Off Ver
    2010
    Posts
    7

    Re: Extract Text from the Filename based on certain characters in the filename

    Dear Bakerman2,

    Thanks for your switf reply. The above code gives me filename(without xls or xlsx extension). How do i extract the string from the filename (or base file name) based on the code ?

  5. #5
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Extract Text from the Filename based on certain characters in the filename

    How about this ?
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    12-16-2017
    Location
    Reading
    MS-Off Ver
    2010
    Posts
    7

    Re: Extract Text from the Filename based on certain characters in the filename

    This is awesome. Thanks a lot bajerman2. Although I thought, due to my lack of knowledge , that this code may not work for all the three scenarios i have mentioned above ... this has solved my problem completly. Thanks a ton for this....

    Quote Originally Posted by bakerman2 View Post
    How about this ?
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    12-16-2017
    Location
    Reading
    MS-Off Ver
    2010
    Posts
    7

    Re: Extract Text from the Filename based on certain characters in the filename

    Can I also ask the following:

    I am copying the data from one of the colum of source excel sheet and paste it to the destination excel sheet using below code:

    Please Login or Register  to view this content.
    Now the problem - I am having is that the format of the newly pasted row in the destination is differed from the previous row. For example, if the above code is pasting the data in row#415 .. the format of the row#415 should be same as the format of row#414 i.e. previous row.

    Is there any way where I can copy the format of the previous row before pasting the data to the new row?

  8. #8
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Extract Text from the Filename based on certain characters in the filename

    Untested but something like this should work.

    Please Login or Register  to view this content.
    Last edited by bakerman2; 12-17-2017 at 06:07 AM.

+ 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. Replies: 9
    Last Post: 08-04-2016, 06:40 AM
  2. Replies: 1
    Last Post: 12-09-2015, 05:43 AM
  3. Formula to return filename in folder based on partial filename
    By Rerock in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 09-26-2014, 12:28 PM
  4. VBA To set Filename based on 1st 10 characters of workbooks being consolidated
    By dawnmau in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-05-2014, 03:29 PM
  5. [SOLVED] How to extract filename from path\filename
    By mpan in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 09-05-2013, 11:23 PM
  6. VBA Extract First 4 Characters From Filename
    By sanjeevpandey in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-18-2013, 06:59 PM
  7. Replies: 3
    Last Post: 07-09-2012, 03:09 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