Link to home
Start Free TrialLog in
Avatar of drezner7
drezner7

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of jimbobmcgee
jimbobmcgee
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
(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.
Avatar of drezner7
drezner7

ASKER

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

strA = Left((oSheet.Cells(iRow, 1).Value)& Space(10),10 )
Worked like a charm....

Thank you very much