How to align numbers right and text left in 1 cell with macro

lehota

New Member
Joined
Jul 16, 2012
Messages
2
Hello,

I would like to know if it is possible to apply 2 different alignments within 1 cell with a macro - I would like to do something like this (see below) but do not know how to do it to align $Ms below each other exactly.

Opening balance 1.0 $M
Movements w/o EAFE 4.0 $M

My code looks like this:

ce = Sheets("Fluctuation analysis for CF100").Cells(c, 3) & WorksheetFunction.Rept(" ", 100 - Len(Sheets("Fluctuation analysis for CF100").Cells(c, 3))) & WorksheetFunction.Text(WorksheetFunction.Round(Sheets("Fluctuation analysis for CF100").Cells(c, 4), 1), "#,##0.0") & " $M " & Chr(10)

I want data in bold to be right aligned to the right border of the cell while everything else should be left aligned to the left border of the cell
 

Some videos you may like

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

Jerry Sullivan

MrExcel MVP
Joined
Mar 18, 2010
Messages
8,787
Hi and Welcome to the Board,

Here's one approach that uses the Distributed HorizontalAlignment ...

Code:
Sub Align_Left_and_Right()
    Dim sLeftText As String, sRightText As String
    Dim c As Long
    c = 2

    With Sheets("Fluctuation analysis for CF100")
        sLeftText = Replace(.Cells(c, 3), " ", Chr(160))
        sRightText = Replace(Format(Round(.Cells(c, 4), 1), "#,##0.0") _
            & " $M ", " ", Chr(160))

         With .Cells(c, 5)
            .HorizontalAlignment = xlDistributed
            .Value = sLeftText & " " & sRightText
        End With
    End With
End Sub

Chr(160) is a non-breaking space character.
 
Last edited:

Watch MrExcel Video

Forum statistics

Threads
1,122,842
Messages
5,598,396
Members
414,235
Latest member
sucosama

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
Top