将A列和E列的excel值与B列和D列中的名称匹配

时间:2016-05-28 10:36:55

标签: excel vba

基本上我有一个带有数字的巨大excel文件,我想为其指定一个名称(我以名称UNKNOWN开头)

btn_send

为不同的行值分配序列号 如果列A2 =列E25,则用UNKNOWN2

替换D25的值

这有意义吗?

我正在寻找能够自动执行此操作的宏

1 个答案:

答案 0 :(得分:0)

很明显,我理解你......

A列和D列中有数字 - 对于您想要特定名称的每个数字。

名称应列在B栏(A中的数字)和E栏(D中的数字)中。

如果A中的数字也在D中,则这些数字共享相同的名称。

试试这段代码:

Sub Test()

Dim rng As Range
Dim i As Long, j As Long, k As Long, l As Long
Dim firstAddress As String, foundAddress As String

For i = 1 To Rows.Count - 1

    If IsEmpty(Cells(i, 1).Value) Then Exit For

Next i

For j = 1 To Rows.Count - 1

    If IsEmpty(Cells(j, 4).Value) Then Exit For

Next j

Set rng = Range("A1:A" & i & ", D1:D" & j)

l = 1

For k = 1 To i + j

    If k < i Then

        firstAddress = rng.Find(Cells(l, 1).Value, Lookat:=xlWhole, MatchCase:=True).Address
        foundAddress = firstAddress

        If Cells(l, 2).Value = "" Then

            Do Until firstAddress = ""

                Cells(Range(foundAddress).Row, Range(foundAddress).Column + 1) = "Name" & k

                foundAddress = rng.Find(Cells(l, 1).Value, Range(foundAddress), Lookat:=xlWhole, MatchCase:=True).Address

                If foundAddress = firstAddress Then _
                    Exit Do

            Loop

        End If

    Else

        If k = i Then _
            l = 1

        firstAddress = rng.Find(Cells(l, 4).Value, Lookat:=xlWhole, MatchCase:=True).Address
        foundAddress = firstAddress

        If Cells(l, 5).Value = "" Then

            Do Until firstAddress = ""

                Cells(Range(foundAddress).Row, Range(foundAddress).Column + 1) = "Name" & k

                foundAddress = rng.Find(Cells(l, 4).Value, Range(foundAddress), Lookat:=xlWhole, MatchCase:=True).Address

                If foundAddress = firstAddress Then _
                    Exit Do

            Loop

        End If

        If l = j - 1 Then _
            Exit Sub

    End If

    l = l + 1

Next k

End Sub

这是图片 - 你想要的是什么?

Before

After