VBA - Text centered, underlined and other font color

akalien

New Member
Joined
Jul 2, 2010
Messages
23
Office Version
  1. 2019
Platform
  1. Windows
Hi,

I've got following VBA code in in addition I want the text to be centered, underlined and the font color RED.

Could please omebody help me out.

Many thanks in advance

[TABLE="width: 683"]
<tbody>[TR]
[TD="colspan: 2"]Private Sub Worksheet_Change(ByVal Target As Range)
If Not (Application.Intersect(Target, Range("A1:A10")) _
Is Nothing) Then
With Target
If Not .HasFormula Then
Application.EnableEvents = False
.Value = UCase(.Value)
Application.EnableEvents = True
End If
End With
End If
End Sub
[/TD]
[/TR]
</tbody><colgroup><col><col></colgroup>[/TABLE]
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Try

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not (Application.Intersect(Target, Range("A1:A10")) _
Is Nothing) Then
With Target
If Not .HasFormula Then
Application.EnableEvents = False
.HorizontalAlignment = xlCenter
.Font.Underline = True
.Font.ColorIndex = 3
.Value = UCase(.Value)
Application.EnableEvents = True
End If
End With
End If
End Sub
 
Upvote 0
Solution
Note that both the above codes will error if more than one cell is changed at a time (eg two or more cells are selected and the Delete key is pressed, 2 or more cells are pasted into the range etc).
I would suggest you consider this more robust version
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim changed As Range, c As Range
  
  Set changed = Intersect(Target, Range("A1:A10"))
  If Not changed Is Nothing Then
    Application.EnableEvents = False
    For Each c In changed
      With c
        If Not .HasFormula Then
          .HorizontalAlignment = xlCenter
          .Font.Underline = True
          .Font.ColorIndex = 3
          .Value = UCase(.Value)
        End If
      End With
    Next c
    Application.EnableEvents = True
  End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,624
Latest member
gregg777

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