Excel auto format cell's font size based on number value?

6,605

You will need a macro in VBA, like this:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim wk As Workbook
Set wk = ThisWorkbook
Dim ws As Worksheet
Set ws = ActiveSheet
CRow = Target.Row
CColumn = Target.Column
If CColumn = X Then
    CellValue = Cells(CRow, CColumn)
    Select Case CellValue
        Case Is < 0
            Cells(CRow, CColumn).Font.Size = 8
        Case 1 To 100
            Cells(CRow, CColumn).Font.Size = 10
        Case 101 To 500
            Cells(CRow, CColumn).Font.Size = 12
        Case 501 To 1000
            Cells(CRow, CColumn).Font.Size = 14
        Case 1001 To 5000
            Cells(CRow, CColumn).Font.Size = 18
        Case 5001 To 10000
            Cells(CRow, CColumn).Font.Size = 22
        Case Else
            Cells(CRow, CColumn).Font.Size = 24

    End Select
End If
End Sub

You have to copy the code, go to View -> Macros in Excel, Create a new one (any name is valid) and on the left column double click the worksheet where you want to use it (red flag in the picture) and in the right side, paste the code.

This macro modifies the font size of the cell in the column X of the worksheet, when it detects that the cell has changed. X is the number of the column (A=1, B=2, C=3, ....)

Double click on Sheet

I defined some ranges of values, for example if the value is between 1 and 100 the font size will be 10. You can add, remove or change it according to your needs.

Keep in mind that when you are going to save it, you have to use .xlsm instead of the typical .xlsx. Also, when you open it, Excel always disable macros and shows a security warning asking you if you want to enable macros

One such sample Excel file can be downloaded here.

Share:
6,605

Related videos on Youtube

Nam G VU
Author by

Nam G VU

Updated on September 18, 2022

Comments

  • Nam G VU
    Nam G VU over 1 year

    I want to make cell's number values to be smaller, in font size, with small values i.e. the bigger the cell value is, the bigger is the font size.

    I can only find Data Bars in Conditional Formattting.

    If you know how to do this, please share.

    enter image description here

  • Nam G VU
    Nam G VU about 9 years
    The font size box is disabled there @jcbermu
  • jcbermu
    jcbermu about 9 years
    You're right. I'll correct my answer
  • jcbermu
    jcbermu about 9 years
    I added VBA code that does the formatting that you need
  • Nam G VU
    Nam G VU about 9 years
    How can I use your VBA function @jcbermu?
  • jcbermu
    jcbermu about 9 years
    Copy the code, then open macros in Excel. On the left column double click the worksheet where you want to use it and in the right side, paste it. That's all. It will change the font size of a cell whenever there is a change in it.
  • Nam G VU
    Nam G VU about 9 years
    How to trigger the code? I pasted and nothing happen.
  • jcbermu
    jcbermu about 9 years
    I added an image to clarify the instructions.
  • Nam G VU
    Nam G VU about 9 years
    It works now and I added one sample in your answer. It's nice. Though, I'm looking for way to format just for a selected column and I'm not good at VBA. May you help me more on that?
  • jcbermu
    jcbermu about 9 years
    For just one column, 2 steps: FIRST. Add below the line that says CColumn = Target.Column this line: If CColumn = X Then. X is the number of the column that you want to be formatted, for example for B is 2, for C is 3... and so on. SECOND.Add below End Selectthis line: End If
  • Nam G VU
    Nam G VU about 9 years
    Please update your answer so that to get accepted. Thank you.
  • jcbermu
    jcbermu about 9 years
    Updated for a single column