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
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
Use Cstr(i)
rather than Str(i)
. Cstr
does not add a space.
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.
Str()
leaves space for the sign.
As Excel has implicit conversion, you can use rg = "B" & i
and get the range you want
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
Excel Concatonate a string based on the int.
Here's a desciption