MLAlex
MLAlex

Reputation: 142

Excel VBA Print adds new line

Months ago I wrote a small Makro for my Company and until now it worked just fine. Recently (maybe because we updated Office Version) there is a bug, that excel adds a new line in a print statement.

Code:

outputString = value1 & "                  " & value2 & "                  " & value3 & "                  " & value4 & "               " & value5 & "                 " & user & "                  " & date 

used to give me this output:

22 S ***/***s                     9932256                     B*****t                                         Fatma                                        811                 R******r Alexander                 27.12.2019

now gives me this output:

22 S ***/***s                     9932256                     B*****t                                         Fatma                                        811                 R******r Alexander                 
                   27.12.2019

As you can see, excel adds a newline.

Can somebody tell me what happends here?

EDIT:

Solution:

Excel adds the new line after using:

user = Application.UserName

Maybe this is due to the office update we had. So I just took substring and cut the last character and works fine now.

Upvotes: 0

Views: 567

Answers (1)

Storax
Storax

Reputation: 12207

Application.Username returns as the documentation states the username. As it is Read/write it can indeed happen that you will get a username that contains a vbLf or vbCrLf. Have a look at the following example

Sub UserName_VbCrlf()
    Dim origUser As String
    Dim user As String

    ' Be careful when testing as it replaces your username!!
    ' Maybe you save it first
    origUser = Application.UserName

    Application.UserName = "Storax" & vbLf & "Home adress"
    user = Application.UserName

    ' This will give different lengths
    Debug.Print Len(user), Len(Replace(user, vbLf, ""))

    ' Restore original username
    Application.UserName = origUser

End Sub

Upvotes: 1

Related Questions