ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Right justify a string (https://www.excelbanter.com/excel-programming/448357-right-justify-string.html)

Robert Crandal[_2_]

Right justify a string
 
Suppose I have the following string variables:

s1 = "0000"
s2 = "13"

I would like to copy the contents of s2 and insert it
into s1 in a right justified manner. So, the final
result of s1 would be:

s1 = "0013"

Does anyone have any generalized solutions for this?

Thanks again!



Claus Busch

Right justify a string
 
Hi Robert,

Am Fri, 8 Mar 2013 12:18:13 -0700 schrieb Robert Crandal:

s1 = "0000"
s2 = "13"

I would like to copy the contents of s2 and insert it
into s1 in a right justified manner. So, the final
result of s1 would be:

s1 = "0013"


try:
s1 = "0000"
s2 = "13"
s2 = Format(CInt(s2), s1)
MsgBox s2


Regards
Claus Busch
--
Win XP PRof SP2 / Vista Ultimate SP2
Office 2003 SP2 /2007 Ultimate SP2

Robert Crandal[_2_]

Right justify a string
 
"Claus Busch" wrote Hi Robert,

try:
s1 = "0000"
s2 = "13"
s2 = Format(CInt(s2), s1)
MsgBox s2


Hi Claus,

Thanks, that worked okay. But I overlooked a few situations.

Suppose that the s1 variable contains 4 space character (or ANY
characters, for that matter):

s1 = " " ' string with 4 spaces
s2 = "13"

Now, I would want s1 to look like:

s1 = " 13"

Do you have any ideas for this situation??



Claus Busch

Right justify a string
 
Hi Robert,

Am Fri, 8 Mar 2013 13:27:47 -0700 schrieb Robert Crandal:

s1 = " " ' string with 4 spaces
s2 = "13"

Now, I would want s1 to look like:

s1 = " 13"


in sheet try:

=LEFT(A1,2)&A2
or with VBA:

s1 = " "
s2 = "13"
s3 = Left(s1, 2) & s2


Regards
Claus Busch
--
Win XP PRof SP2 / Vista Ultimate SP2
Office 2003 SP2 /2007 Ultimate SP2

joeu2004[_2_]

Right justify a string
 
"Robert Crandal" wrote previously:
Suppose I have the following string variables:
s1 = "0000"
s2 = "13"
I would like to copy the contents of s2 and insert it
into s1 in a right justified manner. So, the final
result of s1 would be:
s1 = "0013"



"Robert Crandal" wrote
I overlooked a few situations.
Suppose that the s1 variable contains 4 space character
(or ANY characters, for that matter):
s1 = " " ' string with 4 spaces
s2 = "13"
Now, I would want s1 to look like:
s1 = " 13"


Generally:

s1 = Left(s1,Len(s1)-Len(s2)) & s2

or

s1 = IIf(Len(s1)<Len(s2),s2,Left(s1,Len(s1)-Len(s2)) & s2)

But you might not need to "preload" s1 at all, if that is what you are
doing.

Suppose n is the total length. Then:

s1 = String(n - Len(s2),fillCharacter) & s2


Robert Crandal[_2_]

Right justify a string
 
"Claus Busch" wrote Hi Robert,

s1 = " "
s2 = "13"
s3 = Left(s1, 2) & s2


Hi Claus.... This solution ALMOST worked.

Here's another example:

s1 = "--------" ' 8 dash characters
s2 = "13"

After applying your functions, the result of s3
shoud be:

s3 = "------13" ' 6 dashes + '13' string

Basically, the final length of s3 should be the
same as the length of s1. Also, I need to
cut out the rightmost characters of s1, just to
make room for s2 to be right justified.
I hope that makes sense.



GS[_2_]

Right justify a string
 
Try...

Dim s2$, vS1, n%
vS1 = Split("0000| |--------", "|")
s2 = "13"
For n = LBound(vS1) To UBound(vS1)
Debug.Print Left$(vS1(n), Len(vS1(n)) - Len(s2)) + s2
Next

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion



Claus Busch

Right justify a string
 
Hi Robert,

Am Fri, 8 Mar 2013 14:53:14 -0700 schrieb Robert Crandal:

s1 = "--------" ' 8 dash characters
s2 = "13"

After applying your functions, the result of s3
shoud be:

s3 = "------13" ' 6 dashes + '13' string


then you have to work with len(s1):
s1 = "--------"
s2 = "13"
s3 = Left(s1, Len(s1) - 2) & s2


Regards
Claus Busch
--
Win XP PRof SP2 / Vista Ultimate SP2
Office 2003 SP2 /2007 Ultimate SP2

Claus Busch

Right justify a string
 
Hi Robert,

Am Sat, 9 Mar 2013 08:40:07 +0100 schrieb Claus Busch:

s1 = "--------"
s2 = "13"
s3 = Left(s1, Len(s1) - 2) & s2


if s2 is also variable in length better try:
s1 = "--------"
s2 = "13"
s3 = Left(s1, Len(s1) - Len(s2)) & s2


Regards
Claus Busch
--
Win XP PRof SP2 / Vista Ultimate SP2
Office 2003 SP2 /2007 Ultimate SP2


All times are GMT +1. The time now is 04:07 PM.

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