Search code examples
vbaexcelalignmentright-align

If condition to check if content in cell is right aligned not working


I have a data that is copied and pasted from a pdf file and then split using 'texttocolumns'. I use this data to extract specific information into another excel. All this is done using vba.

As a part of this, I have the below code to check if contents in cell A12 (example) is right aligned. Although the content in the cell "appears" right aligned, the vba does not show the result as 'True'.

If Sheets("Test").Range("A12").HorizontalAlignment = xlRight Then
''Do something
End if

Test data

Test data


Solution

  • numbers are right aligned by default

    so you may want to use this

    With Sheets("Test").Range("A12")
        If .HorizontalAlignment = xlRight Or (IsNumeric(.Value) And .HorizontalAlignment = xlGeneral) Then
            MsgBox cell.Value
        End If
    End With