Home » excel » excel – Event to change font size if character counter greater then 100

excel – Event to change font size if character counter greater then 100

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have this function where anytime a cell inside the specific range changes, calls a function.

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("A1:L60")) Is Nothing Then
    Call fit_text
    End If
End Sub

The function fit_text changes the font size of the value of the active cell.

Sub fit_text()
    MsgBox ActiveCell.Characters.Count

    If ActiveCell.Characters.Count > 100 Then
        ActiveCell.Font.Size = 8
    Else
        ActiveCell.Font.Size = 10
    End If
End Sub

PROBLEM: whenever I change the value of a cell where the character count is bigger then 100, the font size remains 10 and the message box that tells the value of the count shows 0, but whenever I run it on vba the message box shows the correct value and changes the font size if the count is bigger then 100. I need it to be automatic. Can´t change the height or the width of the cells

How to&Answers:

Note that Excel can automatically shrink the font size to fit into the cell. Therefore select your cell, press Ctrl+1 go to the Alignment tab and select Shrink To Fit.


To fix your code:
Don’t use ActiveCell. Use Target or the Intersect range instead. The ActiveCell might not be the cell that was changed. And also Target can be multiple cells so you need to loop through all the changed cells and test each cell individually.

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim AffectedRange As Range
    Set AffectedRange = Intersect(Target, Target.Parent.Range("A1:L60"))

    If Not AffectedRange Is Nothing Then
        Dim Cell As Range
        For Each Cell In AffectedRange 'loop through all changed cells
            MsgBox Len(Cell.Value)

            If Len(Cell.Value) > 100 Then
                Cell.Font.Size = 8
            Else
                Cell.Font.Size = 10
            End If
        Next Cell
    End If
End Sub

Answer:

ActiveCell is the one active after the Change event. You can pass Target from the event to your method fit_text, so that it will always refer to the changed cells:

Private Sub Worksheet_Change(ByVal target As Range)
 If Not Intersect(target, Range("A1:L60")) Is Nothing Then
    Call fit_text(target)
    End If
End Sub


Sub fit_text(target As Range)
    MsgBox ActiveCell.Address(False, False)
    MsgBox target.Characters.Count

'    If ActiveCell.Characters.Count > 100 Then
'        ActiveCell.Font.Size = 8
'    Else
'        ActiveCell.Font.Size = 10
'    End If

If target.Characters.Count > 100 Then
        target.Font.Size = 8
    Else
        target.Font.Size = 10
    End If
End Sub

You will also want to include a check for when Target is more than a single cell; in which case you will probably want your procedure to check each cell’s content.

Answer:

The problem is the ‘ActiveCell’.

For example when you edit the Cell A1 and press enter, the ActiveCell you are using in fit_text is not A1, but A2.

This however can easily fixed, by just passing the Cell from the Worksheet_Change to fit_text.

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("A1:L60")) Is Nothing Then
        'Pass the Target to 'fit_text'
        Call fit_text(Target)
    End If
End Sub

Sub fit_text(Cell)
    'Instead of using ActiveCell, use Cell (which is the passed Target)
    MsgBox Cell.Characters.Count

    If Cell.Characters.Count > 100 Then
        Cell.Font.Size = 8
    Else
        Cell.Font.Size = 10
    End If
End Sub