Excel插入没有条件格式的行

时间:2017-12-05 08:15:38

标签: excel excel-vba vba

我正在处理一个宏来插入一个特定数量的行,到目前为止一直很好。我唯一要努力的是如何清除插入单元格的格式,因为atm插入的行使用上面行的条件格式。

Sub RijInsert()
'
' RijInsert Macro
'
' Sneltoets: Ctrl+Shift+R
'
Dim Rng As Integer
Dim k As Integer
Dim rRange As Range

Set rRange = Selection

Rng = InputBox("Aantal in te voegen rijen?")
For k = 1 To Rng
    Rows(rRange.Row).Insert Shift:=xlDown, _
          CopyOrigin:=xlFormatFromLeftOrAbove
Next
End Sub

谢谢:)

2 个答案:

答案 0 :(得分:0)

首先,您需要存储所选行的行数而非Range对象(因为插入行会移动引用)。然后你可以清除格式:

Sub RijInsert()

    Dim Rng As Integer
    Dim k As Integer
    Dim r As Long

    r = Selection.Row + 1

    Rng = InputBox("Aantal in te voegen rijen?")
    For k = 1 To Rng
        Rows(r).Insert Shift:=xlDown
        Rows(r).FormatConditions.Delete
    Next

End Sub

答案 1 :(得分:0)

试试这个:

Rows(rRange.Row).Offset(-1, 0).ClearFormats

它与其他解决方案类似;)

以下是完整代码:

Sub RijInsert()

Dim Rng As Integer
Dim k As Integer
Dim rRange As Range

Set rRange = Selection

Rng = InputBox("Aantal in te voegen rijen?")
For k = 1 To Rng
    Rows(rRange.Row).Insert Shift:=xlDown, _
          CopyOrigin:=xlFormatFromLeftOrAbove
    Rows(rRange.Row).Offset(-1, 0).ClearFormats
Next
End Sub