확인해야 할 조건이 많거나 사용자의 선택에 따라 작업을 수행하려는 경우 If Else If 문을 사용할 수 있습니다. 그러나 여러 조건에서 문제가됩니다. 이 경우 Excel VBA의 최상의 솔루션은 사례 선택 * 방법입니다.

VBA에서 Select Case 문이란 무엇입니까?

VBA의 Select Case 문은 확인할 조건이 여러 개인 경우 유용합니다. 다른 프로그래밍 언어의 switch 문과 같습니다. 주어진 변수를 확인하고 일치하는 케이스를 실행합니다.

Excel VBA의 Select Case 구문

Select Case variable

case Condition1

result1

case Condition2

Condition2

케이스 조건 N

결과 N

[다른 경우

resultElse]

Select Case:Here Select Case* is a keyword to start the case check.

Variable: Variable is any variable that you want to check.

Case: from the next line, we start checking the conditions with variables.

You can have as many conditions as you want. We can check any condition with the keyword case. The condition can be a logical statement or a straight value of the Selected Variable. It will always result in true-false value. These all are valid statements:

케이스 영역 선택

‘정확한 값 사용

케이스 “Central”

Range ( “D1”). Value = 지역

‘is 문 사용

Case Is = “Central”

Range ( “D1”). Value = 지역

선택 종료

With is you can put arithmetic logical operators to check if the Case is equal to(=), greater than(>), less than(<), etc.

You can use the comma (",") to do one operation on multiple conditions (like or operator).

Case Is = “West”, “North”, “South”

Range ( “D1”). Value = 지역

You can also use To operator to check among large ranges.

사례 1 ~ 40

등급 = “F”

케이스 41 ~ 60

Grade = “C”

케이스 61 ~ 80

Grade = “B”

케이스 81에서 100

Grade = “A”

The Case Else statement is optional. You can use it to do something by default if none of the case match. Like showing a message that the choice is invalid or something.

다른 경우

MsgBox “잘못된 옵션”

Now that we know about the basics of the Select Case, let's have an example.

===  Example: Create A Grading Function UsingVBA Select Case We need to create a function that checks the supplied value and returns the grades according to below rules:

*  If >41, Grade="F"

*  If between 41 and 60, Grade="C"

*  If between 61 and 80, Grade="B"

*  If between 81 and 100, Grade="A"

*  else #VALUE! error.

We will use the Select Case as a Switch statement of VBA here. The code for the custom function will be:

기능 GRADES (num As Double)

케이스 번호 선택

케이스는 <41

학년 = “F”

사례 41 ~ 60

학년 = “C”

케이스 61 ~ 80

학년 = “B”

케이스 81 ~ 100

학년 = “A”

다른 경우

GRADES = “#VALUE!”

선택 종료

끝 기능

image:https://www.office-skill.site/images/wp-content-uploads-2020-01-Capture-43.jpg[image,width=658,height=282]

Now if you use this function on the sheet, it will return the grades easily.

image:https://www.office-skill.site/images/wp-content-uploads-2020-01-Capture-42.jpg[image,width=369,height=288]

So yeah guys, this is how the Select Case (switch) statement is used in Excel VBA to check multiple conditions. Instead of multiple If Else If statement we use the Select Case statement to switch results. If you have any doubts regarding this article or any other Excel/VBA related articles, ask in the comments section below.

Download the working file below:

image:https://www.office-skill.site/images/wp-content-uploads-2015-06-image-481.png[image 48,width=128,height=49]



===  Related Articles:

`link:/office-skill-ko/using-loops-using-loops-in-vba-in-microsoft-excel[Using Loop in VBA in Microsoft Excel]`| The loops in VBA enable us to do a similar task over and over without repetition of code. There are 3 types of loops in Excel VBA.

`link:/office-skill-ko/using-loops-for-loops-with-7-examples[7 Examples of For Loops in Microsoft Excel VBA]`| The 7 examples of for loop can make your automation life easy. Let's start with simple for loop.



===  Popular Articles:

`link:/office-skill-ko/keyboard-formula-shortcuts-50-excel-shortcuts-to-increase-your-productivity[50 Excel Shortcuts to Increase Your Productivity]` | Get faster at your task. These 50 shortcuts will make you work even faster on Excel.

`link:/office-skill-ko/formulas-and-functions-introduction-of-vlookup-function[The VLOOKUP Function in Excel]` | This is one of the most used and popular functions of excel that is used to lookup value from different ranges and sheets. `link:/office-skill-ko/tips-countif-in-microsoft-excel[COUNTIF in Excel 2016]` | Count values with conditions using this amazing function. You don't need filter your data to count specific value.

Countif function is essential to prepare your dashboard.

`link:/office-skill-ko/excel-formula-and-function-excel-sumif-function[How to Use SUMIF Function in Excel]` | This is another dashboard essential function. This helps you sum up values on specific conditions.