Setting RowHeight Excel VBA

限于喜欢 提交于 2021-02-10 14:49:01

问题


I've been struggling for several hours to set row heights for an implied range. The code works except for two problems 1. ALL rows with data are set to AutoFit instead of just the intended range and 2. I cannot seem to add '3' to the row height per the 2nd to last line of code:

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

Any help is much appreciated!


回答1:


The below code will loop through each row auto fit and then increase the row height by +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


来源:https://stackoverflow.com/questions/52020842/setting-rowheight-excel-vba

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!