我有一张excel表格需要操作。我对VBA略知一二,但还不足以实现自动化。
电子表格的格式如下
Cell1 Cell2 Cell3 Cell4
a b c 1, 2, 3, 4, 5, 6
a d e 1 3 5
a f g 7-8
(是的,它们使用了各种分隔符)
我只需要将Cell4拆分成新的行,并让Cells1-3在每行上。
a b c 1
b b c 2
etc...
我已经尝试记录了用于拆分文本和转置的宏,但我无法理解新行的插入等。有什么帮助吗?
发布于 2013-06-27 00:03:39
完整的解决方案
Sub newmac()
Dim rowCount As Long
Dim rowOffset As Long
Dim rowCurrent As Long
Dim subRow As Long
Dim c() As String
rowOffset = 0
rowCount = Application.ActiveSheet.UsedRange.Rows.CountLarge
For i = 1 To rowCount
rowCurrent = i + rowOffset
c = Split(Cells(rowCurrent, 4), ",")
If (UBound(c) <= 0) Then c = Split(Cells(rowCurrent, 4), " ")
If (UBound(c) <= 0) Then c = Split(Cells(rowCurrent, 4), "-")
' more than 1 item. process
If (UBound(c) > 0) Then
Cells(rowCurrent, 4) = c(0)
For j = 1 To UBound(c)
subRow = rowCurrent + j
range("A" & rowCurrent & ":D" & rowCurrent).Copy
range("A" & subRow & ":D" & subRow).Insert
Cells(subRow, 4) = c(j)
Next j
rowOffset = rowOffset + UBound(c)
End If
Next i
End Sub
我的结果是:
a b c 1
a b c 2
a b c 3
a b c 4
a b c 5
a b c 6
a d e 1
a d e 3
a d e 5
a f g 7
a f g 8
发布于 2013-06-26 23:24:05
使用此选项插入一个空行
ActiveSheet.Cells(2, 1).EntireRow.Resize(1).Insert
https://stackoverflow.com/questions/17323876
复制相似问题