Avatar of rsen1
rsen1
 asked on

VBA to change font size for cell with large text

Hi guys,

The following code works for a cell
If Len([C13]) > 27 Then [C13].Font.Size = 8 Else [C13].Font.Size = 9

however I need to cover a worksheet Range(C13:U500)

Please send the correct code.

Thank you,

Robert
Microsoft ExcelMicrosoft OfficeMicrosoft Applications

Avatar of undefined
Last Comment
dlmille

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
dlmille

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
rsen1

ASKER
Thank you Dave, works great
rsen1

ASKER
Dave it works great however it is very slow to calculate any suggestions? Thank you
dlmille

Sub changeFontV2()
Dim r As Range
Dim rng As Range

    Set rng = Range("C13:U500")
    
    rng.Font.Size = 9
    For Each r In rng
        If Len(r.Value) > 27 Then
            r.Font.Size = 8
        End If
    Next r
End Sub

Open in new window


Dave
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
dlmille

And then, try this:

Sub changeFontV3()
Dim r As Range
Dim rng As Range
Dim rChange As Range

    Set rng = Range("C13:U500")
    
    rng.Font.Size = 9
    For Each r In rng
        If Len(r.Value) > 27 Then
            If rChange Is Nothing Then
                Set rChange = r
            Else
                Set rChange = Union(rChange, r)
        End If
    Next r
    rChange.Font.Size = 8
End Sub

Open in new window


Are any of these faster?

Dave
rsen1

ASKER
Yes, thank you, the last one is great
dlmille

Unless it gets VERY VERY large, the Union should be a pretty fast player.  I sometimes do that to "mark" ranges that I want to do something with like delete, or whatever - and it occurred to me it might speed you up with font changes.

Take care,

Dave
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.