Range of non-adjucent columns and error in deleting last line therein

Tango_Bravo

New Member
Joined
Jun 14, 2017
Messages
33
Dear All,


I have a lengthy worksheet, which is a register of issued invoices. Each row contains a separate invoice and each column provides different details for each separate invoice. If I want to cancel an invoice (because data is incorrect) I use a sub which deletes last issued invoice - i.e. the last line in the table.


Columns A keeps tracking number of type A invoices, column B keep tracking number of type B invoices. I want to introduce a third type of invoice in the table and column D is supposed to keep track of the consecutive number for this type of invioice.

After introducing the third type of invoices, when testing cancellation (i.e. deletion of last line) I the following error:

Run-time error 1004: Delete method or Range classified failed

here is the code:

Code:
    Set v1 = .Range("A:B").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
    Set v2 = .Range("D:D").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
    Set r = Union(v1, v2)

    str1 = r.Value


If MsgBox("This will undo last validation, are you sure?", vbOKCancel) = vbCancel Then Exit Sub
flag = False

        If Not r Is Nothing Then
        r.EntireRow.Delete
        flag = True
        End If


And the line that brings the issue is the following:

Code:
        r.EntireRow.Delete


Help will be much appreciated.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
You can't use the .EntireRow.Delete method on a disjoint range, which you create using the Union function. You might try something like:

Code:
        If v1.Row > v2.Row Then
            v1.EntireRow.Delete
        Else
            v2.EntireRow.Delete
        End If
which will delete the furthest row down, or if they are on the same row, that row.
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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