将行中的数据与子数据对齐

时间:2019-06-25 03:31:33

标签: excel vba

我有两列数据,这是两个不同型号的交换机的交换机配置。我想根据接口名称对齐数据并插入空格以进行匹配。我已经看到了很多对齐行的示例,但以下示例中的后续数据都不需要被专门对齐。

旧:

int g1/0/1        int g1/0/1
desc new text     desc old text
…                 …
…                 …
…                 …
…   
…                 int g1/0/2
                  desc old text2
int g1/0/2        …
desc new text2    …
…                 …
…   
…                 int g1/0/3
…                 desc old text3
…                 …
                  …
int g1/0/3        …
desc new text3  
…   
…   
…   
…   
…   

新:

int g1/0/1      int g1/0/1
desc new text   desc old text
…               …
…               …
…               …
…   
…

int g1/0/2      int g1/0/2
desc new text2  desc old text2
…               …
…               …
…               …
…   
…               

int g1/0/3      int g1/0/3
desc new text3  desc old text3
…               …
…               …
…               …
…   
…

更新:

这是我根据以下建议想到的代码。

    Sub Interface_Align()

Dim ws As Worksheet, Xstr As String, Xstr2 As String
Set ws = ActiveWorkbook.ActiveSheet
Dim Rw As Long, Rw2 As Long, SRow As Long, ERow As Long, i As Long, FCol As Long, SCol As Long
Dim Rng1 As Range, Rng2 As Range, C As Range, D As Range


SRow = 1
ERow = 1100
FCol = ActiveCell.Column
SCol = FCol + 1

Set Rng1 = ws.Range(Cells(SRow, FCol), Cells(ERow, FCol))
Set Rng2 = ws.Range(Cells(SRow, SCol), Cells(ERow, SCol))

    For Rw = SRow To ERow

        Xstr = Rng1.Cells(Rw, 1).Text
        If Left(Xstr, 25) = "interface GigabitEthernet" Then

            Set C = Rng2.Find(Xstr)

            If Not C Is Nothing Then
                If C.Row < Rw Then
                    For i = 1 To Rw - C.Row
                        C.Insert xlShiftDown
                    Next
                ElseIf C.Row > Rw Then
                    For i = 1 To C.Row - Rw
                        C.Offset(-1, 0).Delete xlShiftUp
                    Next
                End If

            End If
            For Rw2 = (C.Row + 1) To ERow

                Xstr2 = Rng2.Cells(Rw2, 1).Text

                If Left(Xstr2, 25) = "interface GigabitEthernet" Then

                    Set D = Rng1.Find(Xstr2)

                    If Not D Is Nothing Then

                        If Rw2 > D.Row Then
                            For i = 1 To (Rw2 - D.Row)
                                D.Insert xlShiftDown
                            Next
                            Exit For
                        End If

                    End If

                End If

            Next

        End If

    Next


End Sub

1 个答案:

答案 0 :(得分:0)

可以尝试这样的事情 在库仑A和B中假定数据。在重要数据之间的单元格之间假定为空白(或3个点-如图所示)。根据您的要求修改行列范围等

Sub test()
Dim ws As Worksheet, Xstr As String
Set ws = ThisWorkbook.ActiveSheet
Dim Rw As Long, SRow As Long, ERow As Long, i As Long
Dim Rng As Range, C As Range

SRow = 1
ERow = 100
Set Rng = ws.Range(Cells(SRow, 2), Cells(ERow, 2))

    For Rw = SRow To ERow
    Xstr = ws.Cells(Rw, 1).Value
        If Xstr <> "" And Xstr <> "..." Then
        Set C = Rng.Find(Xstr)
            If Not C Is Nothing Then
                If C.Row < Rw Then
                    For i = 1 To Rw - C.Row
                    C.Insert xlShiftDown
                    Next
                ElseIf C.Row > Rw Then
                    For i = 1 To C.Row - Rw
                    C.Offset(-1, 0).Delete xlShiftUp
                    Next
                End If
            End If
        End If
    Next
End Sub