我这个问题是这样的,我有10个文本框(text1-text10),我要对比每个文本框里面的前4位的内容,但是我有可能不是全部输入,也许就输入6个或8个等,不输入的不比对。
注:文本框里的前四位内容可能出现两种,超过两种提示报错。
    如:text1:ABCD10    TEXT2:ABCD5     TEXT3:ABCD6    TEXT4:ACDE200   这就出现了ABCD 和ACDE两种,超过两种提示。
要定义四个全局变量,Y,N和M,K  把TEXT1-TEXT10里前四位出现的情况分别赋值给Y和N  如果就一种情况就赋值给Y就可以了,N为空,然后在把TEXT1-TEXT10前四位后面出现的数字相加赋值给M和K.前四位不同的要分别相加。
如:我输入的四个为:text1:ABCD10    TEXT2:ABCD5     TEXT3:ABCD6    TEXT4:ACDE200
    那么:M=10+5+6    K=200
注 :我有可能输入的不是四个,可能是1-10个

解决方案 »

  1.   

    简单的办法,可使用一个Collection来实现。
    key是前4位,值是数字相加的和。
    检查key是否有,如果有,拿出值来相加。
    如果没有,将key和值放入Collection中。
      

  2.   

    dim aText(), aHeader(1) as string, aValue() as long
    dim sHeader as string, i as long, j as long
    aText = Array(text1.text, text2.text, text3.text, _
                  text4.text, text5.text, text6.text, _
                  text7.text, text8.text, text9.text, _
                  text10.text,
    for i=0 to ubound(aText)
        if len(aText(i)) > 4 then
            sHeader = Left(aText(i), 4)
            for j = 0 to 1
                if LenB(aHeader(j)) = 0 Then 
                    aHeader(j) = sHeader
                    Exit for
                end if
                if aHeader(j) = sHeader then exit for
            next
            if j>1 then
                msgbox "超过两种"
                exit sub
            end if
            aValue = aValue + CLng(Mid(aText(i), 5))
        end if
    nextY = aHeader(0)
    N = aHeader(1)
    M = aValue(0)
    K = aValue(1)[/code]
      

  3.   

    Private Declare Function SendMessagebyString Lib "user32" Alias "SendMessageA" (ByVal hWND As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As LongPrivate Const LB_FINDSTRINGEXACT = &H1A2Dim x As Comtrol, strTmp As String, n As LongList1.Clear
    For Each x In Me.Controls
        If Typeof x Is TextBox Then
            If Len(x.Text) > 4 Then
                strTmp = Left(x.Text)
                n = SendMessagebyString(List1.hWnd, LB_FINDSTRINGEXACT, -1, strTmp)
                If n = -1 Then 
                     List1.AddItem strTmp
                     List1.ItemData(List1.NewIndex) = Val(Replace(x.Text, strTmp, ""))
                Else
                     List1.ItemData(n) = List1.ItemData(n) + Val(Replace(x.Text, strTmp, ""))
                End If
            End If
        End If
    Next xIf List1.ListCount > 2 Then 
        MsgBox "Too many pattern!"
    Else
        If List1.ListCount Then 
            Y = List1.List(0)
            M = Liist1.ItemData(0)
        End If    If List1.ListCount = 2 Then 
            N = List1.List(1)
            K = Liist1.ItemData(1)
        End If
    Ebd If