Worksheet Functions in macro with named ranges

Kabeer456

New Member
Joined
Aug 20, 2012
Messages
10
Good morning all-

I'm sure the answer is staring at me in the face, but I've wasted an entire day trying every syntax under the sun. I've also read through many similar posts, but none of the solutions seem to work for my situation.

Background: I'm running a macro on a rather lengthy spreadsheet. First step is that it creates a hidden sheet ("Current PMs") containing 2 columns (A: last names, B: first names). My code also assigns each list a to named range (LastNames & FirstNames). The list is subject to change as head count changes so the code treats the list as a dynamic range by finding the last row. Code is posted below...this part works perfectly. I've even confirmed the named ranges contain the proper data sets. I also believe I've made them global for the entire workbook to access.

'==========================================
'Create a named list for LastNames
Set rMyRg = Range([A1], [A1].End(xlDown))

ActiveWorkbook.Names.Add Name:="LastNames", RefersToR1C1:="='" & _
ActiveSheet.Name & "'!" & rMyRg.Address(ReferenceStyle:=xlR1C1)

'Create a named list for FirstNames
Set rMyRg = Range([B1], [B1].End(xlDown))

ActiveWorkbook.Names.Add Name:="FirstNames", RefersToR1C1:="='" & _
ActiveSheet.Name & "'!" & rMyRg.Address(ReferenceStyle:=xlR1C1)

'Hide sheet
Sheets("Current PMs").Visible = xlSheetHidden
'==========================================

Problem
: Further down in the macro, I want to compare a column of the master spreadsheet against the LastNames range previously defined. While I think I have the syntax correct for referring to the named range, I thought I was in the clear. I want to delete all rows that DO NOT contain a value found in my named range (L. Here's what I've got thanks to help in another post.

'==========================================
Sub EdisonTrim()
Dim PM_Range As Range
Dim PMCol As Range
Dim bRow As Long

bRow = Cells(Rows.Count, Columns("F").Column).End(xlUp).Row

'Set PM_Range to the LastNames range defined on the Current PMs tab
Set PM_Range = Range("LastNames")

'Set PMCol to the truncated PM list in column F on the master sheet
Set PMCol = Range("F2:F" & bRow)

For Each cell In PMCol
If Application.WoksheetFunction.CountIf(PM_Range, cell.Value) = 0 Then
cell.EntireRow.Delete
End If
Next cell

End Sub
'==========================================

Every time I run this, I get an error "Object doesn't support this property or method."

Does anything stand out here? I thought my syntax was correct, but something is obviously amiss.


As always, your collective wisdom is much appreciated.

-Brian Bordieri
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hmmm... I'm glad it's Friday. I had to look 3 times at my code to make sure I didn't transpose it incorrectly somehow.

I should have known better with that error.

At any rate, this has solved my problem! I have many others, but none as "difficult" as recognizing a misspelled command!

Cheers! My stupidity thanks you.
 
Upvote 0

Forum statistics

Threads
1,215,420
Messages
6,124,803
Members
449,190
Latest member
cindykay

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