2017-07-27 163 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

这样做的数据非常大。感谢您的建议,但。 –