Solved

VBA to change font size for cell with large text

Posted on 2012-03-12
7
290 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
ID: 37712872
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
ID: 37712886
Thank you Dave, works great
0
 

Author Comment

by:rsen1
ID: 37712913
Dave it works great however it is very slow to calculate any suggestions? Thank you
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 41

Expert Comment

by:dlmille
ID: 37712918
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
ID: 37712921
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
ID: 37712943
Yes, thank you, the last one is great
0
 
LVL 41

Expert Comment

by:dlmille
ID: 37712961
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you how to use shortcut menus in the Access run-time environment.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

815 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

11 Experts available now in Live!

Get 1:1 Help Now