Change alignment based upon cell value & loop thru mutliple worksheets

NoviceLois

New Member
Joined
Nov 22, 2019
Messages
12
Office Version
  1. 2016
Platform
  1. Windows
I am new to VBA and programming in general. I am trying to right align all the values in cells, unless they contain "R", then I want the "R" center aligned, bolded and in red. On each worksheet in the workbook. Thru the generosity of others I was able to find code to eliminate worksheets I do not want changed, and to right align. I used a macro to get the code for conditional formatting to make the "R" bolded Red. But to center align just the cells with the "R" has yielded nothing but compile errors. Thank you in advance for your kind assistance.

Here is the code so far.

Code:
Option Explicit
Sub rgtalgnredRctr()
Dim rs As Worksheet
Dim Cell As Range
For Each rs In ThisWorkbook.Worksheets
If rs.Name <> "Data" And rs.Name <> "Tools" Then
Range("E4").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
With Selection '
' Rightjustify Macro
'
.HorizontalAlignment = xlRight
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
End If

Range("E2").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
Formula1:="=""R"""
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Bold = True
.Italic = False
.Color = -16777024
.TintAndShade = 0

End With

Range("E2").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
For Each Cell In Selection
If Cell.Value = "R" Then
Cell.HorizontalAlignment = xlCenter
End If

Next Cell

End Sub
 
Last edited by a moderator:
JLGWhiz, you are a wizard! It works perfectly. Thank you, thank you, thank you!
I hope this thread will help someone else in the future.
Best wishes always to you!
You're welcome,
regards, JLG
 
Upvote 0

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.

Forum statistics

Threads
1,215,051
Messages
6,122,871
Members
449,097
Latest member
dbomb1414

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