Pivot table + VBA

Closed
shockers Posts 1 Registration date Monday March 21, 2011 Status Member Last seen March 21, 2011 - Mar 21, 2011 at 12:22 AM
RWomanizer Posts 365 Registration date Monday February 7, 2011 Status Contributor Last seen September 30, 2013 - Mar 21, 2011 at 01:09 AM
Hello,

I am very much new to VBA programming in Excel. This is the code.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range
If Target.Address <> "$C$24" Then Exit Sub
Set r = Range("J4")

If Target = 0 Then
With r
.Value = "100"
.Interior.ColorIndex = 4
End With
ElseIf Target > 0 Then
With r
.Value = "100"
.Interior.ColorIndex = 3
End With

End If
End Sub

C24 cell is a Pivot table data. The code works well if it is not Pivot table data.

Name: Pivottable3

Sum of Accidents
Date Total
3/1/2011 0
3/2/2011 0
3/4/2011 0
3/5/2011 0
3/6/2011 0
3/7/2011 0
3/8/2011 0
3/9/2011 2
3/10/2011 2
3/11/2011 2

I need help on how to refer the Total from the pivot table in order to set target address in the code above, to check if it is equal to 0 or > 0

Thanks in advance. I am waiting for your reply.

Prakhash


1 response

RWomanizer Posts 365 Registration date Monday February 7, 2011 Status Contributor Last seen September 30, 2013 120
Mar 21, 2011 at 01:09 AM
there is error in the line

If Target.Address <> "$C$24" Then Exit Sub

use
dim x as long
x = worksheets("worksheet name").range("c24")

If Target.address <> x then exit sub
and then
0