Split Files

ir121973

Active Member
Joined
Feb 9, 2008
Messages
371
Hi, I wonder whether someone could help me please.

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:

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
No.
That can't be behaving as you suggest.
The last element on that line is ".csv"
Therefore there can be nothing after the .csv
So you can't get

"filename".csv-"incrementno".

It CANNOT be behaving as you suggest.

Recheck the code you have posted.
Run your code again.

The code you posted looks like it will operate EXACTLY as you want.
 
Last edited:
Upvote 0
Hi Special-K99, thank you for taking the time to reply.

I too thought the same, but if you look at the original here Split a text file into smaller files with Excel VBA, you'll see that, other than copying the header row for each cut down file, my code is exactly the same. I've also run the original and I still get the same output.

Kind Regards

Chris
 
Upvote 0
You say you are getting files with this format

"filename".csv-"incrementno"

So the extension is csv-"incrmentno"

Are you sure you are not getting this format

"filename".csv-"incrementno".csv

Print the value of sFile to a messagebox before creaqting the output file.
If it displays ".csv" then you need to remove that extension from the sFile variable
 
Upvote 0
Hi Special-K99, thank you for this.

I used the message box, and as you said it did show the filename.csv.

However upon changing the line to Open sFile & "-" & lIncr For Output As #iFile the file still saves with the incremental number after the .csv

Many thanks and kind regards

Chris
 
Upvote 0
You need to remove the .csv from the variable sFile before the "Open sFile..."
Maybe

Code:
sFile=Left(sFile,Len(sFile)-4)
Open sFile & "-" & lIncr For Output As #iFile
 
Upvote 0
Hi @Special-K99, thank you for coming back to me with this and my apologies for no coming back to you sooner.

Unfortunately, this didn't work and ended up creating files, both with and without the incremental, in addition to some strange file structures.

I just wanted to let you know that I have now abandoned this script and have started from scratch, a more simplified example I found which I'm now adapting.

Many thanks and kind regards

Chris
 
Upvote 0

Forum statistics

Threads
1,215,374
Messages
6,124,574
Members
449,173
Latest member
Kon123

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top