Link to home
Start Free TrialLog in
Avatar of John Smith
John SmithFlag for Australia

asked on

How can I change selection into a nominated Range to change formatting?

The following code works if using "Selection" in place of range "N29:N32". Can someone show how to use a nominated range in place of "selection", Please?

Sub LTrimSel2()
'This macro changes cell value to NumberFormat "0.00" & HorizontalAlignment "Centre"

Dim cell As Range
Dim TZone As Range

Set TZone = ActiveSheet.Range("N29:N32")
Range("N29").Select
   
    For Each cell In TZone
        cell.Value = LTrim(cell.Value)
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlTop
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        Selection.NumberFormat = "0.00"
    Next cell
   
'Set TZone = Nothing

End Sub
ASKER CERTIFIED SOLUTION
Avatar of Steve
Steve
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of John Smith

ASKER

The changes work thanks you.
You may also find that the following works (and faster):

Sub LTrimSel2()
'This macro changes cell value to NumberFormat "0.00" & HorizontalAlignment "Centre"
Dim TZone As Range

Set TZone = ActiveSheet.Range("N29:N32")
    With TZone
            .Value = LTrim(.Value)
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlTop
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
            .NumberFormat = "0.00"
    End With
Set TZone = Nothing

End Sub 

Open in new window