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

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

Forum statistics

Threads
1,214,932
Messages
6,122,334
Members
449,077
Latest member
Jocksteriom

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