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
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Try something like this:
Code:
Private Sub CommandButton1_Click()

    Dim lr As Long
    Dim r 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
'           Insert row at row 2
            Sheets("Discharges").Activate
            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
.
Code:
Range("A2").EntireRow.Insert

The above inserts a row at Row 2 and shift all others down.
 
Upvote 0
Who are you responding to, me or Logit?
Did you try my response?
 
Upvote 0
Sorry. I was trying to respond to you. I think the part Insert row at row 2 needs to be defined to the "Discharges" worksheet as it is creating the row on the wrong sheet. I haven't been able to get it to work properly.
 
Upvote 0
Did you include the line right above the row insert step?
Code:
            [COLOR=#ff0000]Sheets("Discharges").Activate[/COLOR]
            Rows("2:2").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
If you include that line, I don't see how it can not insert the row on the "Discharges" page, as it is selecting/activating that sheet before inserting the row.
So the "Discharges" sheet should be the active sheet when it runs that line.
Try stepping through your code line-by-line while watching what it is doing on your workbook at the same time.
 
Upvote 0
I have reworked the macro (below) to insert a row at the top, instead of the bottom. However the information in row 2 is just replacing old information, rather than moving down. How could I get the information to move down each time information is transferred? Thanks Private Sub CommandButton1_Click()<o:p></o:p>
<o:p> </o:p>
Dim lr As Long<o:p></o:p>
Dim r As Long<o:p></o:p>
Dim nr As Long<o:p></o:p>
<o:p> </o:p>
Application.ScreenUpdating = False<o:p></o:p>
<o:p> </o:p>
'Find lr column P with data on Bed Registry<o:p></o:p>
lr = Sheets("Bed Registry").Cells(Rows.Count,"P").End(xlUp).Row<o:p></o:p>
<o:p> </o:p>
'Find lcol - LastColumn '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''<o:p></o:p>
'lcol = Sheets("Bed Registry").ActiveSheet.Cells(1,Application.Columns.Count).End(xlToRight).Column<o:p></o:p>
<o:p> </o:p>
'Loop through all rows on Bed Registry and check column Pfor closed<o:p></o:p>
For r = 2 To lr<o:p></o:p>
IfSheets("Bed Registry").Cells(r, "P") = "CLOSED"Then<o:p></o:p>
Sheets("Discharges").Range("A2").Select<o:p></o:p>
ActiveCell.EntireRow.Insert Shift:=xlDown<o:p></o:p>
'CopyOrigin:=xlFormatFromLeftOrAbove<o:p></o:p>
'Copycolumns B-P to "Discharges" on row 2<o:p></o:p>
'Sheets("Bed Registry").Range(Cells(r, "B"),Cells(r, "P")).Copy Sheets("Discharges").Range (Cells(2,"B"), (Cells r, "P"))<o:p></o:p>
'Clearcolumns B-P on Bed Registry<o:p></o:p>
Sheets("Bed Registry").Range(Cells(r, "B"), Cells(r,"P")).ClearContents<o:p></o:p>
End If<o:p></o:p>
Next r<o:p></o:p>
<o:p> </o:p>
End Sub<o:p></o:p>
 
Upvote 0
I think you missed changing the last "r" to "2" in your copy command.
See if this works:
Code:
Private Sub CommandButton1_Click()

Dim lr As Long
Dim r As Long
Dim nr 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

'Find lcol - LastColumn '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'lcol = Sheets("Bed Registry").ActiveSheet.Cells(1,Application.Columns.Count).End(xlToRight).Column


'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
        Range("A2").EntireRow.Insert Shift:=xlDown
'CopyOrigin:=xlFormatFromLeftOrAbove
'       Copycolumns B-P to "Discharges" on row 2
        Sheets("Bed Registry").Range(Cells(r, "B"), Cells(r, "P")).Copy Sheets("Discharges").Range(Cells(2, "B"), Cells([COLOR=#ff0000][B]2[/B][/COLOR], "P"))
'       Clearcolumns B-P on Bed Registry
        Sheets("Bed Registry").Range(Cells(r, "B"), Cells(r, "P")).ClearContents
    End If
Next r


End Sub
 
Upvote 0
Thanks, I tried that but it did not work. I actually pasted the wrong macro though. This is the one I am currently working with that is not moving the row down. Private Sub CommandButton1_Click()<o:p></o:p>
<o:p> </o:p>
Dim lr As Long<o:p></o:p>
Dim r As Long<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
Application.ScreenUpdating = False<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
'Find lr column P with data on Bed Registry<o:p></o:p>
lr = Sheets("Bed Registry").Cells(Rows.Count,"P").End(xlUp).Row<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
'Loop through all rows on Bed Registry and check column Pfor closed<o:p></o:p>
For r = 2 To lr<o:p></o:p>
If Sheets("Bed Registry").Cells(r, "P")= "CLOSED" Then<o:p></o:p>
'Sheets("Discharges").Activate<o:p></o:p>
'Insert r at r2<o:p></o:p>
'Rows("2:2").Insert Shift:=xlDown,<o:p></o:p>
'CopyOrigin:=xlFormatFromLeftOrAbove<o:p></o:p>
'Copy columns B-P to Discharges on row 2<o:p></o:p>
Sheets("Bed Registry").Range(Cells(r,"B"), Cells(r, "P")).CopySheets("Discharges").Cells(2, "B")<o:p></o:p>
'Clear columns B-P on Bed Registry<o:p></o:p>
Sheets("Bed Registry").Range(Cells(r,"B"), Cells(r, "P")).ClearContents<o:p></o:p>
End If<o:p></o:p>
Next r<o:p></o:p>
<o:p> </o:p>
End Sub<o:p></o:p>
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,694
Members
448,979
Latest member
DET4492

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