Using a Range Name in a Macro

I have a Macro that has the following line it it.  I also have a Range Name named CustomerData that is composed of A7:B50.

Macro line of code:

        ActiveSheet.Range("$A$7:$B$50").AutoFilter field:=1, Criteria1:=Left(regionArray(iNextReg), 2)
    End If

End Sub


For this line of code, I'd like to make it so that if I change the size of the range (i.e. by adding another line or two), that the Macro performs against the range and not the A7:B50.  How do I code that?

Thank you,

B.
Bright01Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MacroShadowCommented:
ActiveSheet.Range(NameofRange).AutoFilter field:=1, Criteria1:=Left(regionArray(iNextReg), 2)
0
Bright01Author Commented:
Please elaborate a little more;  Is the Criteria1 because I have two columns or if they are all included already in the range name, may I simply refer to the range name?

Thank you,

B.
0
MacroShadowCommented:
If you have a dynamic named range, which I imagine you do, instead of hard-coding the range you can use the named range as I showed in the previous post. It will relate to to the entire named range.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Rory ArchibaldCommented:
(not for points)
When using an autofilter, you can specify one or two criteria per column. Since you only specify one here, you only see criteria1. It has nothing to do with the size of the range you are filtering.
So
ActiveSheet.Range("CustomerData").AutoFilter field:=1, Criteria1:="some value"

Open in new window


means only show values in the CustomerData range where the first column equals "some value".
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
You can also just use the range name in square brackets:
[CustomerData].AutoFilter field:=1, Criteria1:=Left(regionArray(iNextReg), 2)

Open in new window

0
Rory ArchibaldCommented:
As long as you don't have a variable in scope called CustomerData. ;)
0
Bright01Author Commented:
I'm in the presence of great minds!  

So in Microshadow's example for me; what does the Criteria here specify?

ActiveSheet.Range(NameofRange).AutoFilter field:=1, Criteria1:=Left(regionArray(iNextReg), 2)

?
0
Bright01Author Commented:
Sorry, it wasn't Microshadow's Criteria, it was what I copied for him to look at.  Still would like to know what it means.

B.
0
MacroShadowCommented:
Hard to tell without seeing the actual file.

Basically, its the two left-most characters of the iNextReg element of the regionArray array.
0
Bright01Author Commented:
Thank you Microshadow!  Very helpful.  I will be authoring more questions on the particular application since I'm still learning to build it.

Thanks again,

B.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.