Repeat Character in VBA

  • Thread starter Thread starter Brad
  • Start date Start date
B

Brad

Thanks for taking the time to read my question

I would like add a space to my string x times, where x = an integer.

How can I do that with out using a loop statement?

x=10
eg: "there are 10 spaces between here" & space * x & "here."

Thanks,

Brad
 
There is a function called space that you could use

dim str as string
str = "this" & space(10) & "that"
 
Hi,

this adds 10 spaces to the end without a loop and with the use of
left/mid/right you could put the spaces anywhere

x = 10
mystring = "Mike H"
mystring = mystring + WorksheetFunction.Rept(" ", x)

Mike
 
Just to add to Jim's reply...

There's another function that can be used with other non-space characters:

dim str as string
str = String(12, "x")

(It can also be used with space characters <bg>.)
 
Yeah but I loved the ironly that Brad had the function correct and that the
only error was *x instead of (x).
 
And just to add onto Dave reply, the String function only repeats single
characters; however, if you wanted to repeat multiple characters, all you
would have to do is couple the String or Space function with the Replace
function. For example, to produce a text string of, say, 100 "XO" character
pairs in a row, just do this...

HundredXOs = Replace(Space(100), " ", "XO")

or this would work also...

HundredXOs = Replace(String(100, "Z"), "Z", "XO")

where you can use any character you want to in place of the "Z".
 
Thanks to all for your posts!!

It is quite ironic that I was so close but yet had no idea.

Hope everyone has a great day today,

Brad
 
Back
Top