VBA - When a cell value changes, insert row and copy down the data

jonny1984

New Member
Joined
Apr 27, 2012
Messages
20
Office Version
  1. 2016
Platform
  1. Windows
Hi

I'm sure this is relatively easy - I am just trying to create a VBA macro that anytime a cell's *value* changes (from an external data source) it will insert a new row and copy down the data

For example here is some data I have in a sheet where cells
B6 = Current Timestamp
C6 = Current Data

Whenever there is a change to Cell C6 (ie the data changes) ->> it should insert a new row and paste in the data so it would look like the example below

Current
08:3573
Previous
08:3470
08:3367
08:3066
08:2965
08:2062
08:1959
08:0258
07:4554

<colgroup><col><col></colgroup><tbody>
</tbody>
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Hi Jonny,

Something like this should do the trick, needs to go in the sheet module;

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$6" Then
    Rows("7").Insert
    Range("B7").Value = Range("B6").Value
    Range("C7").Value = Range("C6").Value
    Range("C6").Activate
End If
End Sub

Hope this helps,
Cheers,
Alan.
 
Upvote 0
Hi Jonny,

Something like this should do the trick, needs to go in the sheet module;

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$6" Then
    Rows("7").Insert
    Range("B7").Value = Range("B6").Value
    Range("C7").Value = Range("C6").Value
    Range("C6").Activate
End If
End Sub

Hope this helps,
Cheers,
Alan.


Hi Alan

Thanks for the above but will the formula above work if the cell C6 is being generated by a formula?

I thought worksheet_change only worked if a user changed the cell , versus if its constantly being re calculated by a formula - which requires the 'worksheet_calculate' function ?

But I couldnt get a code with that function to work!
 
Upvote 0
Hi Jonny,

Sorry I didn't realise it was a formula...

You could try using a helper cell to track if cell C6 has changed since the last calculation, example below uses A1 as the helper cell. Initially you'd need to manually change the helper cell to match C6

Code:
Private Sub Worksheet_Calculate()

If Range("C6").Value <> Range("A1").Value Then
    Rows("7").Insert
    Range("B7").Value = Range("B6").Value
    Range("C7").Value = Range("C6").Value
    Range("C6").Activate
End If
Range("A1").Value = Range("C6").Value

End Sub

Cheers,
Alan
 
Upvote 0

Forum statistics

Threads
1,215,161
Messages
6,123,380
Members
449,097
Latest member
Jabe

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