简体   繁体   中英

VBA .ClearContents for inserted Rows?

So I have a code that finds three rows and insert the same rows below them, but I need to clear contents of these inserted rows, unfortunately, the selecetion applies for the first three rows and not those inserted.

    Sub Add_Timber()
'
' Add_Timber Macro
'

'
    Cells.Find(What:="Timber | Zakázky:", After:=ActiveCell, LookIn:= _
        xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
        xlNext, MatchCase:=False, SearchFormat:=False).Activate
    Selection.EntireRow.Copy
    Selection.Offset(3, 0).Insert Shift:=xlDown


    Cells.Find(What:="Timber | JIRA ID:", After:=ActiveCell, LookIn:= _
        xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
        xlNext, MatchCase:=False, SearchFormat:=False).Activate
    Selection.EntireRow.Copy
    Selection.Offset(3, 0).Insert Shift:=xlDown


    Cells.Find(What:="Timber | Hodiny:", After:=ActiveCell, LookIn:= _
        xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
        xlNext, MatchCase:=False, SearchFormat:=False).Activate
    Selection.EntireRow.Copy
    Selection.Offset(3, 0).Insert Shift:=xlDown


End Sub

So in picture a red block is copied from the previous and I want to delete the data in the NEW INSERTED rows.

Since rows are Always conscutive and in the same ordwer, then you could make copy/insert in one shot

Sub Add_Timber()

    Dim f As Range
    Set f = Cells.Find(What:="Timber | Zakázky:", After:=ActiveCell, LookIn:= _
        xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
        xlNext, MatchCase:=False, SearchFormat:=False)' try finding "Timber | Zakázky:"

    If Not f Is Nothing Then 'if succesfull
        With f.Resize(3).EntireRow ' reference found cell row along with its two consecutive ones
            .Offset(3).Insert ' insert referenced rows three rows below the referenced ones
            .Copy .Offset(3) ' copy referenced rows three rows below
        End With
    End If

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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