ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Add space. (https://www.excelbanter.com/excel-programming/325316-add-space.html)

Tom

Add space.
 
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



Dave Peterson[_5_]

Add space.
 
result = left(my1 & space(20),20)

might work ok

Tom wrote:

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


--

Dave Peterson

Neil

Add space.
 
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

"Tom" wrote:

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

Add space.
 
perfect thanks

"Dave Peterson" wrote in message
...
result = left(my1 & space(20),20)

might work ok

Tom wrote:

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


--

Dave Peterson





All times are GMT +1. The time now is 05:06 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com