Solved

VBA to change font size for cell with large text

Posted on 2012-03-12
7
287 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

932 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

14 Experts available now in Live!

Get 1:1 Help Now