我有一个宏将一个巨大的CSV文件拆分成多个例如50,000行,但只有第一个拆分文件包含标题。我需要修改代码以在每个单独的文件中包含标头,或者在脚本的末尾我想添加一个逻辑,它将循环遍历文件夹中的所有文件并添加标头记录(文件除外) 1)。
这是当前的脚本:
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
答案 0 :(得分:1)
只需替换
行Print #iFile, Join$(vY, vbCrLf)
与
Print #iFile, IIf(lIncr = 0, "", vX(0) & vbCrLf) & Join$(vY, vbCrLf)
答案 1 :(得分:-1)
在Print语句中使用lIncr = 0值将为您拆分的第一个csv生成辅助标头。
由于在调用Print语句时lIncr值已经为1,并且标头是要为初始csv打印的第一行,因此IIF检查失败,并包含辅助标头vX(0)。
IIF检查用于填充所需的每个其他csv的标头。
正确的Print语句是...
打印#iFile,IIf(lIncr = 1,“”,vX(0)&vbCrLf)&Join $(vY,vbCrLf)