Exact SPACE between two strings and proper position for Second String in Multiline Textbox

NimishK

Well-known Member
Joined
Sep 4, 2015
Messages
684
Hello

Can someone refine the below code inorder to get Exact Amount of SPACE between 2 strings
Tried to create function but somehow could not succeed.
I want exact SPACE between Two Strings. So the alignment comes perfectly between Two Strings in Textbox
Somehow the alignment between two strings is not properly aligned with below code
VBA Code:
Option Explicit

Private Sub UserForm_Initialize()
Dim strDescp(4) As String, strQuot(4) As String
Dim strHeader As String
Dim i As Integer, gapSpc As Integer
Dim strData As String

strHeader = "Description" & Space(10) & "Quoted"

strDescp(1) = "The Brown Fox"
strDescp(2) = "quickly Jumped"
strDescp(3) = "over the"
strDescp(4) = "Lazy dogs"

strQuot(1) = "Yes"
strQuot(2) = "No"
strQuot(3) = "No"
strQuot(4) = "Yes"

strData = strHeader & vbCrLf

For i = 1 To UBound(strDescp)
  gapSpc = NewTotSpaces((strDescp(i)), 15, 8)
  strData = strData & strDescp(i) & Space(gapSpc) & strQuot(i) & vbCrLf
Next i

TextBox1.Text = TextBox1.Text & strData
End Sub

Public Function NewTotSpaces(strChar As String, ByVal maxStrLen As Integer, extraSpc As Integer) As Integer

Dim strLen As Integer
Dim newPos As Integer, i As Integer, rMangSpc As Integer, exactSpc As Integer

strLen = Len(strChar)

If strLen >= maxStrLen Then
   rMangSpc = maxStrLen 'strLen
   exactSpc = (rMangSpc - maxStrLen)           'rMangSpc '+ extraSpc
   NewTotSpaces = exactSpc
Else
If strLen < maxStrLen Then
   rMangSpc = maxStrLen - strLen
   exactSpc = rMangSpc + extraSpc
   NewTotSpaces = exactSpc
 
End If
End If

End Function
NimishK
 
Last edited:

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Thanks Zot
my purpose is to use textbox and not listbox nor combobox.

The above coding was done only for two Variables only.

have no idea how you are expecting the output. Is it something like on left Description column and on right Yes/No?
My Output to have proper spacing and for second string to be in a correct position irrespective lenght of 1st string
You may execute the above the code for checking

NimishK
609
 
Upvote 0
My result is like this. I put the code in UserForm. I guess you want it to be like two column ListBox.

The problem is each character has different width. So, it is going to hard to create a virtual vertical line to create same spacing. Maybe I can try something later.
 

Attachments

  • TextBox Align.jpg
    TextBox Align.jpg
    13.8 KB · Views: 3
Upvote 0
The problem is each character has different width. So, it is going to hard to create a virtual vertical line
Exactly. different fonts will have different width

I guess you want it to be like two column ListBox.
Structure should be properly aligned with correct postioning of Second String. FYI I am not all going to use List or combobox
By the way your image indicates you have not put your textbox to Multiline

VBA Code:
Textbox1.Multiline = True
 
Upvote 0
Exactly. different fonts will have different width


Structure should be properly aligned with correct postioning of Second String. FYI I am not all going to use List or combobox
By the way your image indicates you have not put your textbox to Multiline

VBA Code:
Textbox1.Multiline = True
Aaah, Right. No Multiline.

Anyway, would putting two textbox side by side be a possible solution? It would be easier :)
 
Upvote 0
Exactly. different fonts will have different width
Try using monospace font such as Courier New or Consolas.

TextBox1.Font = "Courier New"
 
Upvote 0
Try using monospace font such as Courier New or Consolas.

TextBox1.Font = "Courier New"
That is the best solution. There are a number of monospace fonts out there.
 
Upvote 0
Thanks Zot and Akuini for the suggestion of using Monospace fonts like
"Courrier New", "Consolas"
I tried "Fixedsys" too.
All the above 3 fonts gave right results.
Also how to check for monospace fonts or any link with list of monospace fonts ?

So this meas coding as per # 1 was just perfect. if not you could correct it.


Thanks
NimishK
 
Upvote 0
Thanks Zot and Akuini for the suggestion of using Monospace fonts like
"Courrier New", "Consolas"
I tried "Fixedsys" too.
All the above 3 fonts gave right results.
Also how to check for monospace fonts or any link with list of monospace fonts ?

So this meas coding as per # 1 was just perfect. if not you could correct it.


Thanks
NimishK
I see the result was just fine when I ran your program
 
Upvote 0
Solution

Forum statistics

Threads
1,214,822
Messages
6,121,770
Members
449,049
Latest member
greyangel23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top