vba Excel 宏:如何更改所有行高,但如果 cell.value = bold 使单元格高度更大?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3616610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:03:07  来源:igfitidea点击:

Excel macro: how do I change all row heights, BUT if cell.value = bold make the cell height bigger?

excelvbaformatting

提问by RocketGoal

I'm working on a long list of data (Column B) that has been formatted using boldand indents. The bold cells contain the titles/category names and the indented cell values are the subcategories.

我正在处理使用粗体和缩进格式化的一长串数据(B 列)。粗体单元格包含标题/类别名称,缩进的单元格值是子类别。

The row heights are all over the place. It should have been 10.5 for everything, and the bold cells/rows15. I can change everything to 10.5, but then I need to spend quite a bit of time scrolling through the list amending the bold row heights. I've used the format painter but it's a long list and I didn't want to spend so much time on this part of the process. And now I know that I'll need to do this to another 30 documents.

行高到处都是。一切都应该是 10.5,粗体单元格/行15。我可以将所有内容更改为 10.5,但随后我需要花费相当多的时间滚动列表来修改粗体行高。我使用过格式刷,但它是一个很长的清单,我不想在这部分过程中花费太多时间。现在我知道我需要对另外 30 个文档执行此操作。

Does anyone have a quicker way of doing this?

有没有人有更快的方法来做到这一点?

回答by marg

Sub setHeights()
Dim targetRange As Range
Dim targetCell As Range

    Set targetRange = Range("B:B")
    For Each targetCell In targetRange
        If Not IsEmpty(targetCell) Then
            If targetCell.Font.Bold Then
                targetCell.RowHeight = 15
            ElseIf targetCell.Font.Superscript Then
                targetCell.RowHeight = 12.75
            Else
                targetCell.RowHeight = 10.5
            End If
        End If
    Next targetCell
End Sub

You might want to change Range("B:B")to something like Table1.Range("B1:B255")

您可能想要更改Range("B:B")为类似Table1.Range("B1:B255")

回答by Brian

Rather than setting the range as shown above, Set targetRange = Range("B:B")

而不是如上所示设置范围, Set targetRange = Range("B:B")

Try setting it like this: Set TargetRange = Range("B1", Range("B65536").End(xlUp))

尝试这样设置: Set TargetRange = Range("B1", Range("B65536").End(xlUp))