VBA Spacing between two words

jensslofstra

New Member
Joined
Sep 13, 2016
Messages
9
Hi everyone,
I've been trying the whole day but can't figure it out.
I need to align words in two rows. (It's used in an automatic header.)
What I've stumbled upon was that adding spaces doesn't automaticly mean words align perfectly.

What is want to accomplish is something like this.

[TABLE="width: 500"]
<tbody>[TR]
[TD]Customer:[/TD]
[TD]Mister John Doe[/TD]
[/TR]
[TR]
[TD]Company:[/TD]
[TD]John's Enterprice[/TD]
[/TR]
</tbody>[/TABLE]

I've tried this code already and also used Chr(9) and vbTab instead of spaces.
Code:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
    With Worksheets("Offerte").PageSetup
        .LeftHeader = Worksheets("Sheet1").Range("B3") & Space(16) & Worksheets("Sheet1").Range("C3") & Chr(10) & Worksheets("Sheet1").Range("B4") & Space(14) & Worksheets("Sheet1").Range("C4")
    End With
End Sub
 

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.
Hi there. The difficulty you are having is down to which font you are using. Many (or even most) fonts use a variable amount of width for different letters (an i is much narrower than a w), so alignment with that type of font is impossible. If you change the cell font to a monospace font (monospace means every character is given the same width) - courier is the most popular but there are many others - then you will be able to position stuff exactly. (or is it possible to use 2 columns?).
 
Upvote 0
You must change the font in the header.

Try this:

Code:
    With Worksheets("Offerte").PageSetup
        .LeftHeader = [COLOR=#0000ff]"&""Consolas,Normal"""[/COLOR] & _
            Worksheets("Sheet1").Range("B3") & Space([COLOR=#0000ff]15[/COLOR]) & _
            Worksheets("Sheet1").Range("C3") & Chr(10) & _
            Worksheets("Sheet1").Range("B4") & Space([COLOR=#0000ff]16[/COLOR]) & _
            Worksheets("Sheet1").Range("C4")
    End With

Print Preview:

8430dcc97925001b9c275b2e40151049.jpg
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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