数组A(221,0,211,0,201,203,0,206,0,0,0,0,264,249,0,0,221) 
我要找出0出现的时候,它的前面的一条记录,和后面的一条记录 
比如第1个0出现的时候,我要找到221和211 
第2个0出现的时候,我要找到211和201 
第3个0出现的时候,我要找到203和206 
第4个0出现的时候,我要找到206和264 
第5个0出现的时候,我要找到206和264 
第6个0出现的时候,我要找到206和264 
第7个0出现的时候,我要找到206和264 
第8个0出现的时候,我要找到249和221 
第9个0出现的时候,我要找到249和221 这个判断该怎么做?

解决方案 »

  1.   

     容易:
    Private Sub Command1_Click()
    Dim A() As Variant, i As Integer, m As Integer, n As Integer, p As IntegerA = Array(221, 0, 211, 0, 201, 203, 0, 206, 0, 0, 0, 0, 264, 249, 0, 0, 221)
    For i = LBound(A) To UBound(A)
        If A(i) = 0 Then
            m = i - 1
            Do While (A(m) = 0 And m > LBound(A))
                m = m - 1
                If A(m) Then Exit Do
            Loop
            n = i + 1
            Do While (A(n) = 0 And n < UBound(A))
                n = n + 1
                If A(n) Then Exit Do
            Loop
            p = p + 1
            Debug.Print p, A(m), A(n)
        End If
    Next i
    End Sub 1             221           211 
     2             211           201 
     3             203           206 
     4             206           264 
     5             206           264 
     6             206           264 
     7             206           264 
     8             249           221 
     9             249           221 
      

  2.   

    谢谢啊,能给我解释一下吗?是怎么做的?If A(m) Then Exit Do 这句是什么意思?
      

  3.   

        Dim A, Tmp
        Dim i As Long, x As Long
        
        A = Array(221, 0, 211, 0, 201, 203, 0, 206, 0, 0, 0, 0, 264, 249, 0, 0, 221)
        ReDim Tmp(x)
        For i = 0 To UBound(A)
            If A(i) <> 0 Then
                ReDim Preserve Tmp(x)
                Tmp(x) = i
                x = x + 1
            End If
        Next
        x = 1
        For i = 0 To UBound(A)
            If A(i) = 0 Then
                If Tmp(x) < i Then
                    x = x + 1
                End If
                Debug.Print i & "列 "; A(Tmp(x - 1)); " "; A(Tmp(x))
            End If
        Next
    别忘了,解决问题后即时结贴
      

  4.   

    稍稍改下:
        Dim A, Tmp
        Dim i As Long, x As Long
        
        A = Array(221, 0, 211, 0, 201, 203, 0, 206, 0, 0, 0, 0, 264, 249, 0, 0, 221)
        ReDim Tmp(x)
        For i = 0 To UBound(A)
            If A(i) <> 0 Then
                ReDim Preserve Tmp(x)
                Tmp(x) = i
                x = x + 1
            End If
        Next
        x = 1
        For i = 0 To UBound(A)
            If A(i) = 0 Then
                While Tmp(x) < i
                    x = x + 1
                Wend
                Debug.Print i & ": "; A(Tmp(x - 1)); " "; A(Tmp(x))
            End If
        Next
      

  5.   

    For i = LBound(A) To UBound(A) 
        If A(i) = 0 Then                             '找到一个 0 元素
            m = i - 1 
            Do While (A(m) = 0 And m >  LBound(A))   '上一个元素如果是 0
                m = m - 1                            '继续向上找
                If A(m) Then Exit Do                 '如果非 0 则停止
            Loop 
            n = i + 1 
            Do While (A(n) = 0 And n  < UBound(A))   '下一个元素如果是 0 
                n = n + 1                            '继续向下找 
                If A(n) Then Exit Do                 '如果非 0 则停止 
            Loop 
            p = p + 1                                '这是找到的序号
            Debug.Print p, A(m), A(n) 
        End If 
    Next i 
    End Sub
      

  6.   

    还有就是能否得到的结果是第1列的P,能不能改成是0所在的位置?就是0所在的是第几条记录?更简单了:
    Private Sub Command1_Click() 
    Dim A() As Variant, i As Integer, m As Integer, n As IntegerA = Array(221, 0, 211, 0, 201, 203, 0, 206, 0, 0, 0, 0, 264, 249, 0, 0, 221) 
    For i = LBound(A) To UBound(A) 
        If A(i) = 0 Then 
            m = i - 1 
            Do While (A(m) = 0 And m >  LBound(A)) 
                m = m - 1 
                If A(m) Then Exit Do 
            Loop 
            n = i + 1 
            Do While (A(n) = 0 And n  < UBound(A)) 
                n = n + 1 
                If A(n) Then Exit Do 
            Loop 
            Debug.Print i + 1, A(m), A(n) 
        End If 
    Next i 
    End Sub 
      

  7.   

    Dim Tmpstr%(), i%, j%, k%, S
    Private Sub Form_Load()
       S = Array(221, 0, 211, 0, 201, 203, 0, 206, 0, 0, 0, 0, 264, 249, 0, 0, 221)
       ReDim Preserve Tmpstr(UBound(S))
       For i = 0 To UBound(S)
          If S(i) = 0 Then Tmpstr(j) = i: j = j + 1
       Next i
       For i = 0 To j - 1
          aa = "第 " & CStr(i + 1) & " 个 0 的前后数字是: "
          For k = Tmpstr(i) To 0 Step -1
             If S(k - 1) <> 0 Then aa = aa & S(k - 1): Exit For
          Next k
          For k = Tmpstr(i) To UBound(S)
             If S(k + 1) <> 0 Then aa = aa & " 和 " & S(k + 1): Exit For
          Next k
          Print aa
       Next i
    End Sub