VBA Change event code

jamescooper

Well-known Member
Joined
Sep 8, 2014
Messages
840
Hello, the following code populates the codes of cell K9 if it is changed in the next available row in column AL and K10 in the next available in AV and F4 in the next available in AK.

However, this only happens if the cell is changed manually by entering in. My excel spreadsheet is linked to software where the cell can change without this happening; any idea how I would adapt my code to achieve any changes?

Thanks.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)


Dim nR As Long


nR = WorksheetFunction.Max(2, Sheets("Bet Angel").Range("AL" & Rows.Count).End(xlUp).Row + 1)


If Not Intersect(Target, Sheets("Bet Angel").Range("K9:K10")) Is Nothing Then
If Target.Cells.CountLarge > 1 Or IsEmpty(Target) Then Exit Sub
Sheets("Bet Angel").Range("K9").Copy Destination:=Sheets("Bet Angel").Range("AL" & nR)
Sheets("Bet Angel").Range("K10").Copy Destination:=Sheets("Bet Angel").Range("AV" & nR)
Sheets("Bet Angel").Range("F4").Copy Destination:=Sheets("Bet Angel").Range("AK" & nR)


End If


End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
try using the worksheet_Calculate event, depending on how the data is coming in you might find that this event works.
 
Upvote 0
So would it be?

Code:
Private Sub Worksheet_Calculate


Dim nR As Long


nR = WorksheetFunction.Max(2, Sheets("Bet Angel").Range("AL" & Rows.Count).End(xlUp).Row + 1)


If Not Intersect(Target, Sheets("Bet Angel").Range("K9:K10")) Is Nothing Then
If Target.Cells.CountLarge > 1 Or IsEmpty(Target) Then Exit Sub
Sheets("Bet Angel").Range("K9").Copy Destination:=Sheets("Bet Angel").Range("AL" & nR)
Sheets("Bet Angel").Range("K10").Copy Destination:=Sheets("Bet Angel").Range("AV" & nR)
Sheets("Bet Angel").Range("F4").Copy Destination:=Sheets("Bet Angel").Range("AK" & nR)


End If


End Sub</pre>
 
Upvote 0
not quite, you will have noticed that the worksheet calculate event doesn't have a "Target" range. so you need to save the cells you are interested in and check to see if they have changed. So in a standard module declare two variables:
Code:
Public oldk9
Public oldk10
Sub test()


End Sub
Then in the worksheet calcuulate event put this code:
Code:
Private Sub Worksheet_Calculate()
nr = Cells(Rows.Count, "AL").End(xlUp).Row + 1
inarr = Range("K9:K10")
If inarr(1, 1) <> oldk9 Or inarr(2, 1) <> oldk10 Then


Range("AL" & nr) = inarr(1, 1)
Range("AV" & nr) = inarr(2, 1)
oldk9 = inarr(1, 1)
oldk10 = inarr(2, 1)
'Sheets("Bet Angel").Range("F4").Copy Destination:=Sheets("Bet Angel").Range("AK" & nr)
End If


End Sub
If you find the calculate event doesn't get triggered, try putting an equation in a unused cell which just multiplies the value in K9 by a constant e.g
put :
=2*k9
into cell kk9
 
Last edited:
Upvote 0
not quite, you will have noticed that the worksheet calculate event doesn't have a "Target" range. so you need to save the cells you are interested in and check to see if they have changed. So in a standard module declare two variables:
Code:
Public oldk9
Public oldk10
Sub test()


End Sub
Then in the worksheet calcuulate event put this code:
Code:
Private Sub Worksheet_Calculate()
nr = Cells(Rows.Count, "AL").End(xlUp).Row + 1
inarr = Range("K9:K10")
If inarr(1, 1) <> oldk9 Or inarr(2, 1) <> oldk10 Then


Range("AL" & nr) = inarr(1, 1)
Range("AV" & nr) = inarr(2, 1)
oldk9 = inarr(1, 1)
oldk10 = inarr(2, 1)
'Sheets("Bet Angel").Range("F4").Copy Destination:=Sheets("Bet Angel").Range("AK" & nr)
End If


End Sub
If you find the calculate event doesn't get triggered, try putting an equation in a unused cell which just multiplies the value in K9 by a constant e.g
put :
=2*k9
into cell kk9


Thanks, so the first bit goes in say "Module 1"

Then the second section within the sheet you have your data in - e.g. Bet Angel, as above?
 
Upvote 0
put a break point in the code on the if statement and see if it is getting triggered
 
Upvote 0
Doesn't appear to be.

Do you have a simpler example where say there is a changing value in cell A1 and then it populates in B1, B2, B3, B4 etc..

So I can then adapt my code to suit?

Many thanks.
 
Upvote 0
Have you tried my suggesion of adding an equation which is looking at K9 to force a recalculation?
If you find the calculate event doesn't get triggered, try putting an equation in a unused cell which just multiplies the value in K9 by a constant e.gput :
=2*k9
into cell kk9

If you want to change the code from writing in AL to writing in column B just change all the "AL" to "B"
I doens't make it any simpler!! It is a simple as it can be!!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,331
Members
452,636
Latest member
laura12345

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