does anyone can explain these code

pplza

New Member
Joined
Jun 14, 2020
Messages
2
Office Version
  1. 2013
Platform
  1. Windows
this is code is work great! but I dont know what they are mean

for someday I apply it to other work.

VBA Code:
Dim r As Range
Dim c As Boolean

Private Sub worksheet_calculate()
'    Dim Target As Range
'    Set Target = Range("A1")
    If c = False Then
        c = True
        Exit Sub
    End If
    If r.Count > 1 Then Exit Sub
     Application.EnableEvents = False
        If Not Intersect(r, Me.[k1:k16]) Is Nothing Then
            If r.Value = 0 Then
            Me.Cells(r.Row, "c").Value = Me.Cells(r.Row, "c").Value + 1
            End If
        End If
    c = False
    Application.EnableEvents = True
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
    If c = False Then Exit Sub
    Set r = Target
    c = True
    r.Value = r.Value
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
this is code is work great!
bizar, does this code work ? Are c and r public or global variables ?
summarized :
- Worksheet_Change : replaces the content of your last changed cell/range into the value
- worksheet_calculate : is r not in the range K1:K16 and r is 0 then replace r with the value of the cell underneath +1
 
Upvote 0
bizar, does this code work ? Are c and r public or global variables ?
summarized :
- Worksheet_Change : replaces the content of your last changed cell/range into the value
- worksheet_calculate : is r not in the range K1:K16 and r is 0 then replace r with the value of the cell underneath +1
VBA Code:
 Application.EnableEvents = False
        If Not Intersect(r, Me.[k1:k16]) Is Nothing Then
            If r.Value = 0 Then
            Me.Cells(r.Row, "c").Value = Me.Cells(r.Row, "c").Value + 1
            End If
        End If
    c = False
    Application.EnableEvents = True


can you explain line of this code? thank you in advance
 
Upvote 0
VBA Code:
Application.EnableEvents = False                                'don't react on events (selection change, change, activate, ...) starting from this point (otherwise endless loop)
If Not Intersect(r, Me.[k1:k16]) Is Nothing Then                'if your r has something in common with the range K1:K16
     If r.Value = 0 Then                                        'if the value of r is 0
          Me.Cells(r.Row, "c").Value = Me.Cells(r.Row, "c").Value + 1     'the value of the cell in column C of the same row as r is incremented by 1
     End If
End If
c = False                                                       'some boolean c is reset
Application.EnableEvents = True                                 'starting from here, react again on events
 
Upvote 0
Solution

Forum statistics

Threads
1,214,834
Messages
6,121,871
Members
449,055
Latest member
excelhelp12345

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