Yoshua
Yoshua

Reputation: 11

Alignment to center in excel VBA: Code is not working

Sub belajar_case()
Dim nilai As Single
Dim huruf As String
nilai = Cells(1, 1).Value

Range("A1:B1").Select
With Selection
.HorizontalAlignment = x1Center
End With

Select Case nilai
Case 0 To 20
huruf = "F"
Cells(1, 2) = huruf

Case 20 To 100
huruf = "E-A"
Cells(1, 2) = huruf

End Select
End Sub

This is my code. I want to align the cell to center, but it doesn't work. Message pop up and says "Run-time error '1004':Unable to set the HorizontalAlignment property of the Range class." When I debugged, it referred to line: .HorizontalAlignment = x1Center

Why is that so?

Upvotes: 0

Views: 9676

Answers (1)

user4066647
user4066647

Reputation:

You seem to have copied the code from somewhere. Or you made a typo.

It's not

x1Center: The second character is a numeric one (1)

It should be

xlCenter: The second character should be a l (alphabet)

Upvotes: 3

Related Questions