VBA Reference a named range in the file

detriez

Board Regular
Joined
Sep 13, 2011
Messages
193
Office Version
  1. 365
Platform
  1. Windows
@DonkeyOte posted this great code HERE for deleting records with a hotmail.com domain

I have a named range [nEmailDomains] on the fle with a number of domains
I need to do the exact same except I need to reference nEmailDomains and delete every entry with a domain on the list rather than just hotmail.com

VBA Code:
Dim rng As Range
Columns(3).AutoFilter Field:=1, Criteria1:="=*@hotmail.com"
With ActiveSheet.AutoFilter.Range
    Set rng = .Offset(1, 0).SpecialCells(xlCellTypeVisible)
    Application.DisplayAlerts = False
    If Not rng Is Nothing Then rng.Delete
    Application.DisplayAlerts = True
End With
Columns(3).AutoFilter
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
This would substitute your named range for the Columns(3) reference. But wouldn't it be simpler to just do a
VBA Code:
Range("nEmailDomains").ClearContents
or do you have other data in that range?

VBA Code:
Sub t()
Dim rng As Range
Range("nEmailDomains").AutoFilter Field:=1, Criteria1:="=*@*"
With ActiveSheet.AutoFilter.Range
    Set rng = .Offset(1, 0).SpecialCells(xlCellTypeVisible)
    Application.DisplayAlerts = False
    If Not rng Is Nothing Then rng.Delete
    Application.DisplayAlerts = True
End With
Range("nEmail.Domains").AutoFilter
End Sub
 
Upvote 0
Solution
Thanks @JLGWhiz

I'm sorry I wasn't more clear

I need to delete the entire row when Column 3 (my Email column) has one of the values from the nEmailsDomain list

I'm thinking something like this
VBA Code:
Dim rng As Range

Columns(3).AutoFilter Field:=1, Criteria1:=Range("nEmailDomains")
With ActiveSheet.AutoFilter.Range
    Set rng = .Offset(1, 0).SpecialCells(xlCellTypeVisible)
    Application.DisplayAlerts = False
    If Not rng Is Nothing Then rng.Delete
    Application.DisplayAlerts = True
End With
Columns(3).AutoFilter
 
Upvote 0
The latest code I posted throws a method 'Range' of object '_Global' failed error

1608035719090.png
 
Upvote 0
Thank you!!
I got it sorted. My range was actually nEmailDomain not ...Domains
Thank you for your help
 
Upvote 0
Thanks for the feedback,
regards, JLG
 
Upvote 0

Forum statistics

Threads
1,214,905
Messages
6,122,175
Members
449,071
Latest member
cdnMech

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