Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 301
  • Last Modified:

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
Asked:
drezner7
  • 2
  • 2
1 Solution
 
jimbobmcgeeCommented:
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
 
jimbobmcgeeCommented:
(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
 
drezner7Author 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
 
drezner7Author Commented:
Worked like a charm....

Thank you very much
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now