« Quote of the day | Main | Taking the PMP Exam - Appendix B - Contract Types »

MS Project VBA - Trim Function

Solving a recent custom field formula problem required reaching into the toolbox and pulling out the trim() string function. Trim is one of the simplest string functions. It does one thing and does it well. It just strips any leading or trailing spaces from a text string. So if you have a string like this:

myString = " There is space in front and in back "

you can use:

trim(mystring)

to return "There is space in front and in back"

The problem was that converting a Microsoft Project Unique ID to a string adds a leading space to the string (why? I don't know, but it does) so you can use trim to obtain just the characters you want.

Trim is not just a VBA function, you can use it in Excel formulas, MS Project custom field formulas and just about any programming language I've seen. I've used it extensively with spreadsheets that contain data pasted in from other sources.

RELATED POSTS
  • VBA Writing to a text file (MS Project, Excel)
  • Microsoft Project Undo Levels and Macros
  • Analyze Microsoft Project Resource Usage Data In Excel
  • Microsoft Project VBA - the Instr function
  • Office VBA for Mac After 2008
  • Using a ComboBox in a Microsoft Project Userform
  • Setting Microsoft Project Level Custom fields using VBA
  • VBA returns in Excel 2011 for Mac
  • Iterating through Microsoft Project Subprojects
  • VBA and Visual Basic For ... to ... statements

  • Comments (1)

    JohnB:

    You wrote "The problem was that converting a Microsoft Project Unique ID to a string adds a leading space to the string (why? I don't know, but it does) ..."

    While this is true, you should note that when working in VBA there is no leading space. For example:

    In the P2003 Pro immediate window

    ?""

    shows

    A macro which loops through all tasks displaying:
    ""

    also doesn't show any leading spaces.

    Post a comment

    (Comments are moderated to fight SPAM and will be published after I have a chance to approve them. Thanks for waiting.)

    About

    The previous article is Quote of the day.

    The next article is Taking the PMP Exam - Appendix B - Contract Types.

    Current articles are in the main index page and you can find a complete list of articles in the archives.

    Creative Commons License
    This weblog is licensed under a Creative Commons License.
    Powered by
    Movable Type 3.34