small adjustment for this macro

M3L333

New Member
Joined
Jul 31, 2018
Messages
15
Can someone please help me to make a small adjustment to this macro?

When creating a new row at the top on Sheet "Discharges", and moving the current information down, the Discharges sheet is currently only moving down rows B-P. Once the data is transferred to the discharges sheet there is some extra data inputted into several columns to the rights. I want all data in the row to be moved down together so that the data does not get mixed up.

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 P for 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
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Hello,

You could test the following

Code:
Private Sub CommandButton1_Click()
Dim lrow As Long
Dim lcol As Long
Dim r As Long


Application.ScreenUpdating = False


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


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


'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
      '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, lcol)).Copy Sheets("Discharges").Cells(2, "B")
      'Clear columns B-P on Bed Registry
      Sheets("Bed Registry").Range(Cells(r, "B"), Cells(r, lcol)).ClearContents
    End If
  Next r
End Sub

Have to assume the Number of Columns will keep on changing ...

Otherwise ... you could have replaced the Letter P by the Letter of your last Column ... :wink:
 
Upvote 0
I tried this and it is not working, trying to figure out the issue, but not sure why it won't work.
 
Upvote 0

Forum statistics

Threads
1,214,813
Messages
6,121,706
Members
449,048
Latest member
81jamesacct

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