Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 65
Default Fixed length padded string

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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 65
Default Fixed length padded string

Also, if the input string is greater than 16 chars, then truncate it to just 16 characters:

BEFO s = "The quick brown fox jumps over the lazy dog." ' Len = 40
AFTER: s = "The quick brown fox " ' Len = 16



  #3   Report Post  
Posted to microsoft.public.excel.programming
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)

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 65
Default Fixed length padded string

On Friday, September 20, 2019 at 5:40:31 PM UTC-7, malone wrote:

How about:-

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


Bravo bravo! That works for me. Thanks!

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Fixed length string of text MWysack Excel Discussion (Misc queries) 7 March 21st 13 12:05 AM
Public fixed length string variable Ken Excel Programming 2 February 2nd 09 10:23 PM
creating numbers to a fixed length string... ds Excel Worksheet Functions 4 July 5th 07 01:06 AM
fixed string length,even other record is copied viv Excel Discussion (Misc queries) 0 May 30th 05 08:28 PM
Sorted Fixed Length String Paul Black[_2_] Excel Programming 8 May 19th 05 04:47 PM


All times are GMT +1. The time now is 11:47 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"