简体   繁体   中英

Is there any ways to replace nested if in excel-vba?

Hye I am new to excel vba so don't how to use it efficiently, I got almost 50,000 row of data for the TestID. I want to do some dynamic looping to determine the TestType. Let say if the TestID is 001 to 100 the TestType is TEST A,if TestID is 101 to 220 the TestType is TEST B, if TestID is 221 to 260 the TestType is TEST C.

Is there a way to do this without using nested if using excel vba?

I using nested if to do this but the data is too many

在此处输入图片说明

=IF(ISNUMBER(SEARCH("001",[@TestID])),"Test A",IF(ISNUMBER(SEARCH("002",[@TestID])),"Test A",IF(ISNUMBER(SEARCH("003",[@TestID])),"Test A","")))

If the TestID is 001 to 100 the TestType is TEST A,if TestID is 101 to 220 the TestType is TEST B, if TestID is 221 to 260 the TestType is TEST C

A common, non-VBA method of handling that problem is with a lookup table.

This assumes your TestID values are numeric and not text strings.

For your conditions, the formula might be:

=VLOOKUP(A2,{1,"TEST A";101,"TEST B";221,"TEST C";261,"Unspecified"},2)

在此处输入图片说明

As pointed out by @chrisneilson, if you want to return a blank for values <1 or >=261, you can modify the formula:

=VLOOKUP(A2,{0,"";1,"TEST A";101,"TEST B";221,"TEST C";261,""},2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM