excel - 循环遍历单元格并更改字体

标签 excel vba fonts size

我正在尝试遍历一行中的所有单元格并使用以下条件更改字体大小:

  • 如果字号小于10,则将字号改为10

如果工作表中的所有单元格的字体大小都相同,则此方法有效。如果工作表中的任何单元格具有不同的字体大小,它返回 null。如果我在 A1 中的字体大小为 8,在 A2 中的大小为 20,则没有任何变化。

Sub SetSheetFont(ws As Worksheet)
    Dim x As Integer
    Dim NumRows As Long
    Application.ScreenUpdating = False
    NumRows = Range("A1", Range("A1").End(xlDown)).Rows.Count
    Range("A1").Select
    With ws
        ' If the font size is lower than 10, set to 10
        For x = 1 To NumRows
            If .Cells.Font.Size < 10 Then .Cells.Font.Size = 10
            ActiveCell.Offset(1, 0).Select
        Next
        Application.ScreenUpdating = True
    End With
End Sub

最终目标是遍历列中的所有单元格,直到有一定数量的空单元格,然后从下一列开始(在本例中为 B1)。

我怎样才能至少在一个专栏中完成这项工作?如果我从那里开始,我很确定我可以让它工作。

最佳答案

您可以遍历 UsedRange

中的所有单元格
Sub SetSheetFont(ws As Worksheet)
    Dim myCell As Range
    Application.ScreenUpdating = False
    With ws
        For each myCell in ws.UsedRange
            ' If the font size is lower than 10, set to 10
            If myCell.Font.Size < 10 Then myCell.Font.Size = 10
        Next
    End With
    Application.ScreenUpdating = True
End Sub

旁注:一般来说,你想要 avoid using select在你的代码中

关于excel - 循环遍历单元格并更改字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59581598/

相关文章:

c# - 为什么在 Excel 中比较 1<' 或将任何数字与带有 < 的任何文本进行比较结果为真?

CSS自定义字体不起作用

java - 如何修复嵌入字体 (Java) 的 FileNotFoundException?

iphone - 内存使用随着 CTFontCreateWithName 和 CTFramesetterRef 的增加而增长

excel - VBA .SetText 和 .PutInClipboard 在剪贴板中放置两个符号而不是所需的数据

excel - 双击时避免在表单中选择

excel - 从 VBA 中的范围中删除特殊字符

vba - 自动生成问题处理

ms-access - Access Vba - 查找空值和空白值

excel - 在两列配对的值中删除每对重复项(行)中的一个