2017-01-09 25 views
2

セルの文字列を1つのExcelスプレッドシートでさまざまなセルに分割し、分割したセルを新しい見出しでコピーして新しいシートに貼り付けようとしています。以下は私が分割しようとしているもののイメージです。ここでExcel VBA-セルをセルごとに分割してセルを新しいシートにコピー

What I am trying to split

私が達成しようとしているものです。 Wanted Outcome

残念ながら、私は画像が表示されないように、stackoverflowに新しいです。ユーザーがリンクをクリックしたくない場合は、他の方法で説明しようとします。

私は分割しようとしている長い文字列を含むさまざまなセルを持っています。 以下は、分割したい2行の例です。私は以下に示すように、次のヘッダを上記を分割したい

|(カラム区切りを表す)

Setup  | MC 1: 1 x 18 , MC 2: 2 x 23 , MC 3: 2 x 32| 
------------|---------------------------------------------- 
    Microphone | 2 x PHILIP DYNAMI SBMCMD     | 

Setup  |  |Speaker|Tables|People|Speaker|Tables|People|Speaker|Tables|People| 
---------------------------------------------------------------------------------- 
      |  | MC1 | 1 | 18 | MC2 | 2 | 23 | MC3 | 2 | 32 | 
-------------------------------------------------------------------------------------- 
      |  |  |  |  |  |  |  |  | 
--------------------------------------------------------------------------------------- 
Microphone |  |Number |Manufc| Model|MdlNum | 
    --------------------------------------------------------------------------- 
      |  | 2 |PHILIP|DYNAMI|SBMCMD | 

次のコードは、セットアップ行に使用できます。ただし、マイクの行では機能しません。正しい区切り文字を分割することはできますが、マイクデータを含む正しい行は対象としません。

Sub Sample() 

Dim MYAr, setup 
Dim MicAr, Mic 
Dim ws As Worksheet, wsOutput As Worksheet 
Dim Lrow As Long, i As Long, j As Long, rw As Long, col As Long, Rrow As Long 

Dim arrHeaders 
Dim arrayHeadersMic 


Set ws = ThisWorkbook.Sheets("Sheet1") '~~> Set this to the relevant worksheet 
'Set wsOutput = ThisWorkbook.Sheets.Add '~~> Add a new worksheet for output 
Set wsOutput = ThisWorkbook.Sheets("Sheet2") 
rw = 2 '<< output starts on this row 
arrHeaders = Array("Speaker", "Tables", "People") 
arrHeadersMic = Array("Number", "Manufacturer", "Model", "Model Number") 

With ws 
    Lrow = .Range("B" & .Rows.Count).End(xlUp).Row '~~> get the last row 
    For i = 1 To Lrow 
     If .Cells(i, 1).Value = "Setup" Then 

      wsOutput.Cells(rw, 1).Value = "Setup" 
      wsOutput.Cells(rw + 3, 1).Value = "Microphone" 

      setup = .Range("B" & i).Value 
      If Len(setup) > 0 Then 'Len Returns an integer containing either the number of characters in a string or the nominal number of bytes required to store a variable. 

       MYAr = SetupToArray(setup) 
       'add the headers 
       wsOutput.Cells(rw, 3).Resize(1, 3).Value = arrHeaders 
       wsOutput.Cells(rw + 3, 3).Resize(1, 4).Value = arrHeadersMic 

       'fill headers across 
       wsOutput.Cells(rw, 3).Resize(1, 3).AutoFill _ 
        Destination:=wsOutput.Cells(rw, 3).Resize(1, UBound(MYAr) + 1) 
       'populate the array 
       wsOutput.Cells(rw + 1, 3).Resize(1, UBound(MYAr) + 1).Value = MYAr 

       'figure out the microphone values here.... 

       Lrow = .Range("B" & .Rows.Count).End(xlUp).Row 


       If .Cells(5, 1).Value = "Microphone" Then 



        setup = 0 
        Mic = .Range("B" & i).Value 
        'If Len(Mic) > 0 Then 

        MicAr = MicToArray(Mic) 

         'fill headers across 
         wsOutput.Cells(rw + 3, 3).Resize(1, 4).AutoFill _ 
         Destination:=wsOutput.Cells(rw + 3, 3).Resize(1, UBound(MicAr) + 1) 'UBound Returns the highest available subscript for the indicated dimension of an array. 

         'populate the array 
         wsOutput.Cells(rw + 4, 3).Resize(1, UBound(MicAr) + 1).Value = MicAr 

        'End If 

       End If 



       rw = rw + 7 
      End If 
     End If 
    Next i 


End With 

End Sub 

Function SetupToArray(v) 
Dim MYAr, i 
v = Replace(v, ":", ",") 
v = Replace(v, " x ", ",") 
MYAr = Split(v, ",") 
'trim spaces... 
For i = LBound(MYAr) To UBound(MYAr) 
    MYAr(i) = Trim(MYAr(i)) 
Next i 
SetupToArray = MYAr 
End Function 

Function MicToArray(w) 
Dim MicAr, i 
w = Replace(w, " x ", " ") 
'w = Replace(w, " ", ",") 
MicAr = Split(w, " ") 



'trimspace 
For i = LBound(MicAr) To UBound(MicAr) 
    MicAr(i) = Trim(MicAr(i)) 
Next i 
MicToArray = MicAr 

End Function 

ありがとうございました!

+0

スクリーンショットはパイプで区切られていますが、サンプルテキストはコンマで区切られています。これは実際の区切り文字ですか? –

+0

お詫び@TimWilliamsはコンマで取ることができます、私はスクリーンショットを変更します – smurf

+2

最初の方法のための最も簡単なアプローチは、あなたの様々な区切り文字をすべて単一のタイプに置き換えてから分割することです。 –

答えて

2

EDIT:更新とテスト - Windowsのクリップボードに範囲をコピーして、TSVテキスト形式を使用する方が簡単あなたの "設定" データ

Sub Sample() 

    Dim MYAr, setup 
    Dim ws As Worksheet, wsOutput As Worksheet 
    Dim Lrow As Long, i As Long, j As Long, rw As Long, col As Long 
    Dim arrHeaders 


    Set ws = ThisWorkbook.Sheets("Sheet1") '~~> Set this to the relevant worksheet 
    Set wsOutput = ThisWorkbook.Sheets.Add '~~> Add a new worksheet for output 
    rw = 2 '<< output starts on this row 
    arrHeaders = Array("Speaker", "Tables", "People") 

    With ws 
     Lrow = .Range("B" & .Rows.Count).End(xlUp).Row '~~> get the last row 
     For i = 1 To Lrow 
      If .Cells(i, 1).Value = "Setup" Then 

       wsOutput.Cells(rw, 1).Value = "Setup" 
       wsOutput.Cells(rw + 1, 1).Value = "Microphone" 

       setup = .Range("B" & i).Value 
       If Len(setup) > 0 Then 

        MYAr = SetupToArray(setup) 
        'add the headers 
        wsOutput.Cells(rw, 3).Resize(1, 3).Value = arrHeaders 
        'fill headers across 
        wsOutput.Cells(rw, 3).Resize(1, 3).AutoFill _ 
         Destination:=wsOutput.Cells(rw, 3).Resize(1, UBound(MYAr) + 1) 
        'populate the array 
        wsOutput.Cells(rw + 1, 3).Resize(1, UBound(MYAr) + 1).Value = MYAr 

        'figure out the microphone values here.... 

        rw = rw + 6 
       End If 
      End If 
     Next i 
    End With 

End Sub 

Function SetupToArray(v) 
    Dim MYAr, i 
    v = Replace(v, ":", ",") 
    v = Replace(v, " x ", ",") 
    MYAr = Split(v, ",") 
    'trim spaces... 
    For i = LBound(MYAr) To UBound(MYAr) 
     MYAr(i) = Trim(MYAr(i)) 
    Next i 
    SetupToArray = MYAr 
End Function 
+0

Timさんありがとうございました。あなたの例を使用しようとしていますが、現在のコードで実装するのに問題があります。私は自分の変更を自分のコードに更新しましたが、ランタイムエラー1004が発生しています:アプリケーション定義またはオブジェクト定義のエラー。 '配列をシートに書き込む ws.output.Cells(rw、col).Resize(1、UBound(arr)+ 1).Value = arr – smurf

+0

これは、私のコードに誤って入力してください。 'ws.output' >>' wsOutput' –

+0

Cheers Tim。私は自分のコードに変更を加え、ランタイムエラー '1004'を取得しています:アプリケーション定義またはオブジェクト定義のエラーです。私はまだVBAに新しく、間違っていることを理解するのに苦労しています – smurf

1

の作品(テストされていません):

Sheet1.Cells.Copy ' copy the range 

With CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}") ' this is late bound MSForms.DataObject 
    Dim s As String 
    .GetFromClipboard     ' get the formats from the Windows Clipboard 
    s = .GetText      ' get the "Text" format 
    Application.CutCopyMode = False 

    ' magic 
    s = Replace(s, "MC ", "MC")  ' "MC 1" to "MC1" 
    s = Replace(s, " x ", "|")  ' "1 x 18" to "1|18" 
    s = Replace(s, " , ", "|")  ' "18 , MC" to "18|MC" 
    s = Replace(s, ": ", "|")  ' "MC1: 1" to "MC1|1" 
    s = Replace(s, " ", "|")  ' "2|PHILIP DYNAMI SBMCMD" to "2|PHILIP|DYNAMI|SBMCMD" 

    ' "more magic" 
    s = Replace(s, "Setup" & vbTab, "/Setup||Speaker|Tables|People|Speaker|Tables|People|Speaker|Tables|People/||") 
    s = Replace(s, "Microphone" & vbTab, "/Microphone||Number|manufacturer|Model|Model Num/||") 
    s = Replace(s, "|", vbTab)  ' cells are separated by tab 
    s = Replace(s, "/", vbNewLine) ' rows are separated by new line 

    .SetText s 
    .PutInClipboard 
End With 

Sheet2.PasteSpecial "Text"  ' or Sheet2.Range("A1").PasteSpecial