设置RowHeight Excel VBA

时间:2018-08-25 20:05:00

标签: excel-vba

我已经努力了几个小时才能将行高设置为隐含范围。该代码对 except 起作用,可解决两个问题:1.将所有包含数据的行都设置为AutoFit,而不仅仅是预期范围; 2.我似乎无法在倒数第二行的行高上添加“ 3”代码行:

Sub SetRH()

    ActiveSheet.Unprotect
    Application.ScreenUpdating = False

    Range("C" & (ActiveCell.row)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection.Offset(0, 0), Selection.Offset(0, 4)).Select

    Selection.sort Key1:=Range("C6"), Order1:=xlAscending, Key2:=Range("E6") _
       , Order2:=xlAscending, Key3:=Range("D6"), Order3:=xlAscending, Header _
        :=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

    For Each row In ActiveSheet.UsedRange.Rows: Rows.AutoFit: Next
    For Each row In ActiveSheet.UsedRange.Rows: Rows.RowHeight = Rows.RowHeight + 3: Next

    Application.ScreenUpdating = True

End Sub

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

下面的代码将遍历每行自动拟合,然后将行高增加+3。

Dim ws As Worksheet
Set ws = ActiveSheet

Dim Rng As Range
Dim cel As Range
Set Rng = Range(ActiveCell, Cells(Rows.Count, ActiveCell.Column).End(xlUp))

For Each cel In Rng
    cel.Rows.AutoFit
    cel.Rows.RowHeight = cel.Rows.RowHeight + 3
Next cel