Search code examples
excelcopyrow

copy row and all code and conditional format


I’ve created a leave diary for my teams, but I want to create a button to allow to add new members. I have a template row stored on one sheet titled code on row 4 that I want to copy and insert in the next available line whilst keeping a break between the next line with data in on another sheet. Ie line 1:5 staff 6 is blank 7 is other info so when it inserts the new line with the data it will insert it into line 6 then create a blank row for line 7 and move all other rows down one. I want to keep the formulas and conditional format that has been coded into the template line I am copying. I keep getting an error on the first line of code?

ThisWorkbook.sheets(“code”).row(4).copy
ThisWorkbook.sheets(“sheet3”).range(“A1”).end(xlDown).offset(1,0).entirerow.insert

Solution

  • First of all, you should use proper " quotes instead of , as they cannot be interpreted by VBA. Secondly, .Row( should be .Rows(. Thirdly I highly recommend reading this as right now even if your code works it might have unexpected results.

    Sub functioncaller()
    ThisWorkbook.Sheets("Sheet1").Rows(4).Copy
    ThisWorkbook.Sheets("sheet1").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).EntireRow.Insert
    End Sub