Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA String Spaces: Preferred Method?

When concatenating strings, there are multiple valid methods of inserting spaces.

Space Function:

Print "A" & Space(1) & "B"
A B

VS

Quotes:

Print "A" & " " & "B"
A B

VS

Chr:

Print "A" & Chr(32) & "B"
A B

All three methods yield the same result. The only place where I found Space beneficial was when using a variable for the number of spaces.

Space( <expression of number - len(var)> )

Can anyone provide any insight on why one method is better than another?

like image 781
RHDxSPAWNx Avatar asked Sep 05 '25 17:09

RHDxSPAWNx


1 Answers

As suggested, they all yield the same result when compiled. There might be situational benefit such as:

1) Space() - Good when multiple space is needed.
Space(100) is better than chr(32) & chr(32) & chr(32) & ...

2) " " - Visual expression that is easy to understand, esp for new vba learner
EDIT: Also faster than a method call, thanks to Blackhawk to point out

3) chr() - character expression that could be used to print particular character to say a text file. E.g. printing Enter/Return with chr(10). I don't see any obvious benefit for print space however

like image 187
Alex Avatar answered Sep 07 '25 18:09

Alex