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
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
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:
Upvote 0

Forum statistics

Threads
1,215,527
Messages
6,125,337
Members
449,218
Latest member
Excel Master

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