作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试遍历一行中的所有单元格并使用以下条件更改字体大小:
如果工作表中的所有单元格的字体大小都相同,则此方法有效。如果工作表中的任何单元格具有不同的字体大小,它返回 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/
我是一名优秀的程序员,十分优秀!