Solved

VBA to change font size for cell with large text

Posted on 2012-03-12
7
286 Views
Last Modified: 2012-03-12
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
0
Comment
Question by:rsen1
  • 4
  • 3
7 Comments
 
LVL 41

Accepted Solution

by:
dlmille earned 500 total points
Comment Utility
Assuming you want to do this test on every cell in the range(C13:U500):

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

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

Open in new window



Dave
0
 

Author Comment

by:rsen1
Comment Utility
Thank you Dave, works great
0
 

Author Comment

by:rsen1
Comment Utility
Dave it works great however it is very slow to calculate any suggestions? Thank you
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 41

Expert Comment

by:dlmille
Comment Utility
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
0
 
LVL 41

Expert Comment

by:dlmille
Comment Utility
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
0
 

Author Comment

by:rsen1
Comment Utility
Yes, thank you, the last one is great
0
 
LVL 41

Expert Comment

by:dlmille
Comment Utility
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
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all the…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now