Customa function for nested if and vlookup
-
Once again look forward for help. I finally decided to learn vba macro and as expected started to face problem initially. Ok, Lets get it simple. i know how to use if condition & vlookup function in vba alone. but am having difficulties in converting my following formula as a custom function combining them together. Here is the folrmula: =IFERROR(IF(B2="Boy",VLOOKUP(A2,$H$2:$H$29,1,FALSE),VLOOKUP(A2,$J$2:$J$10,1,FALSE)),"Name Not Present in Either List") Please see the attachment for better understanding. Anyways here the problem. I have a list of Boys and girls in column A and specified their gender in column B. There is two table in column H and J holding the names of boys and girls respectively. now i am using a condition that if cell b2 is boy then the name in cell a2 should be looked in boys table else it should look at the girls table & finally if no name is found then to give a comment saying "Name not Present" So could you please help me in writing this macro. Thank you very much once again. Sami
-
Answer:
Sami, Usage in A2 would be: =checknames(B2,A2,$H$2:$H$29,$J$2:$J$10) and the formula can be drag filled down the column. Public Function CheckNames(r1 As Range, r2 As Range, r3 As Range, r4 As Range) Dim s As Variant s = Application.WorksheetFunction.IfError( _ IIf(r1 = "boy", _ Application.VLookup(r2, r3, 1, False), _ Application.VLookup(r2, r4, 1, False)), _ "Name Not Present in Either List") CheckNames = s End Function Note that IFError only works in Excel 2007 Also, you can't use vlookup with the worksheet qualifier if you want to use IFError. When you qualify vlookup with worksheetfunction, it raises a real trappable error. You can use it that way, but the code would have to be written differently. It seemed like you wanted to match the version you posted as closely as possible - so I went with the above. It can be programmed much differently and the IFERROR is not required in that case. (In VBA, there is no particular advantage to getting everything in one line like there is in a formula.).
Miningco.com Visit the source
Related Q & A:
- How to fit the model for crossed and nested design using lme function in R?Best solution by Cross Validated
- How to access nested array value?Best solution by stackoverflow.com
- How to query a nested document in mongo?Best solution by Stack Overflow
- How to access a nested function from another nested function in javascript?Best solution by devarticles.com
- how to call a function in Python in another function?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.