Centering Text Not Working

C

caldog

I have this code that bolds and that puts color as background if certain
conditions are met. What is not happening is that the text centering line is
erroring out. Can somebody explain why this is.

Steve

Private Sub AAA1_Click()

If Range("A1") = 1 Then
Range("A1").Select
With Selection.Font
.HorizontalAlignment = xlCenter - "Line not working"
.VerticalAlignment = xlCenter - "Line not working"
.ColorIndex = 3
.FontStyle = "Bold"
End With
With Selection.Interior
.ColorIndex = 1
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
End With
Else
Range("A1").Select
With Selection.Interior
.ColorIndex = 29
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
End With
End If
 
D

Dave Peterson

It's the range that has the .horizontalalignment property:

Option Explicit

Private Sub AAA1_Click()
With Range("A1")
If .Value = 1 Then
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
'you change it to 1 later????
.Interior.ColorIndex = 3
.Font.Bold = True
With .Interior
.ColorIndex = 1
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
End With
Else
With .Interior
.ColorIndex = 29
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
End With
End If
End With

End Sub
 
C

chijanzen

caldog:

With Selection
.HorizontalAlignment = xlCenter '- "Line not working"
.VerticalAlignment = xlCenter '-"Line not working"
.Font.ColorIndex = 3
.Font.FontStyle = "Bold"
End With
 
G

Gord Dibben

Steve

Font doesn't have an alignment property.

You can End With after the 2 alignment lines and then start another With
statement for the Font or just amend as follows.

Private Sub AAA1_Click()

If Range("A1") = 1 Then
Range("A1").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.Font.ColorIndex = 3
.Font.FontStyle = "Bold"
End With
With Selection.Interior
.ColorIndex = 1
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
End With
Else
Range("A1").Select
With Selection.Interior
.ColorIndex = 29
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
End With
End If
End Sub


Gord Dibben Excel MVP
 
C

caldog

To Chijanzen, Dave Peterson, & Gord

Thanks for your replys. You supplied me with the answers that I need.

May all of you have a great holiday season.

Steve
 
G

Gord Dibben

Thanks for the feedback and Happy Holidays to you and yours from all of us.

Gord
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top