Error on method intersect

Beneindias

Board Regular
Joined
Jun 21, 2022
Messages
120
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi to all,

Here I am again to bring my problems with vba.

I have a workbook where I have a few sheets, that all have one table and one pivot table.

To update the data on the pivot table, I have this code, that runs when I change the column "Valor s/ Iva" on the table:

VBA Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    Dim tbl As ListObject
    Dim tblName As String
    Dim tblColName As String
    
    For Each tbl In ActiveSheet.ListObjects
        'Application.ScreenUpdating = False
   
        'Table name
        tblName = tbl.Name
        'Column name
        tblColName = tblName & "[Valor s/ Iva]"
            
        If Not Intersect(Target, Range(tblColName)) Is Nothing Then
            'Refreshes workbook pivot tables

            MsgBox "Intersected"
            ThisWorkbook.RefreshAll
            MsgBox "Updated"

        End If
        
        'Application.ScreenUpdating = True
        
    Next tbl
    
End Sub

When I run this macro, I get this error:

run-time error '1004': Method 'Intersect' of 'object '_Global' failed

On this line:
If Not Intersect(Target, Range(tblColName)) Is Nothing Then

I have added those two MsgBox, and the first on ("Intersected") shows, but not the second one.
Although I get the error, the data still updates on the pivot tables


Thank you all
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Hi to all,

Here I am again to bring my problems with vba.

I have a workbook where I have a few sheets, that all have one table and one pivot table.

To update the data on the pivot table, I have this code, that runs when I change the column "Valor s/ Iva" on the table:

VBA Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    Dim tbl As ListObject
    Dim tblName As String
    Dim tblColName As String
   
    For Each tbl In ActiveSheet.ListObjects
        'Application.ScreenUpdating = False
  
        'Table name
        tblName = tbl.Name
        'Column name
        tblColName = tblName & "[Valor s/ Iva]"
           
        If Not Intersect(Target, Range(tblColName)) Is Nothing Then
            'Refreshes workbook pivot tables

            MsgBox "Intersected"
            ThisWorkbook.RefreshAll
            MsgBox "Updated"

        End If
       
        'Application.ScreenUpdating = True
       
    Next tbl
   
End Sub

When I run this macro, I get this error:

run-time error '1004': Method 'Intersect' of 'object '_Global' failed

On this line:
If Not Intersect(Target, Range(tblColName)) Is Nothing Then

I have added those two MsgBox, and the first on ("Intersected") shows, but not the second one.
Although I get the error, the data still updates on the pivot tables


Thank you all
As you only have one table and one pivot table the loop is not needed.

VBA Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

  If Target.CountLarge > 1 Then
    Exit Sub
  End If
              
  If Not Intersect(Target, ActiveSheet.ListObjects(1).ListColumns("Valor s/ Iva").DataBodyRange) Is Nothing Then
        
    ActiveSheet.PivotTables(1).PivotCache.Refresh

  End If
  
End Sub
 
Upvote 0
Solution
As you only have one table and one pivot table the loop is not needed.

VBA Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

  If Target.CountLarge > 1 Then
    Exit Sub
  End If
             
  If Not Intersect(Target, ActiveSheet.ListObjects(1).ListColumns("Valor s/ Iva").DataBodyRange) Is Nothing Then
       
    ActiveSheet.PivotTables(1).PivotCache.Refresh

  End If
 
End Sub
After posting this question, I was exactly thinking about that...I didn't need a loop...

Tried you solution and it worked beautifully

Thank you
 
Upvote 0

Forum statistics

Threads
1,223,877
Messages
6,175,139
Members
452,615
Latest member
bogeys2birdies

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