Results 1 to 6 of 6

Differences between the constants vbCr, vbLf & vbCrLf

This is a discussion on Differences between the constants vbCr, vbLf & vbCrLf within the Excel Questions forums, part of the Question Forums category; Hello I tried the constants vbCr, vbLf & vbCrLf in a MsgBox: MsgBox "Hi" & vbCr & "There!" MsgBox "Hi" ...

  1. #1
    Board Regular
    Join Date
    Mar 2010
    Posts
    81

    Default Differences between the constants vbCr, vbLf & vbCrLf

    Hello

    I tried the constants vbCr, vbLf & vbCrLf in a MsgBox:

    MsgBox "Hi" & vbCr & "There!"
    MsgBox "Hi" & vbLf & "There!"
    MsgBox "Hi" & vbCrLf & "There!"

    and it appears to have the same results (the word "There!" appears on a new paragraph).

    However, there could be differences in their functions, between Carriage return and Linefeed.

    I wish to know the differences between Carriage return and Linefeed (I tried to look in Excel Help but there were no explaination what is a Carriage return and a Linefeed, or a combination of both.

    Thank you in advance for your help!

  2. #2
    VoG
    VoG is offline
    MrExcel MVP
    Moderator
    VoG's Avatar
    Join Date
    Jun 2002
    Location
    127.0.0.1
    Posts
    63,565

    Default Re: Differences between the constants vbCr, vbLf & vbCrLf

    Hi. In a MsgBox they all do the same thing as does

    MsgBox "Hi" & vbNewLine & "There!"

    but you may see a difference if you write to a cell using those VB characters.
    HTH, Peter
    Please test any code on a copy of your workbook.

  3. #3
    Board Regular
    Join Date
    Mar 2010
    Posts
    81

    Default Re: Differences between the constants vbCr, vbLf & vbCrLf

    Hi VoG, thanks for your reply.

    Indeed I tried vbCr and vbLf writing to a cell value and I could see the differences.

    vbCr & vbLf both creates a new paragraph in MsgBox but when it comes to writting values to a cell, it seems to me vbCr didn't have any effect but vbLf acted like as if I used Alt+Enter to create a new paragraph within a cell.

  4. #4
    New Member
    Join Date
    Mar 2011
    Posts
    1

    Default Re: Differences between the constants vbCr, vbLf & vbCrLf

    Hi,antaeusguy.

    You can find the history of Carriage return and Linefeed here.

    http://en.wikipedia.org/wiki/Teleprinter

    Control characters


    A typewriter or electromechanical printer can print characters on paper, and execute operations such as move the carriage back to the left margin of the same line (carriage return), advance to the same column of the next line (line feed), and so on. Commands to control non-printing operations were transmitted in exactly the same way as printable characters by sending control characters with defined functions (e.g., the line feed character forced the carriage to move to the same position on the next line) to teleprinters. In modern computing and communications a few control characters, such as carriage return and line feed, have retained their original functions (although they are often implemented in software rather than activating electromechanical mechanisms to move a physical printer carriage) but many others are no longer required and are used for other purposes.


    Different Operation System use different character for a standard line feed:

    Unix ---vbCr
    Windows---vbCrLf
    Mac---vbLf

  5. #5
    MrExcel MVP
    Moderator
    xenou's Avatar
    Join Date
    Mar 2007
    Location
    Clev. OH, USA
    Posts
    14,168

    Default Re: Differences between the constants vbCr, vbLf & vbCrLf

    For I don't know what reason, major operating systems use different end of line characters. Windows uses CRLF (two characters) to mark the end of a line. But in my experience it generally still breaks to a new line on any LF. This is what you've noticed too.

    For what it's worth:

    Windows: CRLF
    Unix and Unix like systems: LF
    Mac: CR

    In many cases, any of these end of line characters will "work" - especially with text file processing. GUI programs are more sensitive (working in cells, textboxes, word processing programs).

    ξ

    Using: Office 2007/Win7 (work) Office 2010/Win7 (home)

    You are rich in proportion to the number of things you can let alone.
    -- Henry David Thoreau

  6. #6
    Board Regular
    Join Date
    Sep 2010
    Location
    Scottsdale, AZ
    Posts
    71

    Default Re: Differences between the constants vbCr, vbLf & vbCrLf

    I'm coming in to this discussion well after it had ended, but I wanted to make a note here that might someday be helpful to someone.

    I was using vbCrLf to create line breaks within cells. It did exactly what I wanted it to. But one day we noticed that it was also including a space (Chr(13)), and this space caused a lot of probelms. I found, on another post on this forum, that changing to vbLf results in that extra space not being included, and getting rid of that space solved our problems (the problem became apparent when the space began showing up on screens and in print as a little square box everyplace that I had vbCrLf). Perhaps there's some reason to use vbCrLf instead of vbLf but in my case vbLf was certainly superior.

    So if you're having similar problems I'd recommend trying vbLf. But the following code should clean up any of these spaces that already exist in your spreadsheet.

    Code:
    Sub CleanThisNonsenseUp()
    Dim myst$, i%
    myst = ActiveSheet.Range("b6").Value
    For i = 1 To Len(myst)
         MsgBox Asc(Mid(myst, i, 1))
    Next
    End Sub

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


DMCA.com