Share via

pivot table compare current data with refresh data and then apply conditional formatting to the new values

Anonymous
2014-06-18T13:01:50+00:00

vba in excel 2013 pivot table that updates from an online CRM rows are a to h and it can be any number of rows. 

What I am trying to do is track progress. In column f values indicate probability for success 10 25 45 90, which can go up or down. The pivot table is refreshed to get the latest values from CRM. the update is handled by a connection to the crm not in the vba.

So far I been researching methods to conditionally format values that went up, down or remained the same since the last refresh with up down and across arrows. I have managed to piece together what I think should work but alas it is not. So I have come to you internet. 

My code first clears any formatting and goes down the column avoiding null or empty cells, comparing the values in column f to values in column j.

I have 3 conditions greater than, less than or equal to, and would like add an icon for each based on the result of the comparison. 

Finally when it finishes the column the code copies the current values in the pivot table column f to column j outside the pivot table which i hope to be able to hide once the cf works. The code is below, any help in understanding where the problem is would be a big help.

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("J51")) Is Nothing Then
        updateValues
    End If
End Sub

Sub df()
    Application.EnableEvents = True
End Sub

Sub updateValues()

    Application.EnableEvents = False
    Application.ScreenUpdating = False

 Cells.FormatConditions.Delete
    ActiveSheet.PivotTables("PivotTable1").PivotSelect "Probability[All]", _
        xlLabelOnly + xlFirstRow, True
    Selection.FormatConditions.AddIconSetCondition
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1)
        .ReverseOrder = False
        .ShowIconOnly = False
        .IconSet = ActiveWorkbook.IconSets(xl3Arrows)
    End With

        For i = 8 To 51
            Debug.Print i
            If IsNumeric(Cells(i, 6).Value) And Cells(i, 6).Value <> "" Then
                If Cells(i, 6).Value > Cells(i, 10).Value Then
                    Cells(i, 6).FormatConditions.AddIconSetCondition
                    With Cells(i, 6).FormatConditions(1).IconCriteria(1)
                             
                    End With
                ElseIf Cells(i, 6).Value < Cells(i, 10).Value Then
                    Cells(i, 6).FormatConditions.AddIconSetCondition
                    With Cells(i, 6).FormatConditions(1).IconCriteria(2)
                            
                    End With
                Else
                    Cells(i, 6).FormatConditions.AddIconSetCondition
                    With Cells(i, 6).FormatConditions(1).IconCriteria(3)
                             
                    End With
                End If
                Debug.Print "update row"
                Cells(i, 10).Value = Cells(i, 6).Value
            End If
        Next i

    Application.ScreenUpdating = True
    Application.EnableEvents = True
End Sub
Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

1 answer

Sort by: Most helpful
  1. Anonymous
    2014-06-18T13:03:34+00:00

    I also want to add that I quite new at this I read a vba book last night and I have been searching for most of the last two days to get this far. Any help on how to take this further would be much appreciated

    Was this answer helpful?

    0 comments No comments