移动列之间重复的值

时间:2015-09-25 22:39:30

标签: excel vba excel-vba

  1. 这是我的Excel表格。列G和列J具有相同的值但顺序不同。
  2. enter image description here

    1. 代码运行并将值从列J移动到列H,旁边是G列的相同值。
    2. enter image description here

      1. 这个问题是,当有两个值相同时,它会将它移动到它在列H中找到的第一个值.J:2中的值[3,] [2,] [1,] [8,] [29,] [5,] [6,] [7,] [28,] [4,] [22,] [23,] [24,] [25,] [26,] [27,] [9,] [10,] [11,] [12,] [13,] [14,] [15,] [16,] [17,] [18,] [19,] [20,] [21,] 必须移到H:2,而值{ J:5中的{1}}必须移到H:6而不是H:2。
      2. enter image description here enter image description here

        这是代码:

        747.00

1 个答案:

答案 0 :(得分:0)

听起来你正在J列中骑车并在G栏中寻找一场比赛。我已经通过在G栏骑行并在第J栏中寻找一场比赛来扭转局面。一旦找到一场比赛,它就会被移动到列H,从而将其从列J中删除。

Sub match_em_up()
    Dim rwg As Long, rwj As Long

    With Worksheets("Sheet1")
        For rwg = 2 To .Cells(Rows.Count, "G").End(xlUp).Row
            If CBool(Application.CountIf(.Columns("J"), .Cells(rwg, "G").Value2)) Then
                rwj = Application.Match(.Cells(rwg, "G").Value2, .Columns("J"), 0)
                .Cells(rwg, "H") = .Cells(rwj, "J").Value2
                .Cells(rwj, "J").ClearContents
            End If
        Next rwg
    End With
End Sub

在:

enter image description here

后:

enter image description here