Copy a selection in a row instead of using cell.entirerowcopy

fuslela

New Member
Joined
Aug 12, 2020
Messages
23
Office Version
  1. 365
Platform
  1. Windows
Hello All,

I am using the following code which works well, the code monitors a column AA for the value "1" which is placed in the column as the result of a formula calculation

VBA Code:
Private Sub Worksheet_Calculate()

For Each Cell In Sheets("Source").Range("AA2:AA50")
If Cell.Value = "1" Then
Cell.EntireRow.Copy
a = Sheets("Destination").Cells(Rows.Count, "A").End(xlUp).Row + 1
Sheets("Destination").Range("A" & a).PasteSpecial xlPasteValues
End If
Next

End Sub

I would like to improve the code to do the following:
1) Instead of copy the entire row, only copy the row upto column AA so if aa2 is 1, the values from a2 to z2 are copied to another sheet
and
2) add another condition where either a cell/column is monitored in AB to see if that value is also 1, so AA and AB must have a value containing 1 for the macro to fire.

Thanks!
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
How about
VBA Code:
Private Sub Worksheet_Calculate()

For Each cell In Sheets("Source").Range("AA2:AA50")
If cell.Value = "1" And cell.Offset(, 1).Value = "1" Then
Range("A" & cell.Row).Resize(, 26).Copy
a = Sheets("Destination").Cells(Rows.Count, "A").End(xlUp).Row + 1
Sheets("Destination").Range("A" & a).PasteSpecial xlPasteValues
End If
Next

End Sub
 
Upvote 0
Solution
How about
VBA Code:
Private Sub Worksheet_Calculate()

For Each cell In Sheets("Source").Range("AA2:AA50")
If cell.Value = "1" And cell.Offset(, 1).Value = "1" Then
Range("A" & cell.Row).Resize(, 26).Copy
a = Sheets("Destination").Cells(Rows.Count, "A").End(xlUp).Row + 1
Sheets("Destination").Range("A" & a).PasteSpecial xlPasteValues
End If
Next

End Sub
That's nice and tidy and something I hadn't thought about, nice use of offset! I will give it a try.
Many Thanks!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,639
Messages
6,120,679
Members
448,977
Latest member
dbonilla0331

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