2017-07-27 161 views
-4

我有以下類型的數據在Excel中:Excel的多個列匹配

Col 1  Col 2  Col 3 
a   Apple  x & y & z 
a   Ant   x & y & z 
a   Aeroplane x & y & z 
b   Ball  m & n 
b   Bat   m & n 
c   Cat   k & l 
c   Carrom  k & l 
c   Can   k & l 

我想&字符欄3破/裂值,然後對每個值創建額外的一行。類似下面應該是結果:我用

Col 1  Col 2  Col 3 
a   Apple  x 
a   Apple  y 
a   Apple  z 
a   Ant   x 
a   Ant   y 
a   Ant   z 
a   Aeroplane x 
a   Aeroplane y 
a   Aeroplane z 
b   Ball  m 
b   Ball  n 

值只是樣品的實際數據包含&列分隔的話3

adding image since formating getting messed up for table

+1

歡迎來到Stack Overflow!你的問題只包含要求 - 它沒有顯示你方的任何努力來自己解決這個問題。請將您的嘗試加入這個問題 - 因爲本網站不是免費的「我們做您的(家)工作」服務。除此之外:請轉到[幫助]瞭解如何/在這裏問什麼。謝謝! – GhostCat

+0

抱歉沒有付出任何努力。所有我嘗試過的各種excel公式都喜歡打破價值,但都沒有成功。新vba如此張貼在這裏。 –

+0

對不起,但這不是「我們做你的工作」服務。如果想從VBA活動中獲得一些真正的結果 - 那就不要走彎路了。你最好拿起一本好書/教程並開始學習。或者你決定僱用某人併爲這項工作付錢。任何人都可以免費做這件事的機會很小。 – GhostCat

回答

0

此代碼將按照給定的信息執行您的操作。我鼓勵你查看所使用的方法,因爲它們是困難中的基礎和新手。我已經寫下了解釋每個部分正在做什麼的評論。 GL

Sub test() 
Dim filter, C1, C2 As String 
Dim counter As Integer 
Dim letters() As String 
Dim i, x As Integer 
Dim char As String 

Cells(1, 3).Select 
x = 1 

'Checks to see if the third column is blank, if not it will continue 
While Cells(x, 3).Value <> "" 

'Re instantiates an array of size 3 to store the letters (technically 4 since 0 is also a position) Also erases past stored data 
ReDim letters(3) As String 
i = 0 
'Filter stores the whole string 
filter = ActiveCell.Value 

'Stores the column A and B values of that row in C1 and C2 
C1 = ActiveCell.Offset(, -2).Value 
C2 = ActiveCell.Offset(, -1).Value 

'This for loop goes through each character of filter string 
For counter = 1 To Len(filter) 
'char stores the character 
char = Mid(filter, counter, 1) 
    If char = " " Or char = "&" Then 
    Else: 
    'stores any character not blank and "&" in an array 
    letters(i) = char 
    i = i + 1 
    End If 
Next 

'If statement to skip rows that only have 1 letter 
If letters(1) <> Empty Then 

'For loop that will create a new line and print out array letters in each new row 
For i = 0 To 1 
ActiveCell.Value = letters(i) 
    ActiveCell.Offset(1).EntireRow.Insert 
    ActiveCell.Offset(1).Select 
    ActiveCell.Offset(, -2).Value = C1 
    ActiveCell.Offset(, -1) = C2 
    ActiveCell.Value = letters(i + 1) 
Next i 
End If 
x = x + 1 
ActiveCell.Offset(1).Select 

Wend 
End Sub 
+0

謝謝TJYen。這適用於字母,我會根據需要對其進行更改,在最後一列中也可以有字。 –

+0

對於字符,您可以使用循環來計算直到下一個空格的字符數。然後,只需在'char = Mid(過濾器,計數器,要輸入的字符數)'中使用該字詞即可獲取該單詞 – TJYen

0

由於這是一個「一次性」我會做手動修補

將第3列中只有一個項目的所有行放入新工作表中。 把兩個物品放在另一張紙上 依此類推 然後,將兩個物品紙上的第一個物品取出並將其複製到第一張紙上 然後取出兩個物品紙上的第二個物品並將其複製到第一張。 繼續前進,最終你會得到你想要的數據。

雖然這需要很長時間,但它可能比學習VBA更快,並且編寫例程來完成。

+0

這樣做的數據非常大。感謝您的建議,但。 –