以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  [求助]遍历整列,计算每行数字与最近的相同数字间的距离  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=106785)

--  作者:Devil
--  发布时间:2017/9/15 14:59:00
--  [求助]遍历整列,计算每行数字与最近的相同数字间的距离

求助大佬

遍历整列数字,计算每一行数字与最近的相同数字间的距离,填入下一列


--  作者:有点甜
--  发布时间:2017/9/15 15:15:00
--  
Dim t As Table = Tables("表a")
For i As Integer = 0 To t.rows.count-1
    For j As Integer = i+1 To t.rows.count-1
        If t.rows(i)("第一列") = t.rows(j)("第一列") Then
            t.rows(i)("第二列") = j-i
        End If
    Next
Next

--  作者:Devil
--  发布时间:2017/9/19 15:24:00
--  
怎么写能同时向上向下遍历计算最短距离啊大佬,我这个写出来不对
Dim t As Table = Tables(“表a")
Dim n As Integer
Dim m As Integer
Dim i As Integer
Dim j As Integer
For i = 0 To t.rows.count-1
    For j  = i+1 To t.rows.count-1
        If t.rows(i)("第一列") = t.rows(j)("第一列") Then
            m = j-i-1
        End If
    Next
    For a As Integer = i-1 To 0
        If t.rows(a)("第一列") = t.rows(i)("第一列") Then
            n= i-a
        End If
    Next
    If m>n Then
        t.Rows(i)("第二列") = m
    Else
        t.Rows(i)("第二列") = n
    End If
Next

--  作者:有点甜
--  发布时间:2017/9/19 15:40:00
--  

Dim t As Table = Tables("表a")

For i As Integer = 0 To t.rows.count-1
    Dim down As Integer = 0
    Dim up As Integer = 0
    For j As Integer = i+1 To t.rows.count-1
        If t.rows(i)("第一列") = t.rows(j)("第一列") Then
            down = j-i-1
            Exit For
        End If
    Next
    For j As Integer = i-1 To 0 Step -1
        If t.rows(i)("第一列") = t.rows(j)("第一列") Then
            up = i-j-1
            Exit For
        End If
    Next
    t.rows(i)("第二列") = iif(down>up, down, iif(up=0, Nothing, up))
Next