Skip to main content

如何根據 Excel 中的單元格值快速更改字體大小?

Author: Sun Last Modified: 2025-08-06
A screenshot showing a table in Excel with varying font sizes based on values in the adjacent column
在 Excel 中,您可以隨心所欲地更改單元格字體大小,但您是否嘗試過根據另一列的值來更改字體大小,如下方截圖所示?本文中,我將介紹一些 VBA 程式碼,幫助您根據單元格值快速更改字體大小。


根據另一列的值更改字體大小

根據單元格值和字符長度更改字體大小

arrow blue right bubble 根據另一列的值更改字體大小

要根據另一列的值更改字體大小,這裡有一段 VBA 程式碼可以幫助您。

1. 轉到您想根據另一列更改字體大小的工作表,並同時按下 Alt + F11 鍵以打開 Microsoft Visual Basic for Applications 視窗。

2. 點擊 插入 > 模組,然後將以下 VBA 程式碼粘貼到模組中。參見截圖:

VBA:根據另一列的值更改字體大小

Sub fontchange()
'UpdatebyExtendoffice20160614
    Dim xRg As Range
    Dim xTxt As String
    Dim xCell As Range
    On Error Resume Next
    If ActiveWindow.RangeSelection.Count > 1 Then
      xTxt = ActiveWindow.RangeSelection.AddressLocal
    Else
      xTxt = ActiveSheet.UsedRange.AddressLocal
    End If
    Set xRg = Application.InputBox("Select cells that you want to change font size:", "Kutools for Excel", xTxt, , , , , 8)
    If xRg Is Nothing Then Exit Sub
    If (xRg.Areas.Count > 1) Or (xRg.Columns.Count > 1) Then
        MsgBox "Only can select one column", vbInformation, "Kutools for Excel"
        Exit Sub
    End If
    Application.ScreenUpdating = False
    For Each xCell In xRg
        xCell.Font.Size = xCell.Offset(, 1).Value
    Next
    Application.ScreenUpdating = True
End Sub

A screenshot showing the pasted code in the VBA editor

3. 按下 F5 鍵,會彈出一個對話框,選擇您想要更改字體大小的單元格。參見截圖:
A screenshot showing the 'Select cells that you want to change font size' dialog

4. 點擊 確定,然後選定列中的單元格將根據相鄰列中的值調整其字體大小。
A screenshot showing the result in Excel after running the VBA code, where font size is adjusted based on adjacent column values


arrow blue right bubble 根據單元格值和字符長度更改字體大小

如果您想根據單元格自身的值或字符長度更改字體大小,例如,當單元格值大於 10 或字符長度超過 5 時,則字體大小變為 16,字體變為 Arial;否則,字體大小為 11,字體為 Calibri,如下方截圖所示。
A screenshot of Excel showing a cell with a font size of 16 and Arial font, and another with a font size of 11 and Calibri font, based on value and character length

1. 按下 Alt + F11 鍵以啟動 Microsoft Visual Basic for Applications 視窗。

2. 點擊 插入 > 模組,並將以下程式碼粘貼到模組腳本中。參見截圖:

VBA:根據單元格值或字符長度更改字體大小

Private Sub Worksheet_Calculate()
'UpdatebyExtendoffice20160614
    Dim xCell As Range
    For Each xCell In Range("G2:H9")
        With xCell
            If Len(.Text) > 5 Or Val(.Value) > 10 Then
                .Font.Name = "Arial"
                .Font.Size = 16
            Else
                .Font.Name = "Calibri"
                .Font.Size = 11
            End If
        End With
    Next
End Sub

A screenshot showing the pasted code in the VBA editor

3. 按下 F5 鍵運行程式碼,值大於 10 或字符長度超過 5 的單元格已將其字體更改為 Arial,字體大小更改為 16。

提示:在上述程式碼中,您可以將範圍 G2:B9 更改為其他所需的範圍。

最佳 Office 辦公效率工具

🤖 Kutools AI Aide:徹底革新數據分析,基於智能執行|生成程式碼|創建自訂公式|分析數據並生成圖表|調用 Kutools Functions
熱門功能查找、選取項目的背景色或標記重複值 | 刪除空行 | 合併列或單元格且不丟失資料 | 四捨五入...
高級 LOOKUP多條件查找|多值查找|多表查找|模糊查找...
高級下拉列表快速創建下拉列表 | 關聯下拉列表 | 多選下拉列表 ...
列管理器添加指定數量的列 | 移動列 | 切換隱藏列的可見狀態 | 區域與列比較 ...
精選功能網格聚焦 | 設計檢視 | 增強編輯欄 | 工作簿及工作表管理器 | 資源庫 (自動文本) | 日期提取器 | 合併資料 | 加密/解密儲存格 | 按清單發送電子郵件 | 超級篩選 | 特殊篩選(篩選粗體/傾斜/刪除線 ...)...
前15 大工具集12 款文本工具添加文本刪除特定字符,...)|50+ 種圖表 類型甘特圖,...)|40+ 實用 公式基於生日計算年齡,...)|19 款插入工具插入QR码按路徑插入圖片,...)|12 款轉換工具金額轉大寫匯率轉換,...)|7 款合併和分割工具高級合併行分割儲存格,...)| ...以及更多
使用 Kutools 支援你的語言——支援英語、西班牙語、德語、法語、中文及40 多種語言!

利用 Kutools for Excel 大幅提升你的 Excel 技能,感受前所未有的高效體驗。 Kutools for Excel 提供超過300 項高級功能,助你提升效率並保存時間。 點此查看你最需要的功能...


Office Tab 為 Office 帶來標籤式介面,讓你的工作更加輕鬆

  • 啟用 Word、Excel、PowerPoint 的標籤式編輯和閱讀功能
  • 在同一個視窗的標籤中打開和創建多個文件,而不是在新窗口中分開開啟。
  • 可提升你50% 的工作效率,每天為你大量減少滑鼠點擊次數!