如果D列中的单元格包含“,”,我试图复制一行并直接插入新行的下方。我还想做更多的事情,但是更容易在之前和之后显示示例。
之前
Jon Junior Male A, X
Pete Freshman Male A, X
Tyler Senior Male A, X, C
Dave Senior Male A, C, P, U
之后
Jon Junior Male A
Jon Junior Male X
Pete Freshman Male A
Pete Freshman Male X
Tyler Senior Male A
Tyler Senior Male X
Tyler Senior Male C
Dave Senior Male A
Dave Senior Male C
Dave Senior Male P
Dave Senior Male U
我已经尝试结合过去的问题中的一些想法,但是它们似乎没有用,而且我之前从未真正使用过VBA。
Dim GCell As Range
Dim N As Long, i As Long
N = ws.Cells(Rows.Count, "D").End(xlUp).Row
For Each GCell In Range("D2:D")
If GCell.Value Like "*,*" Then
GCell.Offset(1, 0).EntireRow.Insert
ws.Cells(i, "D").EntireRow.Copy ws.Cells(i + 1, 1)
Else
End If
i = i + 1
Next GCell
答案 0 :(得分:0)
您可以这样做,调整工作表名称以适合(我将结果放在其他工作表上):
Sub x()
Dim r As Range, v As Variant
For Each r In Sheets("Sheet1").Range("A1").CurrentRegion.Columns(1).Cells
v = Split(r.Offset(, 3), ",")
r.Resize(, 3).Copy Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp)(2).Resize(UBound(v) + 1)
Sheets("Sheet2").Range("D" & Rows.Count).End(xlUp)(2).Resize(UBound(v) + 1).Value = Application.Transpose(v)
Next r
End Sub
Split
使用逗号作为分隔符将其转换为数组;通过转置它,您可以将其变成一个“垂直”数组,可以粘贴到输出表中。