Copy only cells that have data from one row to another, highlighting changes

UncleBajubjubs

Board Regular
Joined
Jul 11, 2017
Messages
111
Office Version
  1. 2010
Hello, I am trying to write a macro to copy information from a range (E22:DQ22) to another range directly above it (E8:DQ8), but only if the cells have values, and highlighting the changes. For example, if there is already information in cell E8 and F8, and cell E22 has data while F22 does not, the value in E8 will be replaced by that in E22, while the data in F8 will remain the same. Cell E8 will now be highlighted, while F8 will not.

I imagine I need some type of loop, but I'm not sure the bets way to do it. Any ideas?
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Does E22:DQ22 contain formulae, or hard values?
 
Upvote 0
In that case, how about
Code:
Sub uncleBajubjubs()
   Dim Rng As Range
   For Each Rng In Range("E22:DQ22").SpecialCells(xlConstants)
      With Rng.Offset(-14)
         .Value = Rng.Value
         .Interior.Color = 45678
      End With
   Next Rng
End Sub
 
Upvote 0
In that case, how about
Code:
Sub uncleBajubjubs()
   Dim Rng As Range
   For Each Rng In Range("E22:DQ22").SpecialCells(xlConstants)
      With Rng.Offset(-14)
         .Value = Rng.Value
         .Interior.Color = 45678
      End With
   Next Rng
End Sub

That worked perfectly, thank you!
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
So I've been using this macro and it's been working fine, however I wanted to add a small update to make it better. It's possible that the user may wish to put the data not just 14 rows above, but also 13,12,10,9,8,6,5 or 4 rows above, and I want to have a way to let the user choose which row they'd like. Do you think a message box would be the best way to do this?
 
Upvote 0
How about
Code:
Sub uncleBajubjubs()
   Dim Rng As Range
   Dim Offst As Variant
   Offst = InputBox("please enter the number of rows to offset")
   If Offst = "" Then Exit Sub
   For Each Rng In Range("E22:DQ22").SpecialCells(xlConstants)
      With Rng.Offset(-Offst)
         .Value = Rng.Value
         .Interior.Color = 45678
      End With
   Next Rng
End Sub
 
Upvote 0
How about
Code:
Sub uncleBajubjubs()
   Dim Rng As Range
   Dim Offst As Variant
   Offst = InputBox("please enter the number of rows to offset")
   If Offst = "" Then Exit Sub
   For Each Rng In Range("E22:DQ22").SpecialCells(xlConstants)
      With Rng.Offset(-Offst)
         .Value = Rng.Value
         .Interior.Color = 45678
      End With
   Next Rng
End Sub

Perfect again, thanks!
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,215
Members
448,554
Latest member
Gleisner2

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