vba 多个工作表,格式第 1 行粗体和自动换行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28687108/
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-12 06:20:35  来源:igfitidea点击:

Multiple Sheets, format row 1 bold and word-wrap

excel-vbavbaexcel

提问by Tony Lima

OK, Excel VBA has defeated me again. I suspect that when I see the answer I'll feel like an idiot. Luckily, as I get older I'm gradually adjusting to that. Here's my code:

好吧,Excel VBA 又打败了我。我怀疑当我看到答案时,我会觉得自己像个白痴。幸运的是,随着年龄的增长,我逐渐适应了这一点。这是我的代码:

Option Explicit

Sub FormatAllSheets()
    Dim shNames()
    ReDim shNames(Worksheets.Count - 1)
    Dim shIndex As Integer
    For shIndex = 0 To UBound(shNames)
        shNames(shIndex) = Worksheets(shIndex + 1).Name
    Next shIndex

    Range("A1", "ZZ1").Select

    Sheets(shNames).Select
    Selection.Font.Bold = True
    Selection.WrapText = True
End Sub

Many thanks, Tony Lima

非常感谢,托尼·利马

回答by user3561813

The easiest method is probably as follow:

最简单的方法大概如下:

Sub FormatFirstRow()
    Dim sh As Worksheet

    For Each sh In ActiveWorkbook.Worksheets
        With sh.Range("A1:ZZ1")
            .Font.Bold = True
            .WrapText = True
        End With
    Next sh
End Sub

回答by Mark Wickett

user3561813 just beat me to the full answer, but I would change the Withline to this:

user3561813 只是击败了我的完整答案,但我会将这一With行更改为:

With sh.Rows(1)

That way you don't have to assume anything about the width of the first row

这样你就不必假设第一行的宽度

回答by Dan Donoghue

OK, Played around with this, there is a way to do it without looping but the problem is you have to do a select (Selects are my pet hate):

好的,玩这个,有一种方法可以不循环,但问题是你必须做一个选择(选择是我最讨厌的):

Sub BoldFirst()
    Worksheets.Select
    Rows(1).select
    Selection.Font.Bold = true
    Selection.WrapText = True
End Sub

I am not 100% sure but I think Select Methodis necessary to activate the row across the multiple sheets. If you don't execute Selectit only seems to bold in the active sheet.

我不是 100% 确定,但我认为Select Method有必要激活多张纸上的行。如果您不执行Select它,它似乎只会在活动工作表中加粗。

It may or may not be faster than looping the sheets. It would be interesting to see if there is a performance difference.

它可能比循环工作表快,也可能不快。看看是否存在性能差异会很有趣。