error 1004 method intersect of object _global failed

gorgkhan

New Member
Joined
Dec 4, 2023
Messages
8
Office Version
  1. 2010
Platform
  1. Windows
I want to delete the cells selected in the table. Error occurs when I Delete a mid row of the table.

VBA Code:
Sub DeleteTableMember()
Application.ScreenUpdating = False
On Error Resume Next
If ActiveSheet.ListObjects(1) Is Nothing Then Exit Sub
If ActiveSheet.ListObjects(1).DataBodyRange Is Nothing Then Exit Sub
Dim COLUMN_FIRSTCOLUMN As Integer
COLUMN_FIRSTCOLUMN = ActiveSheet.ListObjects(1).DataBodyRange(1, 1).Column
On Error GoTo 0

    Dim tmpCell As Range
    Dim delRng As Range
    For Each tmpCell In Selection
        If Not Intersect(tmpCell, ActiveSheet.ListObjects(1).DataBodyRange) Is Nothing Then
            If delRng Is Nothing Then
                Set delRng = Cells(tmpCell.Row, COLUMN_FIRSTCOLUMN)
            Else
                Set delRng = Union(Cells(tmpCell.Row, COLUMN_FIRSTCOLUMN), delRng)
            End If
        End If
    Next tmpCell
    If delRng Is Nothing Then Exit Sub
    Dim i As Integer
    For i = ActiveSheet.ListObjects(1).ListRows.Count To 1 Step -1
        If Not Intersect(ActiveSheet.ListObjects(1).DataBodyRange(i, 1), delRng) Is Nothing Then
            ActiveSheet.ListObjects(1).ListRows(i).Delete
        End If
    Next i

Application.ScreenUpdating = True
End Sub

any help/fix/improvement is appriciated!
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Which line of code generates the error?
 
Upvote 0
Which line of code generates the error?
as the title explained , this line
VBA Code:
If Not Intersect(ActiveSheet.ListObjects(1).DataBodyRange(i, 1), delRng) Is Nothing Then

after the deletion, the next iteration to intersect causes that error
 
Upvote 0
as the title explained , this line
Umm, not really. The error message pertains to the Intersect statement, and you have two such in your code.

I think what is happening is that once you delete the row, variable delRng goes to an undefined state and throws an error on the next pass. You many need to consider adding an Exit For statement or some other means to handle it.

VBA Code:
    For i = ActiveSheet.ListObjects(1).ListRows.Count To 1 Step -1
        If Not Intersect(ActiveSheet.ListObjects(1).DataBodyRange(i, 1), delRng) Is Nothing Then
            ActiveSheet.ListObjects(1).ListRows(i).Delete
            Exit For  '<-- ADD
        End If
    Next i
 
Upvote 0
Actually it didn't ;)

When you get the error and you hover your mouse over i and delRng in the line what values do you get?
I get i=3 and nothing is displayed for the delRng
 

Attachments

  • 00.PNG
    00.PNG
    2.8 KB · Views: 8
  • 01.PNG
    01.PNG
    13.2 KB · Views: 7
  • 02.PNG
    02.PNG
    7.7 KB · Views: 7
  • 03.PNG
    03.PNG
    3 KB · Views: 6
  • 04.PNG
    04.PNG
    4.4 KB · Views: 6
Upvote 0
Umm, not really. The error message pertains to the Intersect statement, and you have two such in your code.

I think what is happening is that once you delete the row, variable delRng goes to an undefined state and throws an error on the next pass. You many need to consider adding an Exit For statement or some other means to handle it.

VBA Code:
    For i = ActiveSheet.ListObjects(1).ListRows.Count To 1 Step -1
        If Not Intersect(ActiveSheet.ListObjects(1).DataBodyRange(i, 1), delRng) Is Nothing Then
            ActiveSheet.ListObjects(1).ListRows(i).Delete
            Exit For  '<-- ADD
        End If
    Next i
Yes, you are right! I got an error trying getting delRng.address after the deletion. But I don't want to terminate for as I may have more than 1 cell to be deleted. Why delRng got affected by that statement?
 
Upvote 0
Why delRng got affected by that statement?
Because you deleted the object (the table row) and the variable had nothing to reference to, hence the error.

ut I don't want to terminate for as I may have more than 1 cell to be deleted.

Something like this then:
VBA Code:
    Dim RCnt As Long
    For i = ActiveSheet.ListObjects(1).ListRows.Count To 1 Step -1
        If Not Intersect(ActiveSheet.ListObjects(1).DataBodyRange(i, 1), delRng) Is Nothing Then
            ActiveSheet.ListObjects(1).ListRows(i).Delete
            If RCnt = 1 Then
                Exit For
            Else
                RCnt = delRng.Rows.Count
            End If
        End If
    Next i
 
Upvote 0
Because you deleted the object (the table row) and the variable had nothing to reference to, hence the error.
My opinion was delRng referenceing to a normal range, and it should remain intact after deleteing the table row!
Something like this then:
I don't understand the use of RCnt and it is not initialized therefore the if condition to check its value is useless.



Anyway I declare a string variable and initialized by haveing the delRng.address as its value!
 
Upvote 0
My opinion was delRng referenceing to a normal range, and it should remain intact after deleteing the table row!
"Should" is a dangerous word in coding because it often describes the way we want something to work instead of the way it actually does work. :)

I don't understand the use of RCnt and it is not initialized therefore the if condition to check its value is useless.

In VBA, most variables have a default initialization value based on their type. For type Long, it is zero, which works well here as long asdelRng has more than one row. For the case where delRng is just one row, you're right that we will need an additional RCnt = delRng.Rows.Count before the loop:
VBA Code:
    If delRng Is Nothing Then Exit Sub
    Dim i As Integer
    Dim RCnt As Long
    RCnt = delRng.Rows.Count
    For i = ActiveSheet.ListObjects(1).ListRows.Count To 1 Step -1
        If Not Intersect(ActiveSheet.ListObjects(1).DataBodyRange(i, 1), delRng) Is Nothing Then
            ActiveSheet.ListObjects(1).ListRows(i).Delete
            If RCnt = 1 Then
                Exit For
            Else
                RCnt = delRng.Rows.Count
            End If
        End If
    Next i
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,151
Members
453,021
Latest member
Justyna P

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