Try this function:
Rng_fBooleanProperties_ByArray(exRngProp, rTrg, aProperty)
User defined function that sets the following Boolean Range Properties
: AddIndent, Font.Bold, Font.Italic, Font.Strikethrough, Font.Subscript, Font.Superscript, FormulaHidden, Locked, ShrinkToFit, UseStandardHeight, UseStandardWidth and WrapText. Returns True
if successful.
Syntax
exRngProp
As E_RngProp
: Customized Enumeration to define the range property
to be updated.
rTrg
s Range
: Target range to updated.
aProperty
As Variant
: Array of booleans with cells to be updated.
It uses:
• An Array
to hold the Target Range
actual contents
(i.e. Numbers, Text, Logical, Error, Formulas).
• The E_RngProp Enumeration
to define and identify the property to be updated.
• The Range.Value
property to enter the Boolean Array into the Target Range
.
• The Range.Replace
method to change the False
values into empty cells.
• The Range.SpecialCell
method to set the corresponding Range.Property
as required using each Cell.Value
.
This is the code:
Option Explicit
Enum E_RngProp
Rem Range Properties - Boolean & Read\Write
exAddIndent = 1
exFontBold
exFontItalic
exFontStrikethrough
exFontSubscript
exFontSuperscript
exFormulaHidden
exLocked
exShrinkToFit
exUseStandardHeight
exUseStandardWidth
exWrapText
End Enum
Function Rng_fBooleanProperties_ByArray(exRngProp As E_RngProp, rTrg As Range, aProperty As Variant) As Boolean
Dim rPropOn As Range
Dim aFml As Variant
Rem Validate Input
If rTrg Is Nothing Then Exit Function
If Not IsArray(aProperty) Then Exit Function
If rTrg.Rows.Count <> UBound(aProperty) Then Exit Function
If rTrg.Columns.Count <> UBound(aProperty, 2) Then Exit Function
With rTrg
Rem Get Formulas from Target Range
aFml = .Formula
Rem Apply Bold Array to Target Range
.Value = aProperty
.Replace What:=False, Replacement:="", _
LookAt:=xlWhole, SearchOrder:=xlByRows, _
MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
On Error Resume Next
Set rPropOn = .SpecialCells(xlCellTypeConstants, 23)
On Error GoTo 0
Select Case exRngProp
Case exAddIndent
.AddIndent = False
If Not rPropOn Is Nothing Then rPropOn.AddIndent = True
Case exFontBold
.Font.Bold = False
If Not rPropOn Is Nothing Then rPropOn.Font.Bold = True
Case exFontItalic
.Font.Italic = False
If Not rPropOn Is Nothing Then rPropOn.Font.Italic = True
Case exFontStrikethrough
.Font.Strikethrough = False
If Not rPropOn Is Nothing Then rPropOn.Font.Strikethrough = True
Case exFontSubscript
.Font.Subscript = False
If Not rPropOn Is Nothing Then rPropOn.Font.Subscript = True
Case exFontSuperscript
.Font.Superscript = False
If Not rPropOn Is Nothing Then rPropOn.Font.Superscript = True
Case exFormulaHidden
.FormulaHidden = False
If Not rPropOn Is Nothing Then rPropOn.FormulaHidden = True
Case exLocked
.Locked = False
If Not rPropOn Is Nothing Then rPropOn.Locked = True
Case exShrinkToFit
.Locked = False
If Not rPropOn Is Nothing Then rPropOn.ShrinkToFit = True
Case exUseStandardHeight
.UseStandardHeight = False
If Not rPropOn Is Nothing Then rPropOn.UseStandardHeight = True
Case exUseStandardWidth
.UseStandardWidth = False
If Not rPropOn Is Nothing Then rPropOn.UseStandardWidth = True
Case exWrapText
.WrapText = False
If Not rPropOn Is Nothing Then rPropOn.WrapText = True
End Select
Rem Reset Formulas in Target Range
.Formula = aFml
End With
Rem Set Results
Rng_fBooleanProperties_ByArray = True
End Function
Additionally having these lines at the beginning of your main procedure will help to speed up the process:
With Application
.EnableEvents = False
.Calculation = xlCalculationManual
.ScreenUpdating = False
.DisplayAlerts = False
End With
And these lines at the end of your main procedure:
With Application
.DisplayAlerts = True
.Calculation = xlCalculationAutomatic
.EnableEvents = True
.ScreenUpdating = True
End With
The function can be called using any of these methods:
If Not (Rng_fBooleanProperties_ByArray(exFontBold, rTrg, aBold)) Then GoTo Error_Message
OR
Call Rng_fBooleanProperties_ByArray(exFontItalic, rTrg, aItalic)
OR
Rng_fBooleanProperties_ByArray exFontStrikethrough, rTrg, aStrikethrough
Suggest to read the following pages to gain a deeper understanding of the resources used:
Enum Statement, Function Statement, On Error Statement,
Range Object (Excel), Range.Replace Method (Excel), Range.SpecialCells Method (Excel),
Select Case Statement, Using Arrays, With Statement.