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>
 
Hi Andrew

I am getting these two error messages when I refresh the file:

Run-time error '1004':
Unable to get the match property of the WorksheetFunction class

The other one is

Run-time error '1004':
Application-defined or object-defined error

This error props up in Sub Worksheet_Calculate()

Thanks

Raja
 
Upvote 0
Hi Andrew

Hope you are well.

Sorry for the delayed response as I thought I had solved it but looks like I haven't.

The error is in this code"r = WorksheetFunction.Match(Cell.Value, .Columns(1), True)"

Also it appears from the code that the new comments is store in a new worksheet "cooments".Am I right?If yes,then currently this VBA code is not saving the new comments in the worksheet comments.

Let me know how to go about this.

Thanks for your help

Raja
 
Upvote 0
In the code I posted there isn't a line that says:

r = WorksheetFunction.Match(Cell.Value, .Columns(1), True)

My code had:

r = WorksheetFunction.Match(Cell.Value, .Columns(1), False)

which can't fail because it depends on a Countif for Cell.Value in that range.

I think you had better post the code that is failing.
 
Upvote 0
Hi Andrew

Thanks for pointing it out.Maybe I had copied the wrong code.Its here that the code is failing

r = WorksheetFunction.Match(Cells(Target.Row, ColCustomer).Value, .Columns(1), False)

Let me know your views

Thanks a lot for the help

Raja
 
Upvote 0
Hi Andrew

Sorry for the inconvenience

Here it is

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("PivotTable2")
        ColCustomer = .PivotFields("Account").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 = "Account"
            .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("Account").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
 
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