The value in the field should not disappear after refreshing a file

rajagopalanpb

New Member
Joined
Nov 21, 2008
Messages
34
Hi
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
I have got this in a pivot table
<o:p></o:p>
Customer Total sales(USD)
<o:p></o:p>
30010 500.00
30041 2000.00
<o:p></o:p>
<o:p></o:p>
In another column I enter a comment for customer 30010(and leave the comment column for customer 30041 blank) as below
Data 1.xls
Customer Total sales(USD) Comment
<o:p></o:p>
30010 500.00 Test 1
30041 2000.00
Later on the content of the file changes as below:
<o:p></o:p>
Customer Total sales(USD)
<o:p></o:p>
30025 1500.00
30010 500.00
30041 2000.00
When the file is refreshed I would like to have comment as per the former file i.e the comment “Test 1” should appear against customer 30010 and similarly for any other customer that I enter the comment.It should be as below:
Customer Total sales(USD) Comment
<o:p></o:p>
30025 1500.00
30010 500.00 Test 1
Customer Total sales(USD) Comment
<o:p></o:p>
30041 2000.00
Is this possible using Excel VBA?If yes I would like to have the code please.
<o:p></o:p>
Thanks for your help
<o:p></o:p>
Raja
<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
 
If you only want to keep track of the comments when the workbook is open and the pivot table is refreshed, it should be possible to put some code in the Worksheet_Change event procedure. What is the name of your pivot table, which columns does it occupy and where are you entering the comments?
 
Upvote 0
Hello

Thanks a lot for the quick response.

The name of the pivot table is "Pivot All GBP".The cooemnts are to be entered in column V and it occupies columns A-V(including where the comments are to be updated)

Thanks

Raja
 
Upvote 0
See if this works for you:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim ShComments As Worksheet
    Dim ColCustomer As Integer
    Dim ColComments As Integer
    Dim r As Long
    If Target.Count > 1 Then Exit Sub
    With PivotTables("Pivot All GBP")
        ColCustomer = .PivotFields("Customer").LabelRange.Column
        With .TableRange1
            ColComments = .Columns.Count - .Column + 2
        End With
    End With
    If Application.Intersect(Target, Columns(ColComments)) Is Nothing Then Exit Sub
    On Error Resume Next
    Set ShComments = Worksheets("Comments")
    If Err <> 0 Then
        Err.Clear
        Set ShComments = Worksheets.Add(After:=Me)
        Me.Activate
        With ShComments
            ShComments.Name = "Comments"
            .Cells(1, 1).Value = "Customer"
            .Cells(1, 2).Value = "Comment"
        End With
    End If
    On Error GoTo 0
    With ShComments
        If WorksheetFunction.CountIf(.Columns(1), Cells(Target.Row, ColCustomer).Value) > 0 Then
            r = WorksheetFunction.Match(Cells(Target.Row, ColCustomer).Value, .Columns(1), False)
        Else
            r = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
            .Cells(r, 1).Value = Cells(Target.Row, ColCustomer).Value
        End If
            .Cells(r, 2).Value = Target.Value
    End With
End Sub
Private Sub Worksheet_Calculate()
    Dim ShComments As Worksheet
    Dim RngCustomer As Range
    Dim ColComments As Integer
    Dim Cell As Range
    Dim r As Long
    On Error Resume Next
    Set ShComments = Worksheets("Comments")
    If Err <> 0 Then
        Err.Clear
        Exit Sub
    End If
    On Error GoTo 0
    Application.EnableEvents = False
    With PivotTables("Pivot All GBP")
        Set RngCustomer = .PivotFields("Customer").DataRange
        With .TableRange1
            ColComments = .Columns.Count - .Column + 2
        End With
    End With
    With ShComments
        For Each Cell In RngCustomer
            If WorksheetFunction.CountIf(.Columns(1), Cell.Value) > 0 Then
                r = WorksheetFunction.Match(Cell.Value, .Columns(1), False)
                Cells(Cell.Row, ColComments).Value = .Cells(r, 2).Value
            Else
                Cells(Cell.Row, ColComments).Value = ""
            End If
        Next Cell
    End With
    Application.EnableEvents = True
End Sub

When comments are added the Worksheet_Change event procedure stores them in a new worksheet. When the pivot table is refreshed the Worksheet_Calculate event procedure reads the values form that sheet and updates them.
 
Upvote 0
Hi Andrew

Thanks a lot for the code.I keep getting this message when I try to save the code in the Excel File and then as soon as I enter the comments in the comments column,this error message props up.Am I missing something?

Error 1004 Method 'Range' of object '_Worksheet' failed

(Also do you have an email id where I can send you the screen shots of the error because I am unable to attach then here!!!)


Thanks for your help

Raja
 
Upvote 0
That's not using the Range object, but are you sure that you have a pivot table named "Pivot ALL GBP" on the worksheet and that it contains a pivot field named "Customer"?
 
Upvote 0
Hi

the name of the worksheet which contains the Pivot table is Pivot ALL GBP

Sorry,I just realised the name of the customer column is account (which is Column B)

Sorry for the inconvenience .I would appreciate if you could let me know as to how to go about it.

Thanks for your help

Raja
 
Upvote 0

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