2

I am trying to name a range based on a .Find then define that range as my variable so I can enter the variable into a different function. When I run the code I get a type mismatch error.

Sub Faked() Dim r As Range Cells.Find(What:="EE status", After:=ActiveCell, LookIn:=xlFormulas, _ LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False).Activate Range(Selection, Selection.End(xlDown)).Name = "Win" Set r = ("Win") End Sub 
17
  • 2
    Try Set r = range("Win") Commented May 18, 2016 at 21:09
  • 2
    @findwindow Dude I wish you would answer some of these. we need to get you above 2000 so you can edit the questions. Commented May 18, 2016 at 21:09
  • 2
    for each c in r | debug.print c | next? Commented May 18, 2016 at 21:12
  • 2
    You could Debug.Print r.Address it will show you the range address in the immediate window. Commented May 18, 2016 at 21:13
  • 2
    It is working perfectly. Thank you both very much. @Findwindow I have found out that if you are online my questions get answered much more clearly and quickly than any other time.. Commented May 18, 2016 at 21:16

2 Answers 2

6

Because Scott says so. Try Set r = range("Win")

To test if r picked up the range correctly, one could do

for each c in r debug.print c next 

Edit: or if you're cool like Dirk,

Set r = [Win] 
Sign up to request clarification or add additional context in comments.

6 Comments

lol This is so not worth 4 upvotes. I mean, this only got the same number of votes and is much much more impressive. Edit: also, whoever did that left me a comment but deleted it before I can catch it...
LOL do you want me to downvote it? I won't do it unless you're OK with that...
@UnEscapedCharacter I am ok with that.
@DirkReichel are you doing that?
@findwindow just tried that but I guess I am still too new on SO but thanks for your support, feel free to return the favor anytime, LOL
|
5

Replace the code with this:

 Dim r As Range Cells.Find(What:="EE status", After:=ActiveCell, LookIn:=xlFormulas, _ LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False).Activate Set r = Range(Selection, Selection.End(xlDown)) r.Name = "Win" 

Explanation:

You cannot use the SET command with the name property of the range object. The object needs to be populated with the range selection first, then you can just use the property of the object to assign a value to it, in this case the Name property of it.

Hope that helps!

1 Comment

Glad to help, just don't forget to keep track of all the object variables in the code... give them a name that is easy to understand, like "rngMyEmployeeStatus" and so on...! ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.