P
Phil Hageman
This code was working okay until I added some rows in the worksheet. No new
columns. On the seventh line, I have changed the Range numbers to the new
row numbers. What I need is the meaning of lines 15 through 22. I think I
need to adjust a value here somewhere so the formula in line 28 works on the
correct cells (cells with new addresses). Could someone please explain line
15 through 22? (I added the line numbers in the left margin for reference in
this thread.)
1 Private Sub Worksheet_Change(ByVal Target As Range)
2 Dim nRow As Long, nRow1 As Long
3 Dim dV As Double, dP As Double
4 Dim rDV As Range, rDP As Range
5 Dim rng As Range, rng1 As Range, rng2 As Range
6 On Error GoTo ErrHandler
7 Set rng = Intersect(Range("U:U,AD:AD,AM:AM,AV:AV"),
Range("20:31,53:64,86:97"))
8 With Target
9 If .Count > 3 Then Exit Sub
10 If Intersect(.Cells, rng) Is Nothing Then Exit Sub
11 Set rng1 = Intersect(.Cells, rng)
12 If rng1.Count > 1 Then Exit Sub
13 End With
14 Debug.Print rng1.Address, rng1.Parent.Cells(rng1.Column + 2).Address,
15 With rng1
16 Set rng2 = rng1.Parent.Cells(rng1.row, rng1.Column + 3)
17 nRow1 = Int((.row - 19) / 33)
18 nRow = Int(.Column - 21) / 9
19 Set rDV = Range("V11").Offset(nRow + 33 * nRow1, 0)
20 Set rDP = Range("P11").Offset(nRow + 33 * nRow1, 0)
21 dV = Range("V11").Offset(nRow + 33 * nRow1, 0).Value
22 dP = Range("P11").Offset(nRow + 33 * nRow1, 0).Value
23 'sStr = rng1.Address & " - " & rng2.Address & "-" & rDP.Address
& "-" & rDV.Address
24 'MsgBox sStr
25 Application.EnableEvents = False
26 If Not IsEmpty(rDV) And Not IsEmpty(rDP) Then
27 If dP - dV <> 0 Then
28 rng2.Value = (.Value - dV) / (dP - dV)
29 End If
30 Else
31 MsgBox "Values Required for Target and Op Zero: Cells " &
rDP.Address(0, 0) _
32 & " and " & rDV.Address(0, 0)
33 rng1.MergeArea.ClearContents
34 End If
35 Application.EnableEvents = True
36 End With
37 ErrHandler:
38 Application.EnableEvents = True
39 End Sub
columns. On the seventh line, I have changed the Range numbers to the new
row numbers. What I need is the meaning of lines 15 through 22. I think I
need to adjust a value here somewhere so the formula in line 28 works on the
correct cells (cells with new addresses). Could someone please explain line
15 through 22? (I added the line numbers in the left margin for reference in
this thread.)
1 Private Sub Worksheet_Change(ByVal Target As Range)
2 Dim nRow As Long, nRow1 As Long
3 Dim dV As Double, dP As Double
4 Dim rDV As Range, rDP As Range
5 Dim rng As Range, rng1 As Range, rng2 As Range
6 On Error GoTo ErrHandler
7 Set rng = Intersect(Range("U:U,AD:AD,AM:AM,AV:AV"),
Range("20:31,53:64,86:97"))
8 With Target
9 If .Count > 3 Then Exit Sub
10 If Intersect(.Cells, rng) Is Nothing Then Exit Sub
11 Set rng1 = Intersect(.Cells, rng)
12 If rng1.Count > 1 Then Exit Sub
13 End With
14 Debug.Print rng1.Address, rng1.Parent.Cells(rng1.Column + 2).Address,
15 With rng1
16 Set rng2 = rng1.Parent.Cells(rng1.row, rng1.Column + 3)
17 nRow1 = Int((.row - 19) / 33)
18 nRow = Int(.Column - 21) / 9
19 Set rDV = Range("V11").Offset(nRow + 33 * nRow1, 0)
20 Set rDP = Range("P11").Offset(nRow + 33 * nRow1, 0)
21 dV = Range("V11").Offset(nRow + 33 * nRow1, 0).Value
22 dP = Range("P11").Offset(nRow + 33 * nRow1, 0).Value
23 'sStr = rng1.Address & " - " & rng2.Address & "-" & rDP.Address
& "-" & rDV.Address
24 'MsgBox sStr
25 Application.EnableEvents = False
26 If Not IsEmpty(rDV) And Not IsEmpty(rDP) Then
27 If dP - dV <> 0 Then
28 rng2.Value = (.Value - dV) / (dP - dV)
29 End If
30 Else
31 MsgBox "Values Required for Target and Op Zero: Cells " &
rDP.Address(0, 0) _
32 & " and " & rDV.Address(0, 0)
33 rng1.MergeArea.ClearContents
34 End If
35 Application.EnableEvents = True
36 End With
37 ErrHandler:
38 Application.EnableEvents = True
39 End Sub