2017-06-23 63 views
-2

我有一個excel表格+ 19k行。 假設表格有2列(Departmant,id)。 有25個部門,每個部門都有唯一的ID。Excel嵌套如果有問題?

我可以用什麼公式來正確地獲得他們每個人的ID。

我嘗試使用,如果,但我猜它不與+ 25工作,如果在它..

有沒有簡單的方法來做到這一點?

我是一個初學者,我在這張桌子上工作了3天!

+0

嘗試[VLOOKUP](https://support.office.com/en-us/article/VLOOKUP-function-0bbc8083-26fe-4963-8ab8-93a18ad188a1)。 – pnuts

回答

0

來自MrExcel.com。未測試的

Sub FindDistinctValues() 
Dim LastRowFrom As Long 
Dim LastRowTo As Long 
Dim i As Long, j As Long 
Dim temp As Integer 
Dim found As Boolean 

'determines the last row that contains data in column A 
LastRowFrom = Range("A" & Rows.Count).End(xlUp).Row 

'Loop for each entry in column A 
For i = 2 To LastRowFrom 
    'get the next value from column A 
    temp = Range("A" & i).Value 

    'Determine the last row with data in column B 
    LastRowTo = Range("B" & Rows.Count).End(xlUp).Row 

    'initialize j and found 
    j = 1 
    found = False 

    'Loop through "To List" until a match is found or the list has been searched 
    Do 
     'check if the value exists in B column 
     If temp = Range("B" & j).Value Then 
     found = True 
     End If 
     'increment j 
     j = j + 1 
     Loop Until found Or j = LastRowTo + 1 

    'if the value is not already in column B 
    If Not found Then 
     Range("B" & j).Value = temp 
    End If 
Next i 
End Sub