Add space.

  • Thread starter Thread starter Tom
  • Start date Start date
T

Tom

Hello,

I have strings:

my1 = "test"
my2 ="something"

and I need algoritmus for adding so many spaces, to resultant lenght of my
string will be for example 20, so:

result = (my1 + 16 space)
or
result = (my2 + 11 space)

some idea? I came upon only a long solution.

tom
 
result = left(my1 & space(20),20)

might work ok
Hello,

I have strings:

my1 = "test"
my2 ="something"

and I need algoritmus for adding so many spaces, to resultant lenght of my
string will be for example 20, so:

result = (my1 + 16 space)
or
result = (my2 + 11 space)

some idea? I came upon only a long solution.

tom
 
Tom,

The code below will do what you want.
This is attached to a command button, which takes the text in cell A1, tests
its length, then adds the appropriate number of space, and then puts the
results in cell A2, you can of course do whatever you like with the result. I
haven't tested to see what happens if you put a string in of more than 20
characters, so you might want to do that or add some validation.

Range("A1").Select 'Select Cell A1
mystring$ = ActiveCell 'Copy contents of A1 to variable mystring$
a = Len(mystring$) ' Get length of mystring$ and put contents into a
b = 20 - a 'subtract length of mystring$ from 20
For x = 1 To b 'loops number of times defined by b
mystring$ = mystring$ + " " 'adds a space to end of mystring$
Next x

'Test it worked
Range("A2").Select 'Select Cell A2
ActiveCell = mystring$ 'Paste new mystring$ into A2

Hope this is of use and makes sense to you, if not please post again and I
will see if I can make it better for you.

Neil
www.nwarwick.co.uk
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top