Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Str adds extra space [duplicate]

Tags:

string

excel

vba

Dim i As Integer

i = Int((8 - 2 + 1) * Rnd + 2)

Dim rg As String
Dim con As String

con = Str(i)

rg = "B" & con
MsgBox (rg)

This returns "B 4" not "B4 anyone know the issue

like image 794
user2642643 Avatar asked Aug 01 '13 15:08

user2642643


People also ask

How do I get rid of double spacing in Java?

To eliminate spaces at the beginning and at the end of the String, use String#trim() method. And then use your mytext. replaceAll("( )+", " ") .

How do I get rid of extra spaces in text Python?

Python String strip() function will remove leading and trailing whitespaces. If you want to remove only leading or trailing spaces, use lstrip() or rstrip() function instead.

How do you remove multiple spacing between words in Python?

Using re. sub() or split() + join() method to remove extra spaces between words in Python.


5 Answers

Use Cstr(i) rather than Str(i). Cstr does not add a space.

like image 148
Charles Williams Avatar answered Oct 21 '22 08:10

Charles Williams


From the Help page for Str()

When numbers are converted to strings, a leading space is always reserved for the sign of number. If number is positive, the returned string contains a leading space and the plus sign is implied.

like image 40
Andy G Avatar answered Oct 21 '22 06:10

Andy G


Str() leaves space for the sign.

As Excel has implicit conversion, you can use rg = "B" & i and get the range you want

like image 32
SeanC Avatar answered Oct 21 '22 07:10

SeanC


Use format() function ...

con = format(i)

rg = "B" & con
MsgBox (rg)
like image 29
matzone Avatar answered Oct 21 '22 08:10

matzone


Use the Trim function to remove leading space as under:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim i As Integer

i = Int((8 - 2 + 1) * Rnd + 2)

Dim rg As String
Dim con As String

con = Str(i)

rg = "B" & Trim(con)
MsgBox (rg)
End Sub
like image 31
rangan Avatar answered Oct 21 '22 08:10

rangan