Fill in Text with blanks.

D

David Langschied

I am completing a requirement for a bank upload of account transactions.
There is a requirement that the upload contain blanks to the right to fill a
required field. I am struggling to figure out how to do this.

Example:
If the field is 18 characters long and the entry is 10 characters long, then
they want the remaining characters expressed as blanks to the right.

So,

HowdyDoody would be "HowdyDoody "


Any ideas?
 
W

ward376

Here's a way - evaluate the length of each entry, add blanks if less
than desired, then return the number of characters required.

Sub addBlanks()
Dim c As Range

For Each c In Sheet1.Range("a1:a5")
If Len(c.Text) <> 18 Then
c.Offset(0, 1).Value = _
Left(c.Text & " ", 18)
End If
Next c

End Sub

Cliff Edwards
 
C

Chip Pearson

Try something along the lines of

Dim S As String
S = "whatever"
If Len(S) >= 18 Then
S = Left$(S, 18)
Else
S = S & Space$(18 - Len(S))
End If

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)
 
R

Rick Rothstein

If it is NEVER possible for the entry to be longer than 18 characters (that
is, will NEVER overflow the field), then...

FilledOutField = Format(TheEntry, "!" & String(18, "@"))

If it is possible for your entry to overflow the field, then it depends on
which part of the entry you want to keep. If that would be the last 18
characters, then use the above code. If that would be the first 18
characters, then use this instead....

FilledOutField = Format(Left(TheEntry, 18), "!" & String(18, "@"))
 
R

Rick Rothstein

Another possibility you can explore is fixed-length strings...

Dim FilledOutField As String * 18
......
......
FilledOutField = TheEntry
 
D

Dana DeLouis

David said:
I am completing a requirement for a bank upload of account transactions.
There is a requirement that the upload contain blanks to the right to fill a
required field. I am struggling to figure out how to do this.

Example:
If the field is 18 characters long and the entry is 10 characters long, then
they want the remaining characters expressed as blanks to the right.

So,

HowdyDoody would be "HowdyDoody "

Hi. Just an idea if one wanted spaces to the Left.

Sub Demo()
Dim s As String * 18
s = "OnLeft"
Debug.Print s & ":"

RSet s = "OnRight"
Debug.Print s & ":"
End Sub


OnLeft :
OnRight:

= = =
Dana DeLouis
 
D

David Langschied

Thanks! This works perfectly!

Rick Rothstein said:
If it is NEVER possible for the entry to be longer than 18 characters (that
is, will NEVER overflow the field), then...

FilledOutField = Format(TheEntry, "!" & String(18, "@"))

If it is possible for your entry to overflow the field, then it depends on
which part of the entry you want to keep. If that would be the last 18
characters, then use the above code. If that would be the first 18
characters, then use this instead....

FilledOutField = Format(Left(TheEntry, 18), "!" & String(18, "@"))
 

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

Top