当前位置:编程学习 > VB >>

求高手帮忙,if then else语句

问题点:当spdData.CellText(ecCustomerOption, Row)为JP或者FO或者TO
并且spdData.CellText(ecOption, Row)为BF1或者BF8或者BF9或者BFB
那么spdData.CellText(ecCheck1, Row)显示NG,否则OK
我写的代码如下:
     If (spdData.CellText(ecCustomerOption, Row) = "JP" Or spdData.CellText(ecCustomerOption, Row) = "F0" Or spdData.CellText(ecCustomerOption, Row) = "T0") And spdData.CellText(ecOption, Row) = "BF1" Or spdData.CellText(ecOption, Row) = "BF8" Or spdData.CellText(ecOption, Row) = "BF9" Or spdData.CellText(ecOption, Row) = "BFB"  Then

      spdData.CellText(ecCheck1, Row) = "NG"
      Else: spdData.CellText(ecCheck1, Row) = "OK"

   End If
但测试显示,如果(spdData.CellText(ecCustomerOption, Row)=“LN” 并且spdData.CellText(ecOption, Row) = "BF9"的话,spdData.CellText(ecCheck1, Row)也显示NG
请高手指出问题点,多谢! --------------------编程问答-------------------- 我自己已经找到问题点了,O(∩_∩)O哈哈哈~ --------------------编程问答-------------------- 解决了就好。 --------------------编程问答-------------------- If (spdData.CellText(ecCustomerOption, Row) = "JP" Or spdData.CellText(ecCustomerOption, Row) = "F0" Or spdData.CellText(ecCustomerOption, Row) = "T0") And (spdData.CellText(ecOption, Row) = "BF1" Or spdData.CellText(ecOption, Row) = "BF8" Or spdData.CellText(ecOption, Row) = "BF9" Or spdData.CellText(ecOption, Row) = "BFB") Then

--------------------编程问答-------------------- 纯接分了 --------------------编程问答-------------------- 写你这种程序需要if then吗?



Option Explicit

Private Sub Form_Load()
  Dim tCOPt As String
  Dim tOPt As String
  'tCOPt = spdData.CellText(ecCustomerOption, Row)
  'tOPt = spdData.CellText(ecOption, Row)
  tCOPt = "JP": tOPt = "BF8"
  Text1.Text = Split("OK,NG", ",")((StringWordsCheck(tCOPt, "JP,FO,TO") And StringWordsCheck(tOPt, "BF1,BF8,BF9,BFB")) And 1)
End Sub

Function StringWordsCheck(ByVal pString As String, ByVal pWords As String) As Boolean
  ’如果pString等于pWords列表中的一项,则返回True。
  Dim tOutBool As Boolean
  Dim tWordList() As String
  Dim tWordList_Length As Long
  Dim tWordList_Index As Long
  tWordList() = Split(pWords, ",")
  tWordList_Length = UBound(tWordList())
  For tWordList_Index = 0 To tWordList_Length
    tOutBool = tOutBool Or (pString = tWordList(tWordList_Index))
  Next
  StringWordsCheck = tOutBool
End Function
--------------------编程问答-------------------- 刚刚检查了一下代码,发现还有更简单的代码……为什么我这么笨呢?


Private Sub Form_Load()
  Dim tCOPt As String
  Dim tOPt As String
  'tCOPt ="#" & spdData.CellText(ecCustomerOption, Row)
  'tOPt = "#" & spdData.CellText(ecOption, Row)
  tCOPt = "#JP": tOPt = "#BF8"  
  Text1.Text = Split("OK,NG", ",")((CBool(InStr("#JP#FO#TO", tCOPt)) And CBool(InStr("#BF1#BF8#BF9#BFB", tOPt))) And 1)
End Sub
--------------------编程问答-------------------- spdData.CellText(ecCustomerOption, Row)为JP或者FO或者TO,则A为真,否则为假:


CBool(InStr("#JP#FO#TO", "#" & spdData.CellText(ecCustomerOption, Row))


spdData.CellText(ecOption, Row)为BF1或者BF8或者BF9或者BFB,则B为真,否则为假:

CBool(InStr("#BF1#BF8#BF9#BFB", "#" & spdData.CellText(ecOption, Row))


若A和B同时为真,则C为真,否则为假:

C = A And B


如C为真,显示NG,否则显示OK

Split("OK,NG", ",")(C And 1)


综上所述,只需要一行代码:

tOutString = Split("OK,NG", ",")((CBool(InStr("#JP#FO#TO", "#" & spdData.CellText(ecCustomerOption, Row))) And CBool(InStr("#BF1#BF8#BF9#BFB", "#" & spdData.CellText(ecOption, Row)))) And 1)

--------------------编程问答-------------------- 哪有写那么长的? --------------------编程问答-------------------- 看7楼代码=做脑保健操 --------------------编程问答-------------------- 有时思路比技术更重要
补充:VB ,  基础类
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,