+ Reply to Thread
Results 1 to 10 of 10

Truncating Data String

  1. #1
    Registered User
    Join Date
    05-23-2011
    Location
    Sacramento, California, USA
    MS-Off Ver
    Excel 2007
    Posts
    15

    Truncating Data String

    First off, I apologize if this answer is somewhere else already, I have looked and have come up empty handed.

    I have the following data that I am working with:

    /mnt/p_inf01_10080_rrd
    /mnt/p_inf01_10090_sunpower
    /mnt/p_inf01_arb
    /mnt/p_inf01_areva
    /mnt/p_inf01_bm


    I want a column with the text of each row that follows the last underscore, so: "rrd, sunpower, etc."

    Any help on doing this would be greatly appreciated. I am learning VB to find avenues to accomplish my task. Thank you.
    Last edited by theinfini; 05-24-2011 at 12:38 PM.

  2. #2
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Truncating Data String

    Try this
    Please Login or Register  to view this content.

    Hope this helps
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  3. #3
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Truncating Data String

    theinfini,

    Welcome to the Excel Forum.

    I assume that your raw data is in column A, beginning in cell A1.

    Detach/open workbook GetLastString S array col A to B - theinfini - EF777117 - SDG15.xls and run macro GetLastString.



    If you want to use the macro on another workbook:


    Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


    1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
    2. Open your workbook
    3. Press the keys ALT + F11 to open the Visual Basic Editor
    4. Press the keys ALT + I to activate the Insert menu
    5. Press M to insert a Standard Module
    6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
    7. Press the keys ALT + Q to exit the Editor, and return to Excel
    8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


    Please Login or Register  to view this content.

    Then run the GetLastString macro.
    Have a great day,
    Stan

    Windows 10, Excel 2007, on a PC.

    If you are satisfied with the solution(s) provided, please mark your thread as Solved by clicking EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

  4. #4
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Truncating Data String

    theinfini,

    I am learning VB to find avenues to accomplish my task.

    Training / Books / Sites

    How to Learn to Write Macros
    http://articles.excelyogi.com/playin...ba/2008/10/27/

    How to use the macro recorder
    http://articles.excelyogi.com/

    Click here and scroll down to Getting Started with VBA.
    http://www.datapigtechnologies.com/ExcelMain.htm

    If you are serious about learning VBA try
    http://www.add-ins.com/vbhelp.htm

    Excel Tutorials and Tips - VBA - macros - training
    http://www.mrexcel.com/articles.shtml

    See David McRitchie's site if you just started with VBA
    http://www.mvps.org/dmcritchie/excel/getstarted.htm

    What is a Visual Basic Module?
    http://www.emagenit.com/VBA%20Folder...vba_module.htm

    Debra Dalgleish has some notes how to implement macros here:
    http://www.contextures.com/xlvba01.html

    David McRitchie has an intro to macros:
    http://www.mvps.org/dmcritchie/excel/getstarted.htm

    Ron de Bruin's intro to macros:
    http://www.rondebruin.nl/code.htm

    Creating custom functions
    http://office.microsoft.com/en-us/ex...117011033.aspx

    Writing Your First VBA Function in Excel
    http://www.exceltip.com/st/Writing_Y...Excel/631.html

    Where to paste code in VBE VBA
    Introducing the Excel VBA Editor
    http://www.ask.com/web?qsrc=2417&o=1...cel+VBA+Editor

    VBA for Excel (Macros)
    http://www.excel-vba.com/excel-vba-contents.htm

    VBA Lesson 11: VBA Code General Tips and General Vocabulary
    http://www.excel-vba.com/vba-code-2-1-tips.htm

    Excel VBA -- Adding Code to a Workbook
    http://www.contextures.com/xlvba01.html

    http://www.excel-vba.com/
    http://www.mvps.org/dmcritchie/excel/getstarted.htm
    http://www.exceltip.com/excel_links.html

    (livelessons video)
    Excel VBA and Macros with MrExcel
    ISBN: 0-7897-3938-0
    http://www.amazon.com/Excel-Macros-M...7936479&sr=1-1

    Excel Tutorials / Video Tutorials - Functions
    http://www.contextures.com/xlFunctions02.html

    http://www.xl-central.com/index.html

    http://www.datapigtechnologies.com/ExcelMain.htm

    http://www.contextures.com/xlDataVal02.html

    Cascading queries

    http://www.tushar-mehta.com/excel/ne...ing_dropdowns/

    http://www.contextures.com/xlDataVal05.html
    http://www.contextures.com/xlDataVal08.html#Larger

    Excel Data Validation - Add New Items
    http://www.contextures.com/excel-dat...ation-add.html

    Programming The VBA Editor - Created by Chip Pearson at Pearson Software Consulting LLC
    This page describes how to write code that modifies or reads other VBA code.
    http://www.cpearson.com/Excel/vbe.aspx

    Locating files containing VBA
    Searching Files in Subfolders for VBA code string:
    http://www.dailydoseofexcel.com/arch...a-code-string/

    http://www.pcreview.co.uk/forums/thread-978054.php

    Excel 2003 Power Programming with VBA (Excel Power Programming With Vba)
    by John Walkenbach

    VBA and Macros for Microsoft Excel, by Bill Jelen "Mr.Excel" and Tracy Syrstad

    Excel Hacks 100 Industrial-Strength Tips & Tools, by David & Traina Hawley

    VBA and Macros for Microsoft Excel 2007, by Bill Jelen "Mr.Excel" and Tracy Syrstad

    Excel 2007 Book: you can try this...there is a try before you buy ebook available at this link…
    http://www.mrexcel.com/learnexcel2.shtml

    Professional Excel Development
    by Stephen/ Bovey, Rob/ Green, John Bullen (Paperback - Feb 11, 2005)

    Excel 2002 VBA: Programmers Reference
    by Rob Bovey, Stephen Bullen, John Green, and Robert Rosenberg (Paperback - Sep 26, 2001)

    VB & VBA in a Nutshell: The Language
    (http://www.amazon.co.uk/VB-VBA-Nutsh...4671189&sr=1-2)

    Writing Excel Macros with VBA
    (http://www.amazon.co.uk/Writing-Exce...4671189&sr=1-3)

    User Form Creation
    http://www.contextures.com/xlUserForm01.html

    DonkeyOte: My Recommended Reading
    Volatility
    http://www.decisionmodels.com/calcsecretsi.htm

    Sumproduct
    http://www.xldynamic.com/source/xld.SUMPRODUCT.html
    http://www.xldynamic.com/source/xld.SUMPRODUCT.html

    Arrays
    http://www.xtremevbtalk.com/showthread.php?t=296012

    Pivot Intro
    http://peltiertech.com/Excel/Pivots/pivotstart.htm

    Email from XL - VBA
    http://www.rondebruin.nl/sendmail.htm

    Outlook VBA
    http://www.outlookcode.com/article.aspx?ID=40

    Function Dictionary
    http://www.xlfdic.com/

    Function Translations
    http://www.piuha.fi/excel-function-name-translation/

    Dynamic Named Ranges
    http://www.contextures.com/xlNames01.html

    How to create Excel Dashboards
    http://www.contextures.com/excel-dashboards.html
    http://chandoo.org/wp/excel-dashboards/
    http://chandoo.org/wp/management-dashboards-excel/

    Excel Dashboard / Scorecard Ebook
    http://www.qimacros.com/excel-dashboard-scorecard.html

    Mike Alexander from Data Pig Technologies
    Excel 2007 Dashboards & Reports For Dummies

  5. #5
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Truncating Data String

    @ stanleydgromjr

    Why use VBa?

    This would do exactly the same, and is more elegant than my original, rather protracted, suggestion.
    Please Login or Register  to view this content.
    Drag/Fill Down

  6. #6
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Truncating Data String

    yep i'm with marcol no vba needed
    =TRIM(RIGHT(SUBSTITUTE(A2,"_",REPT(" ",100)),100))
    but 100 is a tad excessive
    =TRIM(RIGHT(SUBSTITUTE(A2,"_",REPT(" ",20)),20)) should cover it!
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  7. #7
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Truncating Data String

    Marcol,

    I am learning VB to find avenues to accomplish my task.
    Per theinfini.

    Besides, I used the Substitute formula in the macro.

  8. #8
    Registered User
    Join Date
    10-28-2008
    Location
    New Jersey
    MS-Off Ver
    2007
    Posts
    15

    Re: Truncating Data String

    Marcol, Martindwilson,

    =TRIM(RIGHT(SUBSTITUTE(A2,"_",REPT(" ",20)),20))
    Quick question: Is there a way to make this work if there are (consecutive) spaces in the string?

  9. #9
    Registered User
    Join Date
    05-23-2011
    Location
    Sacramento, California, USA
    MS-Off Ver
    Excel 2007
    Posts
    15

    Re: Truncating Data String

    Marcol,

    Thank you, that worked, but how. I tried "reverse-engineering" it (taking it apart piece by piece and watching the results to understand how it works) but I don't understand how substituting the underscore with 100 spaces removes the rest of the text. Is it a limit on text in an excel box?

    Whatever the case is, I'm just glad it worked.

  10. #10
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Truncating Data String

    By replacing the underscore in all instances with 100 spaces, and I agree with Martin this is somewhat excessive, we produce a string that is long enough to use RIGHT("the extended string",100) to return a string that will contain "all the rightmost characters" preceded by 100 minus "all the rightmost characters" spaces.
    TRIM(RIGHT("the extended string",100)) removes all the leading spaces.

    Not sure where the extra consecutive spaces comes into it, but the worksheetfunction TRIM() removes ALL extra spaces, unlike the VBa function which only removes leading and trailing spaces.

    [EDIT]
    You might want to reverse engineer my original suggestion for your own interest.
    This works out how many instances of "underscore" are present in the string by replacing "_" with "", by subtracting the length of the modified string from the length of the original string we know how many "underscores" there are.
    Using this information we can replace the last instance, or for matter, any single instance of "_" with some unlikely character, say "|" (Pipe), then use Mid() and FIND() to return the characters to the right of the chosen instance.
    Last edited by Marcol; 05-23-2011 at 08:58 PM. Reason: Added a thought to help an OP that seems to want to think on there own behalf.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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