• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Horizontal Alignment

pradeep kalele

New Member
I have the following code for excel vba. The last line (I made it bold for your convenience) intend to align the contents of the cell in the center. I am receiving error as "Unable to set the HorizontalAlingment property of the range class." Kindly help
Sub SetGrades()

Dim Score As Integer

Score = ActiveCell.Value

If Score >= 90 And Score <= 100 Then

ActiveCell(1, 2).Value = "A"
ActiveCell(1, 2).Interior.ColorIndex = 4
ActiveCell(1, 2).HorizontalAlignment = xlCentre

End If

End Sub
 
Hi ,

Try this :
Code:
Sub SetGrades()
    Dim Score As Integer

    With ActiveCell
        Score = .Value

        If Score >= 90 And Score <= 100 Then
            .Offset(, 1).Value = "A"
            .Offset(, 1).Interior.ColorIndex = 4
            .Offset(, 1).HorizontalAlignment = 3
        End If
    End With
End Sub
Narayan
 
Hi ,

Try this :
Code:
Sub SetGrades()
    Dim Score As Integer

    With ActiveCell
        Score = .Value

        If Score >= 90 And Score <= 100 Then
            .Offset(, 1).Value = "A"
            .Offset(, 1).Interior.ColorIndex = 4
            .Offset(, 1).HorizontalAlignment = 3
        End If
    End With
End Sub
Narayan

Oh! Thank you very much, Narayan Sir! It works. May I further request you to kindly tell me why the earlier error was coming as I used that from a tutorial. It this a recent change in VBA command?
 
Back
Top