如何在Excel中的两列中安排重复的条目?

时间:2018-09-08 10:50:36

标签: excel excel-formula

在我的Excel中,我有5列(A,B,C,D,E)和行(1至10)。两个列(A和B)包含一些匹配的文本-例如,假设A1和B5具有“ Apple”,而A3和B2具有“ Orange”,现在我如何排列B列,使得“ Apple”和“ Orange”中的文本B列可以移到与A相同的行(即B1 = Apple,B3 = Orange),而不会干扰其他列的输入?

[Before]

[Desired outcome]

2 个答案:

答案 0 :(得分:2)

如果您要使用非VBA解决方案,则可以使用以下公式并向下拖动:

= IF(COUNTIF(B$1:B$10,A1)>0,A1,IFERROR(INDEX(B$1:B$10,MATCH(B1,A$1:A$10,0)),B1))

请参见下面的工作示例。

enter image description here

当然,这里的缺点是B列本身未更新。必须创建一个新列。如果您实际上希望更新B列,则需要VBA,我建议使用@ Gary'sStudent的解决方案。


编辑

根据对原始问题的更新,您似乎只需要基本的INDEX / MATCH

= INDEX(C$1:C$8,MATCH($A1,$B$1:$B$8,0))

请参见下面的工作示例。请注意,无需在B列中为您想要的公式创建公式,因为它已经与A列相同。

enter image description here

答案 1 :(得分:1)

这基于交换条目。之前:

enter image description here

并运行以下交换代码:

Sub swap()
    Dim i As Long, j As Long, temp As String, v As String
    For i = 1 To 10
        v = Cells(i, 1)
        For j = 1 To 10
            If v = Cells(j, 2) Then
                temp = Cells(i, 2)
                Cells(i, 2) = v
                Cells(j, 2) = temp
            End If
        Next j
    Next i
End Sub

导致:

enter image description here

相关问题