Hi, I wonder whether someone could help me please.
I'm using the script below to split files into more manageable chunks.
The script works fine, except for this line:
When the files are created the format is: "filename"<filename>.csv-"incrementno".<incrementno>
I'd like to change this to "filename"-"incrementno"<filename><incrementno>.csv
I've looked at this all morning and tried different permutations of the code, but I can't get this to work.
I just wondered whether someone could look at this please and offer some guidance on how I can achieve this.
Many thanks and kind regards
Chris</incrementno></filename></incrementno></filename>
I'm using the script below to split files into more manageable chunks.
Code:
Sub SplitTextFile()
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
sFile = Application.GetOpenFilename()
If sFile = "False" Then Exit Sub
lStep = Application.InputBox("Max number of lines/rows?", Type:=1) + 1
lStep = lStep - 1
sText = _
CreateObject("Scripting.FileSystemObject").OpenTextFile(sFile).ReadAll
vX = Split(sText, vbLf)
sText = ""
Do While lSoFar < UBound(vX)
If UBound(vX) - lSoFar >= lStep Then
ReDim vY(lStep)
lMax = lStep + lSoFar
Else
ReDim vY(UBound(vX) - lSoFar)
'Last row to copy is last row in vX
lMax = UBound(vX)
End If
If lSoFar = 0 Then
lNb = 0
lMax = lMax
Else
lMax = lMax - 1
lNb = 1
End If
For lCount = lSoFar To lMax
vY(0) = vX(0)
vY(lNb) = vX(lCount)
lNb = lNb + 1
Next
lSoFar = lCount
iFile = FreeFile
lIncr = lIncr + 1
Open sFile & "-" & lIncr & ".csv" For Output As #iFile
Print #iFile, Join$(vY)
Close #iFile
Loop
Erase vX
Erase vY
Exit Sub
ErrorHandle:
MsgBox Err.Description & " Procedure SplitTextFile"
End Sub
The script works fine, except for this line:
Code:
Open sFile & "-" & lIncr & ".csv" For Output As #iFile
When the files are created the format is: "filename"<filename>.csv-"incrementno".<incrementno>
I'd like to change this to "filename"-"incrementno"<filename><incrementno>.csv
I've looked at this all morning and tried different permutations of the code, but I can't get this to work.
I just wondered whether someone could look at this please and offer some guidance on how I can achieve this.
Many thanks and kind regards
Chris</incrementno></filename></incrementno></filename>
Last edited: