Loop runs into subscript out of range in the last row?

Lil2606

Board Regular
Joined
Jan 29, 2019
Messages
79
Hi all,

So I have this code, works like a charm except it runs into a "run time error 9 subscript out of range" instead of just finishing.. It does the job and then in the last row it pops up the error. Any ideas why?

Code:
Sub testing2()


Dim Dict As Scripting.Dictionary
Dim DictKey As String
Dim p As Long


Set Dict = New Scripting.Dictionary


Dict.CompareMode = TextCompare


For p = 1 To Sheet4.ListObjects("EastTable").ListRows.Count


DictKey = Join(Array(Sheet4.ListObjects("EastTable").ListRows(p).Range(1, Sheet4.ListObjects("EastTable").ListColumns("First Name").Range.Column), _
                     Sheet4.ListObjects("EastTable").ListRows(p).Range(1, Sheet4.ListObjects("EastTable").ListColumns("Last Name").Range.Column)), "|")


If Not Dict.Exists(DictKey) Then
Dict.Add DictKey, Nothing


Else
Sheet4.ListObjects("EastTable").ListRows(p).Delete


End If


Next


End Sub

Could anyone help please?

Question posted on ExcelForum as well link: https://www.excelforum.com/excel-pr...out-of-range-in-the-last-row.html#post5070127
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
The problem is that when you delete a row the counter p no longer exists for the last row.
This could be a solution:

Code:
Sub testing2()
    Dim Dict As Scripting.Dictionary
    Dim DictKey As String
    Dim p As Long, u As Long
    
    Set Dict = New Scripting.Dictionary
    Dict.CompareMode = TextCompare
    For p = 1 To Sheet4.ListObjects("EastTable").ListRows.Count
        If p > Sheet4.ListObjects("EastTable").ListRows.Count Then Exit For
        DictKey = Join(Array(Sheet4.ListObjects("EastTable").ListRows(p).Range(1, Sheet4.ListObjects("EastTable").ListColumns("First Name").Range.Column), _
                    Sheet4.ListObjects("EastTable").ListRows(p).Range(1, Sheet4.ListObjects("EastTable").ListColumns("Last Name").Range.Column)), "|")
        
        If Not Dict.Exists(DictKey) Then
            Dict.Add DictKey, Nothing
        Else
            Sheet4.ListObjects("EastTable").ListRows(p).Delete
            p = p - 1
        End If
    Next
End Sub
 
Upvote 0
Thank you! I actually just came back to say that if I make it count backwards it works :)



That was my original idea but I did not know if you wanted to erase the first or the last duplicates
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,279
Members
449,075
Latest member
staticfluids

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