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
 
It has worked, but immediately comes up with run time error 28 - out of stack space?

Any ideas?

thanks.
 
Upvote 0

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
that is probably because copying the cells across is causing another worksheet calculate event to trigger and so it gets in a loop, try turning off the event before the copies and turning it back on again afterwards see below:
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


[COLOR=#ff0000]Application.EnableEvents = False[/COLOR]
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)
[COLOR=#ff0000]Application.EnableEvents = True[/COLOR]


End If




End Sub
 
Upvote 0
that is probably because copying the cells across is causing another worksheet calculate event to trigger and so it gets in a loop, try turning off the event before the copies and turning it back on again afterwards see below:
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


[COLOR=#ff0000]Application.EnableEvents = False[/COLOR]
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)
[COLOR=#ff0000]Application.EnableEvents = True[/COLOR]


End If




End Sub

Works really well now thank you.
 
Upvote 0

Forum statistics

Threads
1,221,470
Messages
6,160,029
Members
451,611
Latest member
PattiButche

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