2017-06-22 13 views
0

我有類似這樣的數據,其中我在列中的值B,d和F可能偶爾會重複:Excel - 自動填充/數字與跨列和行的自定義模式,同時允許重複?

Column A  Column B  Column C  Column D  Column E  Column F 
--------  --------  --------  --------  --------  -------- 
       Data1      Data2      Data3 
       Data4      Data1           
       Data5            Data1 
       Data3 

我需要做的是自動填充/數列A,C和E,第一去然後垂直使用自定義模式水平放置在工作表上 - 實質上爲列B,D和F中的數據提供唯一的ID。

這樣做,我需要記住以下幾點:

  1. 在列B,d和F重複的值沒有被分配一個新的ID,但接收先前分配的ID。
  2. 列B將始終有數據,因此我希望列A的所有行都被填充。
  3. 列D和F可能不總是有數據。
    • 所以,我只希望列C中的行被填充,如果列D包含數據。
    • 同樣,如果列F包含數據,我只希望列E中的行被填充。

我期望達到的最終結果是:

Column A  Column B  Column C  Column D  Column E  Column F 
--------  --------  --------  --------  --------  -------- 
UR001  Data1  UR002  Data2  UR003  Data3 
UR004  Data4  UR001  Data1           
UR005  Data5         UR001  Data1 
UR003  Data3 

不幸的是我的表是很長的,跨越近千這使得它很難做手工編號。如果我需要在中間引入額外的行,我基本上最終會打破從這一點開始的序列,並且必須一次又一次地開始編號直到結束。

我試過搜索內置的公式和vba代碼,但我無法找到適合我的問題的東西。請幫助!

+3

我正在投票關閉這個問題作爲[off-topic](https://stackoverflow.com/help/on-topic),因爲'*我需要做的是自動填充/編號列A,C和E ... *'是**不是**一個特定的編程問題,並添加像'*我試圖尋找內置的公式和vba代碼的敘述碎片,但我無法找到適合我的問題的東西。請幫助!*'不做任何改變。 – Jeeped

回答

0

請試試看。它使用一個Dictionary來存儲以前在列B,D,F中識別的數據字符串。

Sub BreadthSearchFirst() 

Dim dict 
Set dict = CreateObject("Scripting.Dictionary") 

Dim sht1 As Worksheet 
Set sht1 = ActiveWorkbook.ActiveSheet 

Dim ID, newID, key As String 
Dim j, i, count As Integer 

ID = "UR" 
count = 1 

'include the starting and last row number 
startrow = 1 
totalrow = 100 

'loop over each row 
For j = startrow To totalrow 
    'loop over each column 
    For i = 1 To 6 
     'looks only in even columns 
     If i Mod 2 = 0 Then 
      'Checks if a Value is Present 
      If sht1.Cells(j, i) <> "" Then 
       'assigns the value as a key if the cell is not empty 
       key = sht1.Cells(j, i) 
       'checks if the key is present in the dictionary 
       If dict.Exists(key) Then 
        'call the previously stored data if it exists 
        sht1.Cells(j, i - 1) = dict.Item(key) 
       Else 
        'places the newID in the odd column 
        newID = ID & count 
        sht1.Cells(j, i - 1) = (ID & count) 
        'creates a new key within the dictionary 
        dict.Add key, newID 
        'increment the ID count 
        count = count + 1 
       End If 
      End If 
     End If 

    Next i 
Next j 

End Sub 

有幾件事情需要注意 你需要做出改變,如果列不是第6列(即A至F) 修改起點和終點行,有可用的定位方法最後一行放在表單中,並可以相應地替換。在偶數列(B,D,F)中不應該有任何空白空間,如果是這樣的話,它會將其識別爲數據。

祝你好運!

+0

謝謝,我能夠爲我的案子做這項工作,在這裏和那裏做一些調整! – zu92

+0

精彩@ zu92 – Albert