i have a free space of 60 charachter.
now i need to center a text in this string...
note:
the text to center Have a variable lenght, but max 60 charachter
center text in a fixed string space
-
- PlatinumLounger
- Posts: 4376
- Joined: 26 Apr 2010, 17:36
-
- Administrator
- Posts: 78665
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: center text in a fixed string space
If you use a fixed-width font such as Courier New or Consolas, you can use:
Code: Select all
Dim strShort As String
Dim strLong As String
Dim lngSpace As Long
strShort = "Salvatore"
lngSpace = (60 - Len(strShort) \ 2
strLong = Space(lngSpace) & strShort & Space(60 - lngSpace)
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4376
- Joined: 26 Apr 2010, 17:36
Re: center text in a fixed string space
WORK!HansV wrote: ↑20 Oct 2021, 07:19If you use a fixed-width font such as Courier New or Consolas, you can use:
Code: Select all
Dim strShort As String Dim strLong As String Dim lngSpace As Long strShort = "Salvatore" lngSpace = (60 - Len(strShort) \ 2 strLong = Space(lngSpace) & strShort & Space(60 - lngSpace)
Now i use Consolas, for tabular text in my project!
-
- Lounger
- Posts: 27
- Joined: 04 Jun 2013, 08:32
- Location: North Yorkshire, UK
Re: center text in a fixed string space
Wouldn't this add 25 spaces before and 35 after? You need to deduct the length of the string tooHansV wrote: ↑20 Oct 2021, 07:19If you use a fixed-width font such as Courier New or Consolas, you can use:
Code: Select all
strLong = Space(lngSpace) & strShort & Space(60 - lngSpace)
Code: Select all
strLong = Space(lngSpace) & strShort & Space(60 - lngSpace - Len(strShort))
Code: Select all
strLong = Left(Space(lngSpace) & strShort & Space(60), 60)
-
- Administrator
- Posts: 78665
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
-
- BronzeLounger
- Posts: 1499
- Joined: 28 Feb 2015, 13:11
- Location: Hof, Bayern, Germany
Re: center text in a fixed string space
Hello
I expect VB is wanted here, not VBA?
No matter, here’s one that often seems to get forgotten sometimes, using the Mid Statement
Alan
( the VBA equivalent of that would be, I think
)
I expect VB is wanted here, not VBA?
No matter, here’s one that often seems to get forgotten sometimes, using the Mid Statement
Code: Select all
Let strLong = Space(60)
Mid(strLong, lngSpace, Len(strShort)) = strShort
( the VBA equivalent of that would be, I think
Code: Select all
Let strLong = Application.WorksheetFunction.Replace(Space(60), lngSpace, Len(strShort), strShort)
I am having difficulty logging in with this account just now.
You can find me at DocAElstein also
You can find me at DocAElstein also