2017-04-13 12 views
1

フォルダに保存されているCVS'sすべてを1つのExcelシートにマージしようとしています。それらがマージされた後は、別々のマクロを実行して、それぞれの個別の書式設定を処理します。CSVファイルを個別にフォーマットします。これは、1シートにCSVのすべてのファイルをマージしますが、各CSVファイルには、12行を占めてトップにヘッダーおよび他の役に立たない情報を持っているCSVを1つのExcelシートにマージしてヘッダーを削除する

Sub MergeFiles_Click() 

Dim strSourcePath As String 
Dim strDestPath As String 
Dim strFile As String 
Dim strData As String 
Dim x As Variant 
Dim Cnt As Long 
Dim r As Long 
Dim c As Long 

Application.ScreenUpdating = False 

strSourcePath = Sheet1.Range("G2").Value 

If Right(strSourcePath, 1) <> "\" Then strSourcePath = strSourcePath & "\" 

strFile = Dir(strSourcePath & "*.csv") 

Do While Len(strFile) > 0 

    Cnt = Cnt + 1 

    If Cnt = 1 Then 
      r = 6 
     Else 
      r = Cells(Rows.Count, "A").End(xlUp).Row + 1 
    End If 


    Open strSourcePath & strFile For Input As #1 
    Do Until EOF(1) 
      Line Input #1, strData 
      x = Split(strData, ",") 
      For c = 0 To UBound(x) 
       Cells(r, c + 1).Value = Trim(x(c)) 
      Next c 
      r = r + 1 
     Loop 

    Close #1 

    strFile = Dir 
Loop 

Application.ScreenUpdating = True 

If Cnt = 0 Then _ 
    MsgBox "No CSV files were found...", vbExclamation 

End Sub 

以下のコードは、私がこれまで持っているものです。

最初にCSVが置かれている12行をExcelに置いておきますが、Excelシートに入れる前に残りのファイルからこれらの12行を削除します。

私は基本的には、ファイルがコピーされ、シートの下に貼り付けられたように見えるのではなく、ファイルを1つに見せたいだけです。

ご協力いただければ幸いです。

+1

が、あることCSV形式では通常リテラル文字列(二重引用符で囲まれたフィールドは "このように")内にカンマを埋め込むことができます。それらのいずれかを取得した場合、コードは失敗します。 –

+0

@RichHoltonので、これをテストした後、私はこれが問題を引き起こしたいくつかの例を見つけました。この問題を回避するにはどうすればよいですか? –

+1

この質問/回答は参考になるかもしれません:http://stackoverflow.com/questions/12197274/is-there-a-way-to-import-data-from-csv-to-active-excel-sheet –

答えて

3

既存のコードへの最も簡単な変更はCntが1の場合は最初の12行をコピーするだけにコードを単に含めることで、そうでない場合はそれらを無視:あなたはあなたが作業しているデータを知って

Sub MergeFiles_Click() 

    Dim strSourcePath As String 
    Dim strDestPath As String 
    Dim strFile As String 
    Dim strData As String 
    Dim x As Variant 
    Dim Cnt As Long 
    Dim r As Long 
    Dim c As Long 
    Dim inputRow As Long 

    Application.ScreenUpdating = False 

    strSourcePath = Sheet1.Range("G2").Value 

    If Right(strSourcePath, 1) <> "\" Then strSourcePath = strSourcePath & "\" 

    strFile = Dir(strSourcePath & "*.csv") 

    Do While Len(strFile) > 0 

     Cnt = Cnt + 1 

     If Cnt = 1 Then 
      r = 6 
     Else 
      r = Cells(Rows.Count, "A").End(xlUp).Row + 1 
     End If 


     Open strSourcePath & strFile For Input As #1 
     inputRow = 0 
     Do Until EOF(1) 
      Line Input #1, strData 
      'Maintain a count of how many rows have been read 
      inputRow = inputRow + 1 
      'Only process rows if this is the first file, or if we have 
      'already passed the 12th row 
      If Cnt = 1 Or inputRow > 12 Then 
       x = Split(strData, ",") 
       For c = 0 To UBound(x) 
        Cells(r, c + 1).Value = Trim(x(c)) 
       Next c 
       r = r + 1 
      End If 
     Loop 

     Close #1 

     strFile = Dir 
    Loop 

    Application.ScreenUpdating = True 

    If Cnt = 0 Then _ 
     MsgBox "No CSV files were found...", vbExclamation 

End Sub 
+0

これは完璧に働いてくれてありがとう! –

関連する問題