• Status: Solved
• Priority: Medium
• Security: Public
• Views: 312

# Add limit to a string

I am outputting values from a excel spreadsheet, but what I would like to know is how to place constraints on the output for string limits. I need to have the spaces show up in the output as part of the limit because the data in the excel spreadsheet may not be the full max limit, but I need to account for spaces
I read each column as such:
strA = (oSheet.Cells(iRow, 1).Value) 'Transaction Key

and output in this manner:
oOut.WriteLine strA & "" & strB &

I need my text file to look like this..
If string A had a limit of 10, but it had 3 characters the output should be "123       ".

Thank you
0
drezner7
• 2
• 2
1 Solution

Commented:
Depending on where/how your string limits are defined, the quickest way would be to do:

strA = Left((oSheet.Cells(iRow, 1).Value) & Space(theLimit), theLimit)

where theLimit was the limit defined for column #1.

HTH

J.
0

Commented:
(although 'quickest' is probably not the best term, here; simplest might be better...)

To clarify, the command Space(theLimit) returns a string of theLimit spaces, which is appended to the end of the cell value, then the whole thing is truncated from the left, up to theLimit characters, by the wrapping Left(..., theLimit) call.

This assumes that the limit of the string is a maximum length and, if the cell value exceeds this, it should be truncated.  If the limit is only really a minimum length, you might use:

strA = (oSheet.Cells(iRow, 1).Value)
If Len(strA) < theLimit Then strA = Left(strA & Space(theLimit), theLimit), strA)

J.
0

Author Commented:
Ok, so if the limit was 10 then it would look like this correct?

strA = Left((oSheet.Cells(iRow, 1).Value)& Space(10),10 )
0

Author Commented:
Worked like a charm....

Thank you very much
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.