Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VBA to change font size for cell with large text

Posted on 2012-03-12
7
Medium Priority
?
297 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 42

Accepted Solution

by:
dlmille earned 2000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 42

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 42

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 42

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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

610 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