Sub AlignRight()
Dim Cell As Range
Dim LastRow As Long
Dim Rng As Range
Dim Wks As Worksheet
Set Wks = ActiveSheet
Set Rng = Wks.Range("A1")
Set LastRow = Wks.Cells(Rows.Count, Rng.Column).End(xlUp).Row
Set Rng = Rng.Resize(LastRow - Rng.Row + 1, 1)
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
For Each Cell In Rng
' to test for 4 leading spaces, change the line below to: If Left(Cell, 4) = Space(4) Then
If Mid(Cell, 4, 1) = " " Then
Cell.HorizontalAlignment = xlHAlignRight
End If
Next Cell
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
End With
End Sub
Sub AlignRight()
Dim Cell As Range
Dim LastRow As Long
Dim Rng As Range
Dim Wks As Worksheet
Set Wks = ActiveSheet
Set Rng = Wks.Range("A1")
LastRow = Wks.Cells(Rows.Count, Rng.Column).End(xlUp).Row
Set Rng = Rng.Resize(LastRow - Rng.Row + 1, 1)
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
For Each Cell In Rng
' to test for 4 leading spaces, change the line below to: If Left(Cell, 4) = Space(4) Then
If Mid(Cell, 4, 1) = " " Then
Cell.HorizontalAlignment = xlHAlignRight
End If
Next Cell
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
End With
End Sub
Not sure which option you used from Leith's code, but if your request is to right align all cell that start with 4 spaces, here is a fast (noticeable if you have a huge number of rows to process), non-looping macro that you can also consider...That did the trick. Thank you very much for your help Leith!
Sub RightAlignFourLeadingSpaces()
Application.ReplaceFormat.Clear
Application.ReplaceFormat.HorizontalAlignment = xlHAlignRight
Columns("A").Replace " *", "", xlWhole, SearchFormat:=False, ReplaceFormat:=True
Application.ReplaceFormat.Clear
End Sub