Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel VBA, code in worksheet not working

I have the following code in sheet1 (note - this code is in the wroksheet object, not the workbook object or a module):

Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Integer
r = ActiveCell.Row
Cells(r - 1, 7).Value = Now()
ActiveWorkbook.save
End Sub

Can someone tell me why: 1. the ActiveWorkbook.save doesnt work above - it gets stuck in an infinite loop instead; 2. why I cant step throught the code by just pressing F8

I tried to put the ActiveWorkbook.save in a separate module and then call that function from the code in the worksheet but that got stuck in an infinite loop as well.

like image 476
user1894469 Avatar asked Jul 09 '26 03:07

user1894469


1 Answers

You need to disable events to avoid the infinite loop

Private Sub Worksheet_Change(ByVal Target As Range)
application.enableevents=false
    Cells(target.row - 1, 7).Value = Now()
application.enableevents=true

ActiveWorkbook.save
End Sub
like image 52
nutsch Avatar answered Jul 11 '26 18:07

nutsch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!