จะใช้ Select Case Statement ใน VBA ได้อย่างไร? (ตัวอย่าง)

Excel VBA เลือกคำชี้แจงกรณี

Select Caseเป็นการทดแทนการเขียน if หลาย ๆ คำสั่งใน VBA เมื่อเรามีเงื่อนไขหลายอย่างในโค้ดเราอาจต้องใช้คำสั่ง If หลาย ๆ คำสั่งซึ่งอาจเป็นเรื่องที่น่าเบื่อเนื่องจากมีความซับซ้อนมากขึ้นเนื่องจากมีการระบุคำสั่ง If มากขึ้นใน เลือกคำสั่งกรณีที่เรากำหนดเกณฑ์เป็นกรณีและผลลัพธ์ที่แตกต่างกันตามพวกเขา

SELECT CASE ช่วยในกระบวนการตัดสินใจ ในกรณีนี้จะใช้เพียงนิพจน์เดียวเพื่อประเมินกรณีต่างๆที่เป็นไปได้ วิเคราะห์เงื่อนไขต่างๆได้อย่างง่ายดายเพื่อรันโค้ดที่พัฒนาขึ้น คำชี้แจงกรณีนี้ใช้เป็นคำสั่งแทน ELSE IF เพื่อประเมินนิพจน์ จัดเป็นฟังก์ชันเชิงตรรกะที่มีอยู่ใน Excel ในการใช้ฟังก์ชั่นนี้รหัสจะถูกป้อนผ่านโปรแกรมแก้ไขภาพพื้นฐานที่แสดงภายใต้แท็บผู้พัฒนา

คำอธิบาย

กลุ่มของคำสั่งต่างๆถูกใช้ใน Select Case ควรปฏิบัติตามไวยากรณ์ต่อไปนี้เพื่อใช้สิ่งนี้อย่างมีประสิทธิภาพ คล้ายกับคำสั่ง switch ที่แสดงในภาษาโปรแกรมอื่น ๆ เช่น Java, C # และ PHP

 เลือก [Case] ​​Expression สำหรับการทดสอบ [Case] ​​List of Expression Statements (Case 1, Case 2, Case 3 และอื่น ๆ ... ) Case Else (Else Statements) End Select 

คำอธิบายสำหรับเงื่อนไขที่เกี่ยวข้องกับกรณีที่เลือกมีดังต่อไปนี้

นิพจน์สำหรับการทดสอบ:จำเป็นต้องประเมินข้อมูลประเภทต่างๆเช่นจำนวนเต็มสตริงบูลีนอ็อบเจ็กต์และอักขระ

รายการนิพจน์:  นิพจน์ถูกสร้างขึ้นพร้อมกับเคสเพื่อค้นหาการจับคู่ที่ตรงกันของอินพุตที่ป้อน หากมีมากกว่าสองนิพจน์สิ่งเหล่านี้จะถูกคั่นโดยใช้ตัวดำเนินการลูกน้ำ 'Is' เป็นคำหลักที่ใช้ในการเปรียบเทียบสองนิพจน์โดยใช้ตัวดำเนินการทางตรรกะใน excel เช่น =,, =

  • End Select:ปิดนิยามเคสของตัวสร้างเลือก
  • คำสั่ง:คำสั่งถูกพัฒนาโดยใช้กรณีเพื่อรันนิพจน์ที่ประเมินเพื่อวิเคราะห์ว่ามีคำสั่งที่ตรงกัน
  • ข้อความอื่น:เป็นการทดสอบคำสั่ง else เมื่อนิพจน์การทดสอบไม่ตรงกับคำสั่งกรณีใด ๆ

จะใช้ VBA Select Case Statement ได้อย่างไร?

คุณลักษณะ Select case ที่ VBA มีให้ไม่ทำงานในแผ่นงานปกติ เราจำเป็นต้องใช้ตัวเลือก Visual Basic ภายใต้แท็บนักพัฒนา ฟังก์ชันที่ผู้ใช้กำหนดเองและการเข้ารหัสที่สร้างขึ้นในโหมดนักพัฒนาเพื่อเรียกใช้แอปพลิเคชันขนาดเล็กต่างๆในสาขาธุรกิจ

ใช้ในสถานการณ์เมื่อมีคำสั่ง if ซ้อนกันใน excel เป็นตัวเลือกที่ดีที่สุดในการจัดการกับคำชี้แจงกรณีต่างๆ ในการใช้ตัวเลือกนี้อย่างมีประสิทธิภาพควรดำเนินการต่อไปนี้

  • ก่อนอื่นต้องสร้างมาโครโดยวางปุ่มคำสั่งควบคุมในแผ่นงาน excel
  • หลังจากนั้นคลิกขวาที่ปุ่มคำสั่งและเลือกตัวเลือกดูรหัส
  • วางรหัสระหว่างฟังก์ชันปุ่มคำสั่งและส่วนท้าย
  • ดีบักโค้ดเพื่อระบุข้อผิดพลาดทางไวยากรณ์
  • คอมไพล์โค้ดเพื่อค้นหาข้อผิดพลาดในการคอมไพล์เพื่อรันโปรแกรมให้สำเร็จ
  • เปลี่ยนค่าอินพุตเพื่อสังเกตผลลัพธ์ที่แตกต่างกันตามเกณฑ์การจับคู่
คุณสามารถดาวน์โหลดเทมเพลต VBA Select Case Excel ได้ที่นี่ - VBA Select Case Excel Template

ตัวอย่าง # 1 - Simple Select Case Statement

ตัวอย่างนี้เพื่อทำความเข้าใจกรณีเลือกอย่างง่ายเพื่อค้นหาค่าที่ตรงกัน

รหัส:

Private Sub Selcaseexmample () Dim A As Integer A = 20 เลือก Case A Case 10 MsgBox "First Case is matched!" กรณีที่ 20 MsgBox "กรณีที่สองตรงกัน!" กรณีที่ 30 MsgBox "กรณีที่สามตรงกันใน Select Case!" กรณีที่ 40 MsgBox "กรณีที่สี่ตรงกับกรณีที่เลือก!" Case Else MsgBox "ไม่มีกรณีใดที่ตรงกัน!" End Select End Sub

ผลลัพธ์:

คำสั่งกรณีสี่ข้อและคำสั่ง case else ใช้เพื่อเปรียบเทียบนิพจน์ทดสอบกับกรณีต่างๆ ในตัวอย่างปัจจุบันกรณีที่สองถูกจับคู่เป็นตัวแปร A ตรงกับ 20

ตัวอย่าง # 2 - คำหลัก 'ถึง' เพื่อทดสอบเกรด

ตัวอย่างนี้อธิบายการใช้คีย์เวิร์ด "ถึง" กับกรณีที่เลือก

รหัส:

Private Sub Selcasetoexample () Dim studentmarks As Integer studentmarks = InputBox ("ป้อนเครื่องหมายระหว่าง 1 ถึง 100?") เลือก Case studentmarks กรณีที่ 1 ถึง 36 MsgBox "Fail!" Case 37 ถึง 55 MsgBox "C Grade" Case 56 To 80 MsgBox "B Grade" Case 81 ถึง 100 MsgBox "A Grade" Case Else MsgBox "Out of range" End Select End Sub

ผลลัพธ์:

คำหลัก 'ถึง' มีประโยชน์ในการกำหนดชุดของนิพจน์ทดสอบในช่วง จะมีประโยชน์ในการค้นหาเกรดที่นักเรียนได้รับโดยการเปรียบเทียบกรณีต่างๆ หลังจากรันโปรแกรมเราต้องป้อนค่าเพื่อให้ได้ผลลัพธ์

ผลลัพธ์จะแสดงพร้อมกล่องข้อความดังที่แสดงในภาพหน้าจอด้านบน

ตัวอย่าง # 3 - การใช้คีย์เวิร์ด "Is" กับ Select Case

ตัวอย่างนี้แสดงให้เห็นถึงการใช้คีย์เวิร์ด "Is" กับกรณีที่เลือก

รหัส:

 Sub CheckNumber () Dim NumInput As Integer NumInput = InputBox ("Please enter a number") เลือก Case NumInput Case Is = 200 MsgBox "คุณป้อนตัวเลขที่มากกว่าหรือเท่ากับ 200" End Select End Sub 

คีย์เวิร์ด 'Is' ช่วยในการค้นหาค่าที่ตรงกันโดยใช้เฉพาะ case statement มันเปรียบเทียบนิพจน์ทดสอบกับอินพุตที่กำหนดเพื่อสร้างเอาต์พุต

ผลลัพธ์จะได้รับดังแสดงในรูปถ้าค่าที่ป้อนมากกว่า 200

ตัวอย่าง # 4 - ด้วยปุ่มคำสั่งเพื่อเปลี่ยนค่า

เลือกกรณีที่ยังใช้กับปุ่มคำสั่งโดยการสร้างแมโคร

รหัส:

 Sub color() Dim color As String color = Range("A1").Value Select Case color Case "Red", "Green", "Yellow" Range("B1").Value = 1 Case "White", "Black", "Brown" Range("B1").Value = 2 Case "Blue", "Sky Blue" Range("B1").Value = 3 Case Else Range("B1").Value = 4 End Select End Sub 

As shown in the program, the input is taken from the cells of the worksheet. The program is executed after clicking on the command button used from ActiveX controls. In this, a comma separator is used to combine the test expressions.

When the value in the Cell A1 changed, it results in a change in the B1 cell also by running the excel macro through the command button. You can refer to the screenshot given below:-

Example #5 – Check a Number Odd or Even

This example to help to check a number is even or odd.

Code:

 Sub CheckOddEven() CheckValue = InputBox("Enter the Number") Select Case (CheckValue Mod 2) = 0 Case True MsgBox "The number is even" Case False MsgBox "The number is odd" End Select End Sub 

The coding is developed as shown in the screenshot and output is displayed as follows when even number is entered.

Example #6 – Nested Select Case Statements

Nesting is a useful feature of the select case and the way how it is done is shown.

Code:

 Sub TestWeekday() Select Case Weekday(Now) Case 1, 7 Select Case Weekday(Now) Case 1 MsgBox "Today is Sunday" Case Else MsgBox "Today is Saturday" End Select Case Else MsgBox "Today is a Weekday" End Select End Sub 

Here the select case is defined inside another select case to test a day, weekday, or weekend. Except case 1 and 7 remaining all are weekdays (Note: case 1 is Sunday and case 7 is Saturday)

Things to Remember

  • The ‘Is’ keyword using in the statement of [case] and [case else] are not equal.
  • ActiveX control should be used in executing program through a command button taking input and displaying output in the excel sheet range of cells.
  • It is hard to run the programs in VBA if macros are disabled and always enable Macros to get best results.
  • VBA is a case sensitive and input should be entered accurately to get better results.