VBA Durum Bildirimi - VBA Select Case Bildirimi Örnekleri

Excel VBA Durum Bildirimi

VBA Case İfadesi mantıksal işlevlerden biridir. Case İfadesi birden fazla mantıksal testi test eder ve sonuca iki şekilde ulaşır; örn. Sonuç veya mantıksal test DOĞRU ise, bir dizi sonuç ve sonuç veya mantıksal test YANLIŞ ise, ikinci sonuç kümesi.

Mantıksal testler tipik olarak, çalışma sayfası formülü veya VBA kodlamasında IF formülleri kullanılarak gerçekleştirilir; her iki platformda da, bu işlev birçok türde karmaşık hesaplama yapmamıza yardımcı olur. Çoğumuz, VBA'daki IF ifadesine bir alternatifimiz olduğunu, yani "Case Statement" olduğunu fark etmiyoruz. Bu makale size bu mantıksal ifade hakkında tüm ayrıntıları sağlar.

Sözdizimi

Aşağıda "Case Seç" ifadesinin sözdizimi verilmiştir.

Durum 1 "Test Edilecek Değer" Durumunu Seçin Durum, "Mantıksal Test" Sonucudur Durum 1 DOĞRU ise Durum "Mantıksal Test" Sonucudur Durum 2 DOĞRU ise Durum "Mantıksal Test" Sonucudur Durum 3 DOĞRU ise Durum Başka Değilse sonuçlar DOĞRU Son Seçimi

Bu neredeyse IF deyimi sözdizimine benzer, ancak ELSEIF kullanmak yerine Case 1, Case 2, Case 3 vb. Kullanırız.

VBA Durum Bildirimi Örnekleri

Örnek 1

A1 hücresine numarayı 240 olarak girdim.

Şimdi bu sayının 200'den büyük olup olmadığını SELECT CASE deyimini kullanarak test edeceğiz .

Adım 1: Select Case bildirimini şimdi açın.

Kod:

Sub Select_Case_Example1 () Case End Sub seçin

Adım 2: "Vakayı Seçin" açıldıktan sonra, test ettiğimiz değeri sağlamamız gerekir. Bu durumda, A1 hücresi değerlerini test ediyoruz.

Kod:

Alt Select_Case_Example1 () Case Range ("A1") seçin. Value End Sub

Adım 3: Test edilecek değer verildikten sonra, " Case Is " kelimesini kullanarak excel'de mantıksal testler uygulamamız gerekiyor .

Kod:

Alt Seçim_Temel_Örnek1 () Vaka Aralığını Seçin ("A1") Değer Durum Is> 200 End Sub

Adım 4: Şimdi, sonraki satırda, uygulanan mantıksal test DOĞRU ise "sonuç" değerini vermemiz gerekiyor. Mesaj kutusunda "Sayı> 200" sonucuna ihtiyacımız var.

Kod:

Alt Seçim_Temel_Örnek1 () Vaka Aralığını Seçin ("A1") Değer Durum> 200 Mesaj Kutusu "Sayı> 200"

Adım 5: Bu örnekte, sadece iki sonuca ihtiyacımız var, bu yüzden daha fazla "Case Is" ifadesi kullanmayacağım. Daha sonra, VBA "Select Case" ifadesini kapatmak için "Case Else" kelimesini kullanacağım.

Kod:

Sub Select_Case_Example1 () Case Range ("A1") seçin. Değer Case Is> 200 MsgBox "Number> 200" Case Başka Mesaj Kutusu "Numarası <200" End Sub

Adım 6: Tüm vakalar sağlandıktan sonra, "End Select" kelimesini kullanarak select case ifadesini kapatmamız gerekir.

Kod:

Alt Select_Case_Example1 () Case Range ("A1") seçin. Değer Case Is> 200 MsgBox "Number> 200" Case Başka Mesaj Kutusu "Numarası <200" End Select End Sub

Adım 7: Şimdi kodu çalıştırın ve VBA mesaj kutusunda aldığımız sonucun ne olduğunu görün.

Elde ettiğimiz sonuç "Sayı> 200", çünkü A1 hücresindeki değer 240, yani> 200.

Örnek 2

Şimdi test puanlarının bazı pratik gerçek zamanlı örneklerini göreceğiz. Aşağıdaki VBA koduna bakın.

Kod:

Alt Select_Case_Example2 () Dim ScoreCard As Integer ScoreCard = Application.InputBox ("Puan 0 ila 100 arasında olmalıdır", "Test etmek istediğiniz puan nedir") Case ScoreCard Case Is> = 85 MsgBox "Distinction" Durumunu Seçin Is> = 60 MsgBox "First Class" Case Is> = 50 MsgBox "Second Class" Case Is> = 35 MsgBox "Pass" Case Else MsgBox "Fail" End Select End Sub

Daha iyi anlamak için kodu satır satır açıklamama izin verin.

Öncelikle değişkeni Tamsayı olarak tanımladım ve bu değişken için, bir kullanıcının 0 ile 100 arasında puanı girmesi gereken VBA'da InputBox'ı atadım.

Kodu çalıştırdığınızda aşağıdaki gibi bir giriş kutusu göreceksiniz ve bu giriş kutusuna puanı girmeniz gerekiyor.

Şimdi, giriş kutusuna girdiğimiz her şey "ScoreCard" değişkeninde saklanacaktır.

In the next line, I have applied a select case statement to test this score.

First, it will test the ScoreCard>=85 or not. If this is TRUE, then we will get the value in the message box as “Distinction.”

Select Case ScoreCard Case Is>= 85 MsgBox "Distinction"

Similarly, in the following lines, I have applied the second test as ScoreCard>=60. If this is TRUE, then it will show the result as “First.”

Case Is>= 60 MsgBox "First Class"

Like this, I have applied other tests as well, and in the end, I have used the “Case Else” statement. If all the applied logical tests are FALSE, then we will get the result as “Fail.”

Case Else MsgBox "Fail"

Now I have supplied 68 as the score, and we should get the result as “First Class” in the message box.

Example #3 - Using the “To” keyword

In the above example, we have used student scores to arrive at the result. The same test can be conducted by using the “To” word to determine the lower limit and upper limit of the logical test.

Code:

Sub Select_Case_Example3() Dim ScoreCard As Integer ScoreCard = Application.InputBox("Score should be b/w 0 to 100", "What is the score you want to test") Select Case ScoreCard Case 85 To 100 MsgBox "Distinction" Case 60 To 84 MsgBox "First Class" Case 50 To 59 MsgBox "Second Class" Case 35 To 49 MsgBox "Pass" Case Else MsgBox "Fail" End Select End Sub

I have used the same code as above, but the only yellow-colored area I have changed here. Based on the number we type in the input box accordingly, we will get the result.

Things to Remember

  • Select Case, IF ifadesine bir alternatiftir.
  • Select Case yalnızca VBA ile kullanılabilir.
  • "Case Select" in ilk satırında, yalnızca test edilmesi gereken değeri sağlamamız gerekiyor. Daha sonra "Case" satırında mantıksal testi uygulamamız gerekiyor. Bu bizim IF koşulumuza benzemiyor.

Ilginç makaleler...