gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:46:38 25 4
gpt4 key购买 nike

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

  • 如果字号小于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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com