Friday, April 25, 2014

Instruksi If Then Else dan Select Case Else Pada VBA

www.AstroDigi.com AstroDigi.com | Senin, 21 Apr 2014 | Dalam pemrograman tentu seringkali ada keputusan yang memerlukan pencabangan. Misal: Bila A=1 maka aksi yang dilakukan adalah ABC, bila A=2 maka aksi yang dilakukan adalah DEF, dst. Pengelompokan data, pemilihan output, adalah contoh pekerjaan yang perlu pencabangan.

Instruksi pencabangan pada VBA ada dua pilihan yaitu: If Then Else dan Select Case. Instruksi If Then lebih efektif untuk pencabangan dengan sedikit pilihan (misalnya 2 atau 3 pilihan saja), sedangkan instruksi Select Case Else akan efektif untuk pencabangan dengan banyak pilihan.

Misal:
Dim Aksi As String

If Lampu = "Merah" Then
  Aksi = "Berhenti"
End If
If Lampu = "Kuning" Then
  Aksi = "Bersiap"
End If
If Lampu = "Hijau" Then
  Aksi = "Jalan"
End If
'-------------------------

Dapat dituliskan dengan Select Case, sebagai berikut:

Dim Aksi As String

Select Case Lampu

Case "Merah"
  Aksi = "Berhenti"
Case "Kuning"
  Aksi = "Bersiap"
Case "Hijau"
  Aksi = "Jalan"
End Select
'-------------------------

Atau bisa juga dilakukan pengecekan bertingkat seperti:

Dim Aksi As String

If Lampu = "Merah" Then
  Aksi = "Berhenti"
Else
  If Lampu = "Kuning" Then
   Aksi = "Bersiap"
  Else
   If Lampu = "Hijau" Then
    Aksi = "Jalan"
   Else
    Aksi = "Periksa Ulang"
   End If
  End If
End If
'-------------------------

Dapat dituliskan dengan Select Case Else, sebagai berikut:

Dim Aksi As String

Select Case Lampu

Case "Merah"
  Aksi = "Berhenti"
Case "Kuning"
  Aksi = "Bersiap"
Case "Hijau"
  Aksi = "Jalan"
Case Else
  Aksi = "Periksa Ulang"
End Select
'-------------------------

Dari keempat contoh diatas terlihat jelas bahwa untuk pencabangan dengan opsi banyak instruksi Selesct Case Else jauh lebih efektif dibandingkan dengan If Then Else.

Selanjutnya silahkan anda coba agar dapat langsung mengetahui perbedaan dari kedua instruksi tersebut, selamat mencoba . . . have a nice day :-)


 www.AstroDigi.com (Nino Guevara Ruwano)

Comments :

6 comments to “Instruksi If Then Else dan Select Case Else Pada VBA”

ninest123 07.31
true religion jeans, oakley sunglasses, louboutin, michael kors outlet, michael kors outlet, coach outlet, longchamp outlet, tiffany jewelry, christian louboutin outlet, michael kors, kate spade outlet, prada handbags, ray ban sunglasses, gucci outlet, tiffany and co, replica watches, michael kors outlet, coach outlet, nike air max, polo ralph lauren outlet, louboutin shoes, ray ban sunglasses, tory burch outlet, michael kors outlet, jordan shoes, nike air max, oakley sunglasses, chanel handbags, coach purses, kate spade handbags, nike free, coach factory outlet, burberry, longchamp outlet, burberry outlet online, nike outlet, prada outlet, oakley sunglasses, polo ralph lauren outlet, longchamp, michael kors outlet, louboutin outlet

ninest123 Ninest said...
on 

true religion outlet, mulberry, lacoste pas cher, true religion jeans, nike air max, new balance pas cher, abercrombie and fitch, lululemon, hermes, nike blazer, longchamp pas cher, air force, vans pas cher, michael kors, nike free run uk, north face, sac guess, nike air max, air jordan pas cher, burberry, converse pas cher, north face, louboutin pas cher, nike air max, nike free, vanessa bruno, ralph lauren pas cher, sac longchamp, michael kors, ralph lauren uk, ray ban uk, air max, hollister, oakley pas cher, michael kors, hogan, tn pas cher, hollister pas cher, ray ban pas cher, nike roshe run, true religion jeans, timberland

ninest123 Ninest said...
on 

wedding dresses, soccer jerseys, nike roshe, abercrombie and fitch, iphone 6 cases, ferragamo shoes, reebok shoes, soccer shoes, ghd, asics running shoes, nike huarache, lululemon, vans shoes, instyler, nike roshe, oakley, insanity workout, p90x workout, mcm handbags, north face outlet, beats by dre, celine handbags, bottega veneta, baseball bats, north face outlet, longchamp, giuseppe zanotti, hollister, valentino shoes, new balance, nfl jerseys, hollister, jimmy choo shoes, babyliss, herve leger, mac cosmetics, birkin bag, timberland boots, chi flat iron, nike trainers, mont blanc, nike air max

ninest123 Ninest said...
on 

swarovski, replica watches, toms shoes, ugg boots, supra shoes, marc jacobs, montre pas cher, links of london, thomas sabo, vans, hollister, ugg pas cher, ugg,ugg australia,ugg italia, nike air max, ray ban, ugg boots uk, ugg boots, ugg,uggs,uggs canada, karen millen, gucci, bottes ugg, lancel, uggs on sale, ralph lauren, hollister, converse, wedding dresses, pandora charms, louboutin, juicy couture outlet, converse outlet, juicy couture outlet, pandora charms, swarovski crystal, ugg boots, coach outlet, pandora jewelry, ugg boots
ninest123 07.31

ninest123 Ninest said...
on 

michael jordan shoes
fitflops sale
adidas nmd for sale
michael kors factory outlet
michael kors outlet store
michael kors factory outlet
cheap nfl jerseys
cheap true religion jeans
cheap air jordan
cheap jordan shoes
nike kobe sneakers
http://www.outlettiffanyand.co
michael kors outlet online
air jordans,cheap air jordans,air jordan shoes,air jordan 11,air jordan 13,air jordan 6,air jordan 4
nike huarache
tiffany and co jewelry
michael kors handbags
http://www.michaelkors-outletwebsite.com
ralph lauren online,cheap ralph lauren
retro jordans
tiffany and co jewellery

Chen Jeje said...
on 

cheap nike shoes
cheap oakley sunglasses
salomon shoes
adidas nmd
tiffany and co
los angeles lakers jerseys
san antonio spurs jerseys
green bay packers jerseys
gucci sito ufficiale
oakley sunglasses

Unknown said...
on 

Post a Comment

Related Posts Plugin for WordPress, Blogger...