Run Time Error 1004

amoverton2

Board Regular
Joined
May 13, 2021
Messages
77
Office Version
  1. 2016
Platform
  1. Windows
Hi all!

So out of the blue I'm starting to get a run-time error with this code (Run Time Error 1004: Select Method of Range class failed) that used to have no issues... Can someone tell me why, or how to fix or change the code???

Note: I did try doing the suggested ways of fixing it to no avail via other posts and other online posts

VBA Code:
Private Sub cmdDELETE_Click()
Dim account As String
Dim r As Long


If cmbDELNAME.Text = "" Then
    MsgBox "Enter Account Number"
End If

account = Trim(cmbDELNAME.Text)

r = 2
    Worksheets("O_ProspectiveGain_Add").activate
    Do While Worksheets("O_ProspectiveGain_Add").Cells(r, 2) <> ""
    If Worksheets("O_ProspectiveGain_Add").Cells(r, 2).Value = account Then
        Worksheets("O_ProspectiveGain_Add").Cells(r, 2).Select
        ActiveCell.EntireRow.Delete Shift:=xlUp
        End If
        r = r + 1
   Loop

Worksheets("O_Sponsor_Add").activate
r = 2
    Do While Worksheets("O_Sponsor_Add").Cells(r, 2) <> ""
    If Worksheets("O_Sponsor_Add").Cells(r, 2).Value = account Then
        Worksheets("O_Sponsor_Add").Cells(r, 2).Select
        ActiveCell.EntireRow.Delete Shift:=xlUp
        End If
        r = r + 1
   Loop
Worksheets("O_AdminInfoGain_Add").activate
r = 2
    Do While Worksheets("O_AdminInfoGain_Add").Cells(r, 2) <> ""
    If Worksheets("O_AdminInfoGain_Add").Cells(r, 2).Value = account Then
        Worksheets("O_AdminInfoGain_Add").Cells(r, 2).Select
        ActiveCell.EntireRow.Delete Shift:=xlUp
        End If
        r = r + 1
   Loop

Worksheets("O_LastContact_Add").activate
r = 2
    Do While Worksheets("O_LastContact_Add").Cells(r, 2) <> ""
    If Worksheets("O_LastContact_Add").Cells(r, 2).Value = account Then
        Worksheets("O_LastContact_Add").Cells(r, 2).Select
        ActiveCell.EntireRow.Delete Shift:=xlUp
        End If
        r = r + 1
   Loop

Worksheets("O_TravelInfo_Add").activate
r = 2
    Do While Worksheets("O_TravelInfo_Add").Cells(r, 2) <> ""
    If Worksheets("O_TravelInfo_Add").Cells(r, 2).Value = account Then
        Worksheets("O_TravelInfo_Add").Cells(r, 2).Select
        ActiveCell.EntireRow.Delete Shift:=xlUp
        End If
        r = r + 1
   Loop

Worksheets("O_FamilyInfo_Add").activate
r = 2
    Do While Worksheets("O_FamilyInfo_Add").Cells(r, 2) <> ""
    If Worksheets("O_FamilyInfo_Add").Cells(r, 2).Value = account Then
        Worksheets("O_FamilyInfo_Add").Cells(r, 2).Select
        ActiveCell.EntireRow.Delete Shift:=xlUp
        End If
        r = r + 1
   Loop

Worksheets("O_MiscNotes_Add").activate
r = 2
    Do While Worksheets("O_MiscNotes_Add").Cells(r, 2) <> ""
    If Worksheets("O_MiscNotes_Add").Cells(r, 2).Value = account Then
        Worksheets("O_MiscNotes_Add").Cells(r, 2).Select
        ActiveCell.EntireRow.Delete Shift:=xlUp
        End If
        r = r + 1
   Loop

    MsgBox "Member Deleted"
    
    ThisWorkbook.Save
    MsgBox "Saved"
    
End Sub

Thank you!!!
 
Okay, no more runtime error, however nothing is actually deleted...

Basically, the original code was to find a name (that was entered into a combobox (with a named range)) and delete the entire row in which that name was on in those specific sheets.
I think I know what it is, my guess is that it is not finding the Account specified on one or more of the sheets.
Try
Replacing this
VBA Code:
  rngToDelete.EntireRow.Delete

With this
VBA Code:
        If Not rngToDelete Is Nothing Then
            rngToDelete.EntireRow.Delete
        End If
 
Upvote 0

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Do you know how to look in the immediate window ?

For trouble shooting purposes, replace the code in my previous post with this, which adds some debug.print lines.
The show us what is in the immediate window.

Can you also show us what the data looks like in column 2 (the account column), preferably also showing the Row & Column references)

VBA Code:
        If Not rngToDelete Is Nothing Then
            Debug.Print "Delete from sheet: "; sht.Name; vbTab; "Range: "; rngToDelete.Address
            rngToDelete.EntireRow.Delete
        Else
            Debug.Print "Nothing Deleted: "; sht.Name; vbTab; "account value "; account
        End If
 
Upvote 0
I'm a dumbass... So I used your code but forgot to change the sheet names and was deleting something somewhere else in the workbook...

I'm pretty new with VBA and I appreciate your help and everyone else who has helped me out on my project... there a couple of questions coming so I look forward to the knowledge you guys/gals will bestow on me...
 
Upvote 0
Need to add If sheet name is correct in check list ?
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,262
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