EASY: VBA to center text and set font color

the2js97

New Member
Joined
Sep 18, 2009
Messages
4
Sounds easy, I want to center the text in Column C. I was trying to use the following code but it failed:
With Sheets("TOC")
.Range("C:C").HorizontalAlignment = xlCenter
End With

Also tried to change the font color of Column B to black by using the following code but it failed:
With Sheets("TOC")
.Range("B:B").Font.Color = RGB(0, 0, 0)
End With

Thanks in advance
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Try this
Code:
Sub Macro1()
    With Sheets("TOC")
        .Columns("C:C").HorizontalAlignment = xlCenter
        .Columns("B:B").Font.Color = RGB(0, 0, 0)
    End With
End Sub

using .Range should have worked too...Im not seeing why it didnt.

Code:
Sub Macro1()
    With Sheets("TOC")
        .Range("C:C").HorizontalAlignment = xlCenter
        .Range("B:B").Font.Color = RGB(0, 0, 0)
    End With
End Sub
 
Upvote 0
Thanks for the new code.

After closing Excel and starting again my original code worked. Sorry for the "head scratching."

Thanks again for your efforts
 
Upvote 0
Black is one of the 8 named Excel colors, you can also refer to it as vbBlack

Here is the list:

vbBlack
vbRed
vbGreen
vbYellow
vbBlue
vbMagenta
vbCyan
vbWhite
 
Upvote 0

Forum statistics

Threads
1,214,912
Messages
6,122,200
Members
449,072
Latest member
DW Draft

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top