Board logo

標題: [發問] 如何將多個IF化繁為簡? [打印本頁]

作者: av8d    時間: 2014-5-13 10:19     標題: 如何將多個IF化繁為簡?

  1. If [B5] = 1 Then Look1
  2.     If [B5] = 2 Then Look2
  3.     If [B5] = 3 Then Look3
  4.     If [B5] = 4 Then Look4
  5.     If [B5] = 5 Then Look5
  6.     If [B5] = 6 Then Look6
  7.     If [B5] = 7 Then Look7
  8.     If [B5] = 8 Then Look8
  9.     If [B5] = 9 Then Look9
  10.     If [B5] = 10 Then Look10
  11.     If [B5] = 11 Then Look11
  12.     If [B5] = 12 Then Look12
  13.     If [B5] = 13 Then Look13
  14.     If [B5] = 14 Then Look14
  15.     If [B5] = 15 Then Look15
  16.     If [B5] = 16 Then Look16
複製代碼
再次感謝!
作者: Scott090    時間: 2014-5-13 12:31

回復 1# av8d

這個符合需求?
  1. Sub chooseVar()
  2.     look1 = 1
  3.     look2 = 2
  4.     look3 = 3
  5.     aa = Choose([B5], look1, look2, look3)
  6.     bb = Switch([B5] = 1, look1, [B5] = 2, look2, [B5] = 3, look3)
  7. End Sub
複製代碼

作者: yen956    時間: 2014-5-13 12:34

回復 1# av8d
'請將下列放在Sheet1 , 不要放 Module1
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim I, j As Integer, Rng As Range
    If Not Intersect(Target, [B5]) Is Nothing Then
        If Application.IsNumber([B5]) Then
            Application.Run "Book1!look" & [B5] & ""
        End If
    End If
End Sub

'請將下列放在 Module1, 不要放 Sheet1
Sub look1()
   MsgBox "look1"
End Sub
Sub look2()
   MsgBox "look2"
End Sub
Sub look3()
   MsgBox "look3"
End Sub
Sub look4()
   MsgBox "look4"
End Sub
Sub look5()
   MsgBox "look5"
End Sub

'然後試 Sheet1 的 [B5]
作者: Scott090    時間: 2014-5-13 13:27

本帖最後由 Scott090 於 2014-5-13 13:33 編輯

回復 1# av8d


    或者:
  1. On [B5] GoTo look1, look2, look3
  2.     On [B5] GoSub look1, look2, look3
複製代碼





歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)