简体   繁体   English

Excel VBA为插入的行设置行高

[英]Excel VBA set row height for inserted row

I just figured out how to insert rows below cell with value. 我只是想出了如何在带有值的单元格下面插入行。 However as an addition I would like inserted row to be specific height, lets say for example 7. This code is a part of large code so I don't save sub in my example. 但是,另外,我希望插入的行具有特定的高度,比如说7。此代码是大代码的一部分,因此在我的示例中不保存sub

The problem is that this code is not working for setting RowHeight. 问题在于此代码不适用于设置RowHeight。 First part is is inserting new row but there is no effect on Height of the rows. 第一部分是插入新行,但对行高没有影响。

I have tried the following one: 我尝试了以下方法:

Dim rng as Range

        For Each Rng In ThisWorkbook.Worksheets("Offer Letter").Range("E2:E60")
        If Not IsEmpty(Rng) Then
            Rng.Offset(1, 0).EntireRow.Insert
        End If
    Next


        For Each Rng In ThisWorkbook.Worksheets("Offer Letter").Range("E2:E60")
        If Not IsEmpty(Rng) Then
            Rng.Offset(1, 0).EntireRow.RowHeight = 7
        End If
    Next

You don't say what your problem is, but you don't need two loops. 您无需说出问题所在,但不需要两个循环。 Set the height when you insert the row. 插入行时设置高度。

Thanks to @JvdV for pointing out that when deleting or inserting rows one should loop backwards to avoid missing/skipping rows. 感谢@JvdV指出,在删除或插入行时,应该向后循环,以避免丢失/跳过行。

Sub y()

Dim r As Long

With ThisWorkbook.Worksheets("Offer Letter")
    For r = 60 To 2 Step -1
        If Not IsEmpty(.Cells(r, "E")) Then
            .Cells(r + 1, "E").EntireRow.Insert
            .Cells(r + 1, "E").EntireRow.RowHeight = 7
        End If
    Next r
End With

End Sub

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM