2015-02-23 77 views
0

请帮助我一些关于下面的excel的建议。在早期的形式是这样的:Excel VBA - 逗号分隔单元格到行

A B C 
1 A1 ;100;200;300;400;500; 
2 A2 ;716;721;428;1162;2183;433;434;1242;717;718; 
3 A3 ;100;101; 

,我想达到这样的结果:

A B  C 
1 A1 100 
1   200 
1  300 
1  400 
1  500 
2 A2 716 
2  721 
2  428 
2  1162 
2  2183 
2  433 
2  434 
2  1242 
2  717 
2  718 
3 A3 100 
3  101 

我使用此代码尝试,但它不返回预期的结果。

Sub SliceNDice() 
Dim objRegex As Object 
Dim X 
Dim Y 
Dim lngRow As Long 
Dim lngCnt As Long 
Dim tempArr() As String 
Dim strArr 
Set objRegex = CreateObject("vbscript.regexp") 
objRegex.Pattern = "^\s+(.+?)$" 
'Define the range to be analysed 
X = Range([a1], Cells(Rows.Count, "b").End(xlUp)).Value2 
ReDim Y(1 To 2, 1 To 1000) 
For lngRow = 1 To UBound(X, 1) 
    'Split each string by ";" 
    tempArr = Split(X(lngRow, 2), ";") 
    For Each strArr In tempArr 
     lngCnt = lngCnt + 1 
     'Add another 1000 records to resorted array every 1000 records 
     If lngCnt Mod 1000 = 0 Then ReDim Preserve Y(1 To 2, 1 To lngCnt + 1000) 
     Y(1, lngCnt) = X(lngRow, 1) 
     Y(2, lngCnt) = objRegex.Replace(strArr, "$1") 
    Next 
Next lngRow 
'Dump the re-ordered range to columns C:D 
[c1].Resize(lngCnt, 2).Value2 = Application.Transpose(Y) 
End Sub 

在此先感谢!

+2

您应该格式化您的代码,使其可读。编辑时,您可以看到{}。突出显示您的代码并将其应用到每个人都可读的位置。 – 2015-02-23 16:25:01

+0

这是我的第一个问题。我会牢记这一点。谢谢! – 2015-02-23 16:52:11

+0

“它不会返回预期结果”,它返回什么? – pnuts 2015-02-23 16:56:19

回答

0

这段代码为你工作

Sub SplitAndCopy() 
    Dim sh As Worksheet 
    Set sh = ThisWorkbook.Worksheets("YourTargetSheet") 
    Dim i As Long, j As Long, k As Long 
    k = 2 
    For i = 2 To Cells(Rows.Count, "A").End(xlUp).Row 
     For j = LBound(Split(Range("C" & i).Value, ";")) + 1 To UBound(Split(Range("C" & i).Value, ";")) - 1 
      sh.Range("A" & k).Value = Range("A" & i).Value 
      If j = LBound(Split(Range("C" & i).Value, ";")) + 1 Then 
       sh.Range("B" & k).Value = Range("B" & i).Value 
      End If 
      sh.Range("C" & k).Value = Split(Range("C" & i).Value, ";")(j) 
      k = k + 1 
     Next j 
    Next i 
End Sub 
+0

好的编程习惯之一说:如果某段代码被多于一个使用,则将其移入单独的函数/子例程中。 ;)上面的代码做了它必须做的事情,但是Split函数的结果应该被“保存”到变量中。这是我放弃答案的唯一原因。 – 2015-02-23 17:46:50

+0

对不起,我已经对我的编程习惯感到失望了lol – Jeanno 2015-02-23 18:00:37

+0

我在Set sh = ThisWorkbook.Worksheets(“YourTargetSheet”)行收到此脚本的错误消息。运行时错误'9' - 脚本超出范围。 – 2015-02-24 08:05:00

1

试试这个:

Option Explicit 

Sub DoSomething() 
Dim i As Integer, j As Integer, k As Integer 
Dim srcwsh As Worksheet, dstwsh As Worksheet 
Dim sTmp As String, sNumbers() As String 

Set srcwsh = ThisWorkbook.Worksheets("Sheet1") 
Set dstwsh = ThisWorkbook.Worksheets("Sheet2") 

i = 1 
j = 1 
Do While srcwsh.Range("A" & i) <> "" 
    sTmp = srcwsh.Range("C" & i) 
    sNumbers = GetNumbers(sTmp) 
    For k = LBound(sNumbers()) To UBound(sNumbers()) 
     dstwsh.Range("A" & j) = srcwsh.Range("A" & i) 
     dstwsh.Range("B" & j) = srcwsh.Range("B" & i) 
     dstwsh.Range("C" & j) = sNumbers(k) 
     j = j + 1 
    Next 
    i = i + 1 
Loop 

Set srcwsh = Nothing 
Set dstwsh = Nothing 


End Sub 

Function GetNumbers(ByVal sNumbers As String) As String() 
Dim sTmp As String 

sTmp = sNumbers 
'remove first ; 
sTmp = Left(sTmp, Len(sTmp) - 1) 
'remove last ;) 
sTmp = Right(sTmp, Len(sTmp) - 1) 

GetNumbers = Split(sTmp, ";") 

End Function 

注:我建议你添加错误处理程序。欲了解更多信息,请访问:Exception and Error Handling in Visual Basic

+0

不知道为什么......但这个脚本没有做任何事情...... – 2015-02-24 08:08:29

+0

@ user3016842,相信我,它做它必须做的事情。请检查来源和目的地表单的名称。 – 2015-02-24 11:32:59

0

我宁愿走这条路:

Private Type data 
    col1 As Integer 
    col2 As String 
    col3 As String 
End Type 

Sub SplitAndCopy() 

    Dim x%, y%, c% 
    Dim arrData() As data 
    Dim splitCol() As String 

    ReDim arrData(1 To Cells(1, 1).End(xlDown)) 

    x = 1: y = 1: c = 1 

    Do Until Cells(x, 1) = "" 
     arrData(x).col1 = Cells(x, 1) 
     arrData(x).col2 = Cells(x, 2) 
     arrData(x).col3 = Cells(x, 3) 

     x = x + 1 
    Loop 

    [a:d].Clear 

    For x = 1 To UBound(arrData) 

     Cells(c, 2) = arrData(x).col2 
     splitCol = Split(Mid(arrData(x).col3, 2, Len(arrData(x).col3) - 2), ";") 

     ' sort splitCol 

     For y = 0 To UBound(splitCol) 
      Cells(c, 1) = arrData(x).col1 
      Cells(c, 3) = splitCol(y) 
      c = c + 1 
     Next y 

    Next x 

End Sub 

我不能完全确定,如果你需要你的第三列进行排序,如果你可以添加排序功能。

+0

这个工作得很好!非常感谢! :) – 2015-02-24 08:07:49

+0

好,你可以将它标记为'答案':) – Xarylem 2015-02-24 11:39:20