2016-05-28 29 views
1

基本上我有一個巨大的Excel與我想(我開始與姓名不詳)指定名稱爲數字匹配Excel列A和E的值的名字在列B和d

A | B 
123456 | Unknown 
456875 | Unknown2 
文件

爲不同的行值分配一個序列號。
如果A2列= E25列,用UNKNOWN2替換D25的值

這是否合理?

我找的,會做自動

+0

'這是否有意義?'不給我....你能嘗試發佈您的表看起來像什麼,你想要什麼樣的數據添加一個更清晰的表示?像「E25欄」和「1,2,3-4000」這樣的短語非常混亂。 –

+0

對不起,我不知道如何更清楚地解釋我自己,大聲笑 –

+0

是隻在一列中的「數字」? – user3598756

回答

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

+0

哇,這實際上工作(有一些錯誤取代了一些用戶),但是感謝一堆,節省了我幾天的工作! +1 –

相關問題