+ Reply to Thread
Results 1 to 21 of 21

Using Case Select On Range With Multiple Conditions?

  1. #1
    Registered User
    Join Date
    06-05-2013
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    39

    Using Case Select On Range With Multiple Conditions?

    Hey all,

    Need some assistance with a macro I am currently trying to formulate. I ususally use AutoFilter or IF Statements, so I am totally new to Case Select and need some basic help. To give some background info on what I'm working on, I have a file with various data. I need to create a Case Select to go through each row so to go from A2 (Since A1 is header) till the the last cell (End(xlDown) but Idk how to do that with Case Select) and then do something like the following:

    •If Q2 = "Cash" AND J2 = "GC" then change cell A2 to "Capital Activity"
    •If Q2 = "Bond Deals" AND B2 contains "CLO" (don't know how to do this in case select either) then change cell A2 to "CLO Subtype"
    •If Q2 = "Bond Deals" AND R2 has 9 or 12 characters (don't know how to do this in case select either..use LEN?) then change cell A2 to "Bond"

    If anyone can provide me with that I can figure out the rest of my macro on my own. I have provided my previous macro which got too messy/big/complicated which I tried to do the same thing using AutoFilter. After talking to one of my peers, he advised I look into using Case Select so that is why I am asking. Thanks in advance. I have also provided the file for viewing/using.

    For The Q2 = Cash & J2 = "GC" > A2: Capital Activity
    Please Login or Register  to view this content.
    For The Q2 = Bond Deals & B2 containing "CLO" > A2: CLO Subtype
    Please Login or Register  to view this content.
    As you can see, my code is very messy and complicated. I would really appreciate help, and am more than ready to provide more information if needed. Here is the example file. Thanks In Advance!

    https://www.dropbox.com/s/21xhnd8dz5...electEG.xlsx?m

    Also, have posted problem on friend site OzGrid: http://www.ozgrid.com/forum/showthre...371#post672371
    Last edited by alulla; 07-03-2013 at 11:37 AM.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Using Case Select On Range With Multiple Conditions?

    Your criteria aren't really suited for Select Case on it's own.

    Select Case works best when you are checking a single value, eg a cell value, against a set of criteria.

    You could use it for Q2 and nest some Ifs, but you could also just use a set of If statements.
    Please Login or Register  to view this content.
    With Select Case that would look something like this.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    06-05-2013
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    39

    Re: Using Case Select On Range With Multiple Conditions?

    Quote Originally Posted by Norie View Post
    Your criteria aren't really suited for Select Case on it's own.

    Select Case works best when you are checking a single value, eg a cell value, against a set of criteria.

    You could use it for Q2 and nest some Ifs, but you could also just use a set of If statements.
    Thank you Norie! I've been thinking about using If Statements, but do you think they'd provide for what I'm trying to do? I am going to give If statements a try and post back here. Thanks for your quick reply though!

    EDIT: It is currently giving me an error on "If cl.Offset(, 16).Value = "Bond Deals".....

    and also it went through my first line and did not change A2 to "Capital Activity" even though both values were met? I am confused...
    Last edited by alulla; 07-03-2013 at 12:06 PM.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Using Case Select On Range With Multiple Conditions?

    Well they should work for the criteria you laid out.

  5. #5
    Registered User
    Join Date
    06-05-2013
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    39

    Re: Using Case Select On Range With Multiple Conditions?

    Quote Originally Posted by Norie View Post
    Well they should work for the criteria you laid out.
    Yes nevermind fixed it. But for the CLO, how would I make it contains CLO...like in my AutoFilter macro I had done =*CLO * or something like that to show that it contained and was not specifically "CLO". Any idea? By the way, thanks again. This will help me so much you have no idea now that I know what I have to do.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Using Case Select On Range With Multiple Conditions?

    Oops, didn't spot the 'contains' bit.

    Change ="CLO" to Like "*CLO*"
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    06-05-2013
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    39

    Re: Using Case Select On Range With Multiple Conditions?

    Quote Originally Posted by Norie View Post
    Oops, didn't spot the 'contains' bit.

    Change ="CLO" to Like "*CLO*"
    Please Login or Register  to view this content.
    Ah! That makes sense, I changed it and it works for when there is a CLO but if the Comment is "#N/A" it comes up to an error saying " Run-time error '13': Type mismatch". Any idea how to change this? Thanks again.

    Also, you know how you showed me how to make it "Like "*CLO*"? How would I do it if I wanted to do it only if it begun with CLO?
    Last edited by alulla; 07-03-2013 at 01:05 PM.

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Using Case Select On Range With Multiple Conditions?

    #N/A is actually an error value and that's why you get the type mismatch.

    Which column(s) do you have that in?

    To see if CLO is at the beginning use "CLO*".

  9. #9
    Registered User
    Join Date
    06-05-2013
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    39

    Re: Using Case Select On Range With Multiple Conditions?

    Quote Originally Posted by Norie View Post
    #N/A is actually an error value and that's why you get the type mismatch.

    Which column(s) do you have that in?

    To see if CLO is at the beginning use "CLO*".
    It is in column B aka when we offset it by (, 1). I think I found out what the problem is. When I click on "#N/A" it displays a VLookup...do you think you could help me come up with a line of code which automatically converts all the data at the start of the file into the values which the formulas have come up with?

    That way the problem shouldn't show up right? And also, thank you for the CLO*...i was going go try =CLO* off a google search i did lol, so I would have been puzzled.

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Using Case Select On Range With Multiple Conditions?

    What you could do is get rid of the #NA by wrapping the VLOOKUPs, and any other formulas in ISERROR.

    =ISERROR(VLOOKUP(...), "N/A")

    That returns the string "N/A" not the error #NA, so you can still see there's an error but it won't cause a problem in the code.

  11. #11
    Registered User
    Join Date
    06-05-2013
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    39

    Re: Using Case Select On Range With Multiple Conditions?

    Quote Originally Posted by Norie View Post
    What you could do is get rid of the #NA by wrapping the VLOOKUPs, and any other formulas in ISERROR.

    =ISERROR(VLOOKUP(...), "N/A")

    That returns the string "N/A" not the error #NA, so you can still see there's an error but it won't cause a problem in the code.
    Is there anyway to simply automatically convert all the data at the start of the macro into the values which the formulas have already come up with?

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Using Case Select On Range With Multiple Conditions?

    Yes, you could do a copy, paste special values.

    However you would still have the #NA values and you would still get the type mismatch error.

    By the way, every row in column B has the #NA error.

  13. #13
    Registered User
    Join Date
    06-05-2013
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    39

    Re: Using Case Select On Range With Multiple Conditions?

    Quote Originally Posted by Norie View Post
    Yes, you could do a copy, paste special values.

    However you would still have the #NA values and you would still get the type mismatch error.

    By the way, every row in column B has the #NA error.
    Yes, I know it was just something I did for the example by accident. And could you by any chance write out the code to do a copy, paste special values that would perform such a function? Because after that, I figure I'd just do an If statement and do something like If (B2 = "#N/A") make B2 = "N/A"...would that work? Anyway you could write something like that? Thanks for all your help, I've already composed half of my full macro following your format and once I get those two lines I'm sure the macro will work exactly how I want it to! Going into this, I really didn't think it'd be as simple as If statements.

  14. #14
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Using Case Select On Range With Multiple Conditions?

    If you do convert to values you'll still have the error values, and you'll still get the data type mismatch error.

    B2 = "N/A" will generate a type mismatch error if B2 is #N/A because you are trying to compare a string and an error value.

    You could add If Not IsError() throughout the code.

    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    06-05-2013
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    39

    Re: Using Case Select On Range With Multiple Conditions?

    Quote Originally Posted by Norie View Post
    If you do convert to values you'll still have the error values, and you'll still get the data type mismatch error.

    B2 = "N/A" will generate a type mismatch error if B2 is #N/A because you are trying to compare a string and an error value.

    You could add If Not IsError() throughout the code.

    Please Login or Register  to view this content.
    I understand what you're saying but am kind of confused why can't I do the following.

    1) Copy & Paste Special All The Values so Everything is a value. What would the error "#N/A" show up if we do this? #N/A right? If so then:
    2) Do an IF statement saying If B2 = "#N/A" make B2 = "NA" or something....wouldn't this work since #N/A would be a String now since we just pasted special and made it into an actual value?

    I completely understand your alternative, but feel like this would be easier especially since I want it converted into Values only anyway..

  16. #16
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Using Case Select On Range With Multiple Conditions?

    When you copy and paste the result of the formula, the error value #NA, will still be the error value #NA, it won't be converted to a string.

  17. #17
    Registered User
    Join Date
    06-05-2013
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    39

    Re: Using Case Select On Range With Multiple Conditions?

    Ohhhhhh. Damn. So I guess I have to use your alternative?

  18. #18
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Using Case Select On Range With Multiple Conditions?

    You've got 2 options I suppose.

    1 Eliminate the errors at source.

    2 Write code to deal with them.

    I would normally go for 1, but if that's not possible then 2.

  19. #19
    Registered User
    Join Date
    06-05-2013
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    39

    Re: Using Case Select On Range With Multiple Conditions?

    Quote Originally Posted by Norie View Post
    You've got 2 options I suppose.

    1 Eliminate the errors at source.

    2 Write code to deal with them.

    I would normally go for 1, but if that's not possible then 2.
    Okay, thanks for all your advice. You've been a great deal of help. Is there anyway before you left you could write out the code to copy and paste all the values as special that I could start my macro off with?? I need that anyway and only know how to do it sheet to sheet.

  20. #20
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Using Case Select On Range With Multiple Conditions?

    Something like this, change the sheet name as required.
    Please Login or Register  to view this content.

  21. #21
    Registered User
    Join Date
    06-05-2013
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    39

    Re: Using Case Select On Range With Multiple Conditions?

    Quote Originally Posted by Norie View Post
    Something like this, change the sheet name as required.
    Please Login or Register  to view this content.
    Thank you, you're the man. And I'll most likely just insert the code like you said 3-4 posts back in front of each one to make it work. Thanks again man. Sorry for all the questions, you truly are a "Guru".

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1