VBA Anahtar Kutusu - Excel VBA Switch Bildirimini Kullanma Örnekleri

Excel VBA Anahtar Kutusu

Switch Case veya Select Case , VBA'daki IF-THEN ifadesine alternatif olarak çalıştığı mantıksal testleri gerçekleştirmek için VBA'da bulunan bir ifadedir. Bir Switch Case kullanarak, birden çok mantıksal test yapabilir ve birden çok sonuca dayalı sonuçlara ulaşabiliriz.

Switch Case / Select Case deyiminin sözdizimi aşağıdadır.

Kod:

Durum 1 Testi DOĞRU ise Durum 1 Değerini seçin Durum 2 Testi DOĞRU ise Durum 2 Değeri Durum 3 Test DOĞRU ise Durum 3 Değeri Yukarıdaki durumlardan hiçbiri DOĞRU değilse Durum Başka Değer Son Seçimi

: Mantıksal test nedir? Teste buradan girmemiz gerekiyor.

Durum 1, Durum 2: Her durumda, Excel'de birden çok mantıksal testi test etmemiz gerekir.

VBA Switch Case Bildirimi Nasıl Kullanılır?

Örnek 1

A1 hücresine değeri 550 olarak girdim.

Bu sayıyı switch case deyimlerini kullanarak test edeceğiz ve eğer değer 500'den fazlaysa "500'den fazla" durumuna ulaşacağız yoksa "500'den az" durumuna ulaşacağız.

Önce VBA Alt prosedürünü açın.

Kod:

Alt Switch_Case () End Sub

VBA'da Select Case İfadesini açın ve mantıksal test Aralığı ("A2") değerini sağlayın.

Kod:

Sub Switch_Case () Case Range ("A2") seçin. Value End Sub

Şimdi, Case> 500 olarak ilk durumu girin.

Kod:

Sub Switch_Case () Case Range ("A2") seçin Değer Case Is> 500 End Sub

Bu durum DOĞRU ise, o zaman B hücresinde ihtiyacımız olan sonuç nedir, yani "500'den fazla".

Kod:

Sub Switch_Case () Case Range ("A2") seçin. Değer Case Is> 500 Range ("B2"). Value = "500'den fazla" End Sub

Şimdi geriye tek bir sonuç kaldı, yani Case Else ifadesi. İlk Durum YANLIŞ ise, sonuca "500'den az" olarak ihtiyacımız var.

Kod:

Sub Switch_Case () Case Range ("A2") seçin. Değer Case Is> 500 Range ("B2"). Value = "500'den fazla" Case Else Range ("B2"). Değer = "500'den Küçük" End Sub

Şimdi "End Select" ifadesini kullanarak ifadeyi kapatın.

Kod:

Sub Switch_Case () Case Range ("A2") seçin. Değer Case Is> 500 Range ("B2"). Value = "500'den fazla" Case Else Range ("B2"). Değer = "500'den Az" Son Seçimi Sub

Run the code we will get the value in cell B2.

Since the value in cell A2 is greater than 500, we got the result as “More than 500”.

Example #2

Now we will see using more case examples. Below is the score of the student in the examination.

With this score, we need to arrive at Grade, for that below is the criteria.

  • Score>=85, Grade = “Dist”
  • Score>=60, Grade = “First”
  • Score>=50, Grade = “Second”
  • Score>=35, Grade = “Pass”
  • If anything else Grade = “Fail.”

Code:

Sub Switch_Case1()

 Dim Score As Integer Score = 65 Select Case Score Case Is>= 85 MsgBox "Dist" Case Is>= 60 MsgBox "First" Case Is>= 50 MsgBox "Second" Case Is>= 35 MsgBox "Pass" Case Else MsgBox "Fail" End Select End Sub

Run this code. We will get the grade in the message box.

Since the score is more than 60 but less than 85 grade is “First.”

Example #3

We have seen how to find a grade for one student, what about finding a grade for more than one student. Below are the scores of students.

Since more than one student is involved, we need to enclose FOR NEXT loop in VBA. Below is the VBA code.

Code:

Sub Switch_Case2() Dim k As Integer For k = 2 To 7 Select Case Cells(k, 2).Value Case Is>= 85 Cells(k, 3).Value = "Dist" Case Is>= 60 Cells(k, 3).Value = "First" Case Is>= 50 Cells(k, 3).Value = "Second" Case Is>= 35 Cells(k, 3).Value = "Pass" Case Else Cells(k, 3).Value = "Fail" End Select Next k End Sub

Run this code. We will get grades.

Things to Remember

  • Switch Case is often referred to as “Select Case.”
  • The switch is a function, not a statement.
  • Mantıksal testlerden hiçbiri DOĞRU değilse, alternatif sonucu basitçe CASE ELSE deyiminde geçirebilir ve ifadeyi her zaman "END SELECT" kelimesiyle kapatabilirsiniz.

Ilginç makaleler...