Code looping twice

Dipak543

New Member
Joined
Jul 4, 2020
Messages
26
Office Version
  1. 365
Platform
  1. Windows
Hi friends,

I am stuck in a VBA problem. actually, the code is printing the value twice in subsequent rows its working fine on my previous excel version 2007 but now I shifted to office 365 and its printing the values twice whenever the cell changes.
Attached photo for clarification see timing it printing the value twice in the same second.

Thanks



VBA Code:
Private Sub Worksheet_Calculate()
If Time >= TimeSerial(9, 25, 2) And Time <= TimeSerial(20, 29, 59) Then

    capturerow = 2

    currow = Cells(Rows.Count, 1).End(xlUp).Row
   
    Cells(currow + 1, 1) = Cells(capturerow, 1)
    Cells(currow + 1, 2) = Cells(capturerow, 2)
              
Else
Exit Sub
End If
   
End Sub
VBA Code:
Private Sub worksheet_change(ByVal target As Range)
   
  
Dim Cell As Range
For Each Cell In target
    If Cell.Column = Range("A:A").Column Then
        If Cell.Value <> "" Then
            Cells(Cell.Row, "c").Value = Format(Now(), "HH:MM:SS")
           

          
        End If
    End If
Next Cell

End Sub
 

Attachments

  • Screenshot (244).png
    Screenshot (244).png
    156.3 KB · Views: 21

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
I'm guessing you ought to do a Application.EnableEvents=False before making any changes inside the event handler Worksheet_Calculate(), then reset it to True before exiting. Why it should work differently between 2007 and 365 though, I can't say.
 
Upvote 0
I'm guessing you ought to do a Application.EnableEvents=False before making any changes inside the event handler Worksheet_Calculate(), then reset it to True before exiting. Why it should work differently between 2007 and 365 though, I can't say.
Thank you for the reply-

Like this-?

Private Sub Worksheet_Calculate()
If Time >= TimeSerial(9, 25, 2) And Time <= TimeSerial(20, 29, 59) Then

Application.EnableEvents = False

capturerow = 2

currow = Range("A65536").End(xlUp).Row

Cells(currow + 1, 1) = Cells(capturerow, 1)
Cells(currow + 1, 2) = Cells(capturerow, 2)

Application.EnableEvents = True

Else
Exit Sub
End If

End Sub
Private Sub worksheet_change(ByVal target As Range)


Dim Cell As Range
For Each Cell In target
If Cell.Column = Range("A:A").Column Then
If Cell.Value <> "" Then
Cells(Cell.Row, "c").Value = Format(Now(), "HH:MM:SS")



End If
End If
Next Cell

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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