VBA to copy row from one sheet and paste above row on another sheet

kwooden

New Member
Joined
May 5, 2021
Messages
11
Office Version
  1. 365
Platform
  1. Windows
I have a workbook with 2 worksheets, one named "Timesheet" and the other named "Data".

1685199497532.png



1685197571434.png


I created a button on the "Timesheet" worksheet (Add Work) that will insert a blank row on the row directly above the button. Row 2 on the "Data" worksheet has code in some of the cells and I want to insert this row instead of a blank row. I need row 2 from the "Data" worksheet to be copied and inserted on the row directly above the button each time the button is selected so that the inserted row is always the last row inserted.

For example
1685199280903.png


If I click the button again it should copy and insert row 2 from "Data" worksheet directly underneath the 12:00 entry line.

Below is the VBA I use to insert the blank row.
1685197977866.png
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Perhaps this.
VBA Code:
Sub Add_Work_Lines_Above_Button()
    Dim d As Long, I As Long
    Dim c As Range

    With ThisWorkbook.Worksheets("Data")
        Set c = .Range("B2", .Cells(2, .Columns.Count).End(xlToLeft))
    End With

    With ThisWorkbook.Worksheets("TimeSheet")
        d = .Range("A:A").End(xlDown).Row
        For I = d To 2 Step -1
            If .Cells(I, 1).Value = "SatLastRow" Then
                .Rows(.Cells(I, 1).Row).Insert shift:=xlDown
                c.Copy .Cells(I, 1).Offset(0, 1)
            End If
        Next I
    End With
End Sub

FWIW, when you post an image of your data instead of something that can be copied and pasted into a spreadsheet, it is difficult for others to experiment with it. Which means your chances of getting help drop significantly. Instead, use the free XL2BB tool (link below) to post your data in a way that makes it accessible to others. this free tool instead to post some sample data.
 
Upvote 0

rlv01,​


Thank you so much for the chat and for the information about the XLl2BB tool. The chat works great and does exactly what I needed.
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,164
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top