re-work this macro (create new row)

M3L333

New Member
Joined
Jul 31, 2018
Messages
15
Instead of "find next row to paste to on Discharges", how could I have this macro create a new row between row 1 & 2 to paste to information there. This way, my most current information would always be on top.

Private Sub CommandButton1_Click()

Dim lr As Long
Dim r As Long
Dim nr As Long

Application.ScreenUpdating = False

' Find last row in column P with data on Bed Registry
lr = Sheets("Bed Registry").Cells(Rows.Count, "P").End(xlUp).Row

' Loop through all rows on Bed Registry and check column P for closed
For r = 2 To lr
If Sheets("Bed Registry").Cells(r, "P") = "CLOSED" Then
' Find next row to paste to on Discharges
nr = Sheets("Discharges").Cells(Rows.Count, "P").End(xlUp).Row + 1
' Copy columns B-P to Discharges
Sheets("Bed Registry").Range(Cells(r, "B"), Cells(r, "P")).Copy Sheets("Discharges").Cells(nr, "B")
' Clear columns B-P on Bed Registry
Sheets("Bed Registry").Range(Cells(r, "B"), Cells(r, "P")).ClearContents
End If
Next r

Application.ScreenUpdating = True

End Sub
 
It looks like you are commenting out necessary lines (not sure why you are doing that).
Does this work?
Code:
Private Sub CommandButton1_Click()

Dim lr As Long
Dim r As Long

Application.ScreenUpdating = False

'Find lr column P with data on Bed Registry
lr = Sheets("Bed Registry").Cells(Rows.Count, "P").End(xlUp).Row

'Loop through all rows on Bed Registry and check column Pfor closed
For r = 2 To lr
    If Sheets("Bed Registry").Cells(r, "P") = "CLOSED" Then
        Sheets("Discharges").Activate
        'Insert r at r2
        Rows("2:2").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        'Copy columns B-P to Discharges on row 2
        Sheets("Bed Registry").Range(Cells(r, "B"), Cells(r, "P")).Copy Sheets("Discharges").Cells(2, "B")
        'Clear columns B-P on Bed Registry
        Sheets("Bed Registry").Range(Cells(r, "B"), Cells(r, "P")).ClearContents
    End If
Next r

End Sub
 
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
It looks like you are commenting out necessary lines (not sure why you are doing that).
Does this work?
Code:
Private Sub CommandButton1_Click()

Dim lr As Long
Dim r As Long

Application.ScreenUpdating = False

'Find lr column P with data on Bed Registry
lr = Sheets("Bed Registry").Cells(Rows.Count, "P").End(xlUp).Row

'Loop through all rows on Bed Registry and check column Pfor closed
For r = 2 To lr
    If Sheets("Bed Registry").Cells(r, "P") = "CLOSED" Then
        Sheets("Discharges").Activate
        'Insert r at r2
        Rows("2:2").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        'Copy columns B-P to Discharges on row 2
        Sheets("Bed Registry").Range(Cells(r, "B"), Cells(r, "P")).Copy Sheets("Discharges").Cells(2, "B")
        'Clear columns B-P on Bed Registry
        Sheets("Bed Registry").Range(Cells(r, "B"), Cells(r, "P")).ClearContents
    End If
Next r

Application.ScreenUpdating = True

End Sub
 
Upvote 0
Thanks, but this is not working. The information is not transferring and rows are now being created on both worksheets.
 
Upvote 0
OK, I recreated the whole scenario, and it looks like it wanted one more "Activate" line.
This worked for me:
Code:
Private Sub CommandButton1_Click()

Dim lr As Long
Dim r As Long

Application.ScreenUpdating = False

'Find lr column P with data on Bed Registry
lr = Sheets("Bed Registry").Cells(Rows.Count, "P").End(xlUp).Row

'Loop through all rows on Bed Registry and check column Pfor closed
For r = 2 To lr
    If Sheets("Bed Registry").Cells(r, "P") = "CLOSED" Then
        Sheets("Discharges").Activate
        'Insert r at r2
        Rows("2:2").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        'Copy columns B-P to Discharges on row 2
        Sheets("Bed Registry").Activate
        Sheets("Bed Registry").Range(Cells(r, "B"), Cells(r, "P")).Copy Sheets("Discharges").Cells(2, "B")
        'Clear columns B-P on Bed Registry
        Sheets("Bed Registry").Range(Cells(r, "B"), Cells(r, "P")).ClearContents
    End If
Next r

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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