Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
12
返回列表 发新帖
楼主: 爱疯

[已解决]从A到F,不能走重复路线,要求输出所有的走法

[复制链接]
 楼主| 发表于 2016-6-12 15:59 | 显示全部楼层
本帖最后由 爱疯 于 2016-6-12 16:34 编辑
香川群子 发表于 2016-6-12 14:23
递归其中,检查子节点是否已被利用过,这个很关键。 If InStr(s, t2) = 0

'http://www.excelpx.com/thread-418810-1-1.html 10楼
Dim dic, k&, destination
Sub test()    'by kagawa 2016/6/12
    Dim ar, i&, startPoint
    startPoint = [a2]
    destination = [b2]
    [d1].CurrentRegion.Offset(1) = ""
    ar = [a4].CurrentRegion
    Set dic = CreateObject("Scripting.Dictionary")
    For i = 2 To UBound(ar)
        dic(ar(i, 1)) = dic(ar(i, 1)) & "," & ar(i, 2)
    Next
    k = 1: Call dg(startPoint, startPoint)
    MsgBox k - 1
End Sub

Sub dg(path, startPoint)
    Dim endPoints, endPoint$, i&
    '获取起点的所有终点
    endPoints = Split(dic(startPoint), ",")
    '遍历所有终点
    For i = 1 To UBound(endPoints)
        '读取其中的某个终点(endPoints里面的终点也可理解为方向)
        endPoint = endPoints(i)
        '是不是指定终点
        If endPoint = destination Then
            k = k + 1: Cells(k, 4) = path & endPoint
            Exit Sub
        Else
            '因为要获取所有路径,所以递归在for语句内部
            If InStr(path, endPoint) = 0 Then
                '如果路径不包含终点,就递归
                Call dg(path & endPoint, endPoint)
            Else
                '包含,就返回
                'Stop
            End If
        End If
    Next
End Sub





谢谢群主老师!
开始没发现,10楼已经有了注释,自己就闷着去读了半天。。。。{:201:}

要改下变量名,自己才看的过来{:021:}


excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
回复

使用道具 举报

发表于 2016-6-12 16:17 | 显示全部楼层
爱疯 发表于 2016-6-12 15:59
Dim dic, k&, destination

Sub test()    'by kagawa 2016/6/12

在哪输出?
回复

使用道具 举报

 楼主| 发表于 2016-6-12 16:24 | 显示全部楼层
张雄友 发表于 2016-6-12 16:17
在哪输出?

QQ截图20160612161412.jpg


见10楼附件,D2往下的区域是输出区域
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-5-7 18:11 , Processed in 0.117893 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表