View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
malone malone is offline
external usenet poster
 
Posts: 6
Default Fixed length padded string

On 21-Sep-2019 11:19 AM, RG III wrote:
What's the easiest way to modify a string in a variable so that it only holds a maximum of 16 characters, and if it's less than 16 characters I want to attach asterisk "*" characters until it makes the string length 16?

Here are some before-after examples:

BEFO s = "french fries" ' Len = 12
AFTER: s = "french fries****" ' Len = 16

BEFO s = "" ' Len = 0
AFTER: s = "****************" ' Len = 16

BEFO s = "tree" ' Len = 4
AFTER: s = "tree************" ' Len = 16

BEFO s = "There are fries." ' Len = 16
AFTER: s = "There are fries." ' Len = 16

The last example has 16 characters, so there's no need to pad it.

I think I could do this simply in C/C++ with the printf() format string, but I'm looking for a quick way to do this with VBA. Thanks.

-Robert


How about:-

s = left(s & String(16, "*"), 16)