2017-10-24 1 views
1

거대한 CSV 파일을 여러 개의 청크로 분할하는 매크로가 있습니다. 50,000 개의 행이 있지만 첫 번째 분할 파일 만 헤더가 포함됩니다. 각 별도의 파일에 헤더를 포함하도록 코드를 수정하거나 스크립트의 끝에 폴더의 모든 파일을 반복하고 파일 헤더를 추가하는 로직을 추가하고 싶습니다. 1).VBA를 사용하여 헤더 행을 여러 CSV 파일에 삽입

Sub SplitCSVFile() 
'Splits a text or csv file into smaller files with a user defined number (max) of lines or rows. The new files get the original file name + a number (1, 2, 3 etc.). 

Dim sFile As String 'Name of the original file 
Dim sText As String 'The file text 
Dim lStep As Long 'Max number of lines in the new files 
Dim vX, vY   'Variant arrays. vX = input, vY = output 
Dim iFile As Integer 'File number from Windows 
Dim lCount As Long 'Counter 
Dim lIncr As Long 'Number for file name 
Dim lMax As Long  'Upper limit for loop 
Dim lNb As Long  'Counter 
Dim lSoFar As Long 'How far did we get? 

On Error GoTo ErrorHandle 

'Select a file 
sFile = Application.GetOpenFilename() 

'If the user cancelled 
If sFile = "False" Then Exit Sub 

'Ask the user for max number of lines per file. 
lStep = Application.InputBox("Max number of lines/rows?", Type:=1) 

'Arrays have zero as LBound, so we subtract 1 
lStep = lStep - 1 

'Read the file text to sText 
sText = _ 
CreateObject("Scripting.FileSystemObject").OpenTextFile(sFile).ReadAll 

'Put the text into the array vX. Carriage return–linefeed combination will add a new row to the array. 
vX = Split(sText, vbCrLf) 

'Free memory 
sText = "" 

'Start a loop that will run until all rows in the array have been read and saved into new files. The variable lSoFar keeps track of how far we are in vX. 
Do While lSoFar < UBound(vX) 
    'If the number of rows minus lSoFar is bigger than max number of rows, the array vY is dimensioned to max number of rows. 
    If UBound(vX) - lSoFar >= lStep Then 
     ReDim vY(lStep) 
     'lMax is set = last rownumber to be copied to vY. 
     lMax = lStep + lSoFar 
    Else 
     'Else we dimension vY to the number of rows left. 
     ReDim vY(UBound(vX) - lSoFar) 
     'Last row to copy is last row in vX 
     lMax = UBound(vX) 
    End If 

    lNb = 0 
    'Now we copy the rows from vX to vY 
    For lCount = lSoFar To lMax 
     vY(lNb) = vX(lCount) 
     lNb = lNb + 1 
    Next 

    'lSoFar keeps track of how far we got in vX 
    lSoFar = lCount 

    'Get a free file number 
    iFile = FreeFile 

    'Increment the number for the new file name 
    lIncr = lIncr + 1 

    'Save vY as a CSV file 
    'Open sFile & "-" & lIncr & ".csv" For Output As #iFile 
    Open Replace(sFile, ".csv", "") & "_" & lIncr & ".csv" For Output As #iFile 
     'The Join function makes a text string from the array elements. 
     Print #iFile, Join$(vY, vbCrLf) 
    Close #iFile 
Loop 

Erase vX 
Erase vY 

Exit Sub 
ErrorHandle: 
MsgBox Err.Description & " Procedure SplitTextFile" 
End Sub 

답변

1

그냥

Print #iFile, IIf(lIncr = 0, "", vX(0) & vbCrLf) & Join$(vY, vbCrLf) 
+0

퍼펙 라인을

Print #iFile, Join$(vY, vbCrLf) 

교체 :

다음은 현재 스크립트입니다! 감사. 헤더를 이미 가지고 있기 때문에 첫 번째 파일에 대해 이렇게하지 않는 if 문을 추가했습니다. – user2725402

+0

@ user2725402 추가 검사가 필요 없지만 조건은 이미 IIf (lIncr = 0, "", vX (0) & vbCrLf)'표현식에 있습니다. – omegastripes

관련 문제