+ Reply to Thread
Results 1 to 3 of 3

Can you stop the Trim function translating hyphenated cells into date format?

  1. #1
    Registered User
    Join Date
    12-07-2006
    Posts
    36

    Question Can you stop the Trim function translating hyphenated cells into date format?

    I wrote some simple code to go through a worksheet and Trim the contents of all the cells, like this:

    For r = 1 To 100
    For c = 1 To 10
    Cells(r, c).Value = Trim(Cells(r, c).Value)
    Next c
    Next r


    But some of the cells have numeric codes in them which contain hyphens.

    The Trim function seems to have interpreted some of these hyphenated codes as dates, and it has automatically translated them into date format.
    Is there a way to stop the Trim function from doing this?

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello Iterature,

    The Trim function removes both leading and trailing spaces from a string. The cell formatting determines how Excel will interpret the contents. If the cell format is "General" then Excel will attempt try determine if the string can be converted into some other format, like a number, a date, time, etc. Excel will attempt to convert any string whose first character is number and has embedded hyphens to a Date format. Leaving the leading space will prevent the conversion to a Date.

    To Trim the Trailing spaces...
    RTrim(string)

    Sincerely,
    Leith Ross

  3. #3
    Registered User
    Join Date
    12-07-2006
    Posts
    36
    Many thanks for your reply, Leith.
    I didn't know that Excel tries to automatically format 'General' type cells.

    Thanks.

+ 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