VBA使有向图无向

VBA使有向图无向,vba,excel,Vba,Excel,我有一个excel电子表格,有两列,说明了两个顶点(服务器)之间的关系。不幸的是,有太多的列,所以有条目A,B和B,A。我需要去掉那些多余的条目。有人能提供一个功能吗 例如: 希望输出是分开的,但是如果它修改相同的输入列就可以了 通过回答这个问题,我希望它能为您提供一个起点或想法,让您了解基础知识,并为下一次提出半途而废的解决方案 无VBA时: 您可以比较字符串。在单元格D2中->=IF(A2=IF(A2 Sub removeDub() Dim dict As Object Set dict

我有一个excel电子表格,有两列,说明了两个顶点(服务器)之间的关系。不幸的是,有太多的列,所以有条目A,B和B,A。我需要去掉那些多余的条目。有人能提供一个功能吗

例如:


希望输出是分开的,但是如果它修改相同的输入列就可以了

通过回答这个问题,我希望它能为您提供一个起点或想法,让您了解基础知识,并为下一次提出半途而废的解决方案

无VBA时:

您可以比较字符串。在单元格D2中->
=IF(A2
=IF(A2
Sub removeDub()
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
Dim i As Long
Dim s1, s2 As String
'Dim a() As String

With Sheets("Sheet1")
For i = 2 To .Range("A1").End(xlDown).Row
    If Cells(i, 1).Value < Cells(i, 2).Value Then
        s1 = .Cells(i, 1).Value
        s2 = .Cells(i, 2).Value
    Else
        s1 = .Cells(i, 2).Value
        s2 = .Cells(i, 1).Value
    End If

    If Not dict.Exists(s1 & "," & s2) Then
        dict.Add s1 & "," & s2, 1
        .Range("D" & .Cells.Rows.Count).End(xlUp).Offset(1) = s1
        .Range("E" & .Cells.Rows.Count).End(xlUp).Offset(1) = s2
    End If
Next i

'For Each Key In dict.Keys
'    a = Split(Key, ",")
'    .Range("D" & .Cells.Rows.Count).End(xlUp).Offset(1) = a(0)
'    .Range("E" & .Cells.Rows.Count).End(xlUp).Offset(1) = a(1)
'Next Key
End With

End Sub