gpt4 book ai didi

excel - 使用VBA代码在excel中查找循环链接

转载 作者:行者123 更新时间:2023-12-04 22:24:53 25 4
gpt4 key购买 nike

我有一张如下表。这表示 Name 中的值之间的链接(或边)。柱子。

+-------+-------------------+
| Name | from |
+-------+-------------------+
| G | X; Y; HG; WP |
| X | U |
| Y | |
| U | V |
| V | |
| K | M; N |
| M | |
| N | |
| G1 | G |
| G2 | G1 |
| G3 | G2 |
| G4a | G3 |
| J | G4a |
| G4b | G3 |
| G5b | G4b |
| H | G5b |
| R | H; J |
| R1 | R |
| R2 | R1 |
| O | R2 |
| O1 | O |
| O2 | O1 |
| O3 | O2 |
| F | H; K; TR |
| P | G; Z |
| Z | |
| HG | VB; NH |
| WP | |
| TR | Z |
| VB | ICH; OL; NZ; LO |
| NH | |
| ICH | NZ |
| NZ | |
| LO | |
| OL | TZ; HG |
| TZ | |
| BN | WD; PO |
| WD | RZ; UX |
| PO | QA; IU; BV; MM; BN|
| RZ | |
| UX | |
| IU | |
| QA | |
| BV | |
| MM | |
+-------+-------------------+

我想看看 Name 中的值是否如果我们在 from 中顺序查找值,列作为前辈存在柱子。换句话说,我想看看是否有任何循环链接。

如何使用 VBA 执行此操作excel中的代码?

在此示例中 HG, VB, OL, BN, PO有循环链接。我只想突出显示“名称”列中的那些单元格。

一种方法是在单独的列中查找每一行的所有前辈,如下所示。
+-------+--------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------+
| GName | from | predecessors |
+-------+--------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------+
| G | X; Y; HG; WP | X; U; V; Y; HG; VB; NH; ICH; NZ; LO; OL; TZ; WP |
| X | U | U; V |
| Y | | |
| U | V | V |
| V | | |
| K | M; N | M; N |
| M | | |
| N | | |
| G1 | G | G; X; Y; HG; WP; U; VB; NH; V; ICH; NZ; LO; OL; TZ |
| G2 | G1 | G1; G; X; Y; HG; WP; U; VB; NH; V; ICH; NZ; LO; OL; TZ |
| G3 | G2 | G2; G1; G; X; Y; HG; WP; U; VB; NH; V; ICH; NZ; LO; OL; TZ |
| G4a | G3 | G3; G2; G1; G; X; Y; HG; WP; U; VB; NH; V; ICH; NZ; LO; OL; TZ |
| J | G4a | G4a; G3; G2; G1; G; X; Y; HG; WP; U; VB; NH; V; ICH; NZ; LO; OL; TZ |
| G4b | G3 | G3; G2; G1; G; X; Y; HG; WP; U; VB; NH; V; ICH; NZ; LO; OL; TZ |
| G5b | G4b | G4b; G3; G2; G1; G; X; Y; HG; WP; U; VB; NH; V; ICH; NZ; LO; OL; TZ |
| H | G5b | G5b; G4b; G3; G2; G1; G; X; Y; HG; WP; U; VB; NH; V; ICH; NZ; LO; OL; TZ |
| R | H; J | J; G4a; G3; G2; G1; G; X; Y; HG; WP; U; VB; NH; V; ICH; NZ; LO; OL; TZ; H; G5b; G4b; G3; G2; G1; G; X; Y; HG; WP; U; VB; NH; V; ICH; NZ; LO; OL; TZ |
| R1 | R | R; J; H; G4a; G5b; G3; G4b; G2; G1; G; X; Y; HG; WP; U; VB; NH; V; ICH; NZ; LO; OL; TZ |
| R2 | R1 | R1; R; J; H; G4a; G5b; G3; G4b; G2; G1; G; X; Y; HG; WP; U; VB; NH; V; ICH; NZ; LO; OL; TZ |
| O | R2 | R2; R1; R; J; H; G4a; G5b; G3; G4b; G2; G1; G; X; Y; HG; WP; U; VB; NH; V; ICH; NZ; LO; OL; TZ |
| O1 | O | O; R2; R1; R; J; H; G4a; G5b; G3; G4b; G2; G1; G; X; Y; HG; WP; U; VB; NH; V; ICH; NZ; LO; OL; TZ |
| O2 | O1 | O1; O; R2; R1; R; J; H; G4a; G5b; G3; G4b; G2; G1; G; X; Y; HG; WP; U; VB; NH; V; ICH; NZ; LO; OL; TZ |
| O3 | O2 | O2; O1; O; R2; R1; R; J; H; G4a; G5b; G3; G4b; G2; G1; G; X; Y; HG; WP; U; VB; NH; V; ICH; NZ; LO; OL; TZ |
| F | H; KTR | K; M; N; H; G5b; G4b; G3; G2; G1; G; X; Y; HG; WP; U; VB; NH; V; ICH; NZ; LO; OL; TZ; TR; Z |
| P | G; Z | G; X; Y; HG; WP; U; VB; NH; V; ICH; NZ; LO; OL; TZ; Z |
| Z | | |
| HG | VB; NH | VB; ICH; NZ; LO; OL; HG; TZ; NH; NH |
| WP | | |
| TR | Z | Z |
| VB | ICH; OL; NZ; LO | ICH; NZ; NZ; LO; OL; HG; TZ; VB; NH; ICH; NZ; LO |
| NH | | |
| ICH | NZ | NZ |
| NZ | | |
| LO | | |
| OL | TZ; HG | HG; VB; NH; ICH; NZ; LO; OL; TZ; TZ |
| TZ | | |
| BN | WD; PO | WD; RZ; UX; PO; BN; IU; QA; BV; MM; WD; RZ; UX |
| WD | RZ; UX | RZ; UX |
| PO | QA; IU; BV; MM; BN | BN; WD; PO; RZ; UX; IU; QA; BV; MM; IU; QA; BV; MM |
| RZ | | |
| UX | | |
| IU | | |
| QA | | |
| BV | | |
| MM | | |
+-------+--------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------+

现在搜索 Name 中的值 predecessors 中相应单元格中的列列将给出所需的结果。

如何使用 VBA 实现此目的excel中的代码?

编辑 据我了解,这是一个网络问题。为了更清楚,这里是网络图。

enter image description here

编辑2 这是我在 建议的方向上的尝试(这里是 VBA 菜鸟) @GSerg .卡在获取前辈的位置。获取 Argument not optional错误。
Public Function NetworkCyclicityCheck(node As String, col As Range) As String

Dim dicP As Object: Set dicP = CreateObject("Scripting.Dictionary")

If Trim(node) <> "" Then
For Each x In Split(cl.Value2, ";")
Key = Trim(x)
With Range
pcell = .Find(What:=Key, LookAt:=xlWhole, MatchCase:=False)
dicP(pcell) = dicP(pcell) & "," & cl.Address(0, 0)
Next x
End If

Set NetworkCyclicityCheck = dicP
Set dicP = Nothing

End Function

最佳答案

这对某些人来说可能看起来很奇怪,但这个问题真的让我很感兴趣,因为我以前从未遇到过需要编写递归代码的情况。

不确定它的效率如何,但它在我有限的测试中立即起作用。将其放入一个新模块中,它应该可以正常运行。

Option Explicit

Private DestinationByLocation As Object

Sub Test()

HighlightTheCycles Sheets("Sheet1").Range("A2:B46"), , True, True

End Sub

Public Sub HighlightTheCycles(ByVal TableRange As Range, Optional ClearExistingFill As Boolean = True, Optional OutputTheLoopPath As Boolean = False, Optional AlertIfNoCycles As Boolean = False)

Dim InputData As Variant, OutputRange As Range
Dim x As Long, y As Long, Temp As Variant

Dim LoopLocations As Object
Set DestinationByLocation = CreateObject("Scripting.Dictionary")
Set LoopLocations = CreateObject("Scripting.Dictionary")
DestinationByLocation.CompareMode = 1
LoopLocations.CompareMode = 1

'Set fill to 'no-fill'
If ClearExistingFill Then
If OutputTheLoopPath Then
TableRange.Resize(TableRange.Rows.Count, 3).Interior.Pattern = xlNone
Else
TableRange.Interior.Pattern = xlNone
End If
End If

'Establish all possible destinations
InputData = TableRange.Value
For x = 1 To UBound(InputData, 1)
If Len(InputData(x, 2)) > 0 Then
Temp = Split(InputData(x, 2), ";")
For y = 0 To UBound(Temp, 1)
Temp(y) = Trim(Temp(y))
If DestinationByLocation.Exists(Temp(y)) Then
DestinationByLocation(Temp(y)) = DestinationByLocation(Temp(y)) & ";" & InputData(x, 1)
Else
DestinationByLocation.Add Temp(y), InputData(x, 1)
End If
Next y
End If
Next x

'Look for loops
Dim TempPath As Variant
Temp = DestinationByLocation.Keys
For x = 1 To UBound(Temp, 1)
TempPath = TakeATrip(Temp(x))
If Right(TempPath, 3) = ";;;" Then
TempPath = Split(Left(TempPath, Len(TempPath) - 3), ";")
If TempPath(UBound(TempPath, 1)) = Temp(x) Then
LoopLocations.Add Temp(x), TempPath
End If
End If
Next x

'Mark the cells that result in a loop
If LoopLocations.Count > 0 Then
If OutputTheLoopPath Then

'Output the loop path found in a third column
ReDim Temp(1 To UBound(InputData, 1), 1 To 1)
For x = 1 To UBound(InputData, 1)
If LoopLocations.Exists(InputData(x, 1)) Then
If OutputRange Is Nothing Then
Set OutputRange = TableRange.Cells(x, 1).Resize(1, 3)
Else
Set OutputRange = Union(OutputRange, TableRange.Cells(x, 1).Resize(1, 3))
End If
Temp(x, 1) = Join(LoopLocations(InputData(x, 1)), ";")
Temp(x, 1) = Replace(Right(Temp(x, 1), Len(Temp(x, 1)) - 1), ";", " ;")
End If
Next x
TableRange.Cells(1, 1).Offset(0, 2).Resize(UBound(Temp, 1), 1).Value = Temp

Else

'Do not output a third column
For x = 1 To UBound(InputData, 1)
If LoopLocations.Exists(InputData(x, 1)) Then
If OutputRange Is Nothing Then
Set OutputRange = TableRange.Cells(x, 1).Resize(1, 2)
Else
Set OutputRange = Union(OutputRange, TableRange.Cells(x, 1).Resize(1, 2))
End If
End If
Next x

End If

OutputRange.Interior.Color = RGB(255, 0, 0)

ElseIf AlertIfNoCycles Then

MsgBox _
"No cycles found in " & TableRange.Address(0, 0, , True), _
vbInformation

End If

End Sub

Private Function TakeATrip(ByVal MyLocation As Variant, Optional ExistingPath As Variant = "") As Variant

Dim MyJourneys As Variant, x As Long
Dim MyPaths As Variant

'If no new destinations, record the trip
If DestinationByLocation.Exists(MyLocation) Then
If DestinationByLocation(MyLocation) = "" Then
TakeATrip = ExistingPath & ";" & MyLocation
Exit Function
End If
Else
TakeATrip = ExistingPath & ";" & MyLocation
Exit Function
End If

'Prepare for multiple routes
MyJourneys = Split(DestinationByLocation(MyLocation), ";")
ReDim MyPaths(0 To UBound(MyJourneys))

For x = 0 To UBound(MyJourneys)
If Len(MyJourneys(x)) > 0 Then
If ExistingPath & ";" & MyLocation Like "*;" & MyJourneys(x) & ";*" Then
TakeATrip = ExistingPath & ";" & MyLocation & ";" & MyJourneys(x) & ";;;"
Exit Function 'a loop is found, we're done
End If
'explore new found path
MyPaths(x) = TakeATrip(MyJourneys(x), ExistingPath & ";" & MyLocation)
End If
If Right(MyPaths(x), 3) = ";;;" Then
TakeATrip = MyPaths(x)
Exit Function
End If
Next x

TakeATrip = Join(MyPaths, "|")

End Function

请注意 字典默认区分大小写。如果您需要此代码忽略大小写,请添加到顶部的声明中:
option compare text

并改变这个
  Set DestinationByLocation = CreateObject("Scripting.Dictionary")
Set LoopLocations = CreateObject("Scripting.Dictionary")

对此
  Set DestinationByLocation = CreateObject("Scripting.Dictionary")
Set LoopLocations = CreateObject("Scripting.Dictionary")
DestinationByLocation.CompareMode = 1
LoopLocations.CompareMode = 1

感谢 Crops 在未找到循环时识别错误;我更正了代码以包括检查无循环和可选的消息框输出。

关于excel - 使用VBA代码在excel中查找循环链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58820806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com