Sub GetData()
Dim varInput() As Variant, lngStart As Long, i As Integer
Application.StatusBar = "Select CSV files to stitch together..."
varInput = Application.GetOpenFilename("All files (*.*), *.*", 1, "Please select files to stitch...", "Select", True)
lngStart = Application.WorksheetFunction.CountA(Columns("A:A"))
For i = LBound(varInput) To UBound(varInput)
Application.ActiveSheet.Cells(lngStart + i, 1) = varInput(i)
Next i
Range("A" & LBound(varInput) + lngStart & ":A" & UBound(varInput) + lngStart).Select
Application.ActiveSheet.Unprotect
Selection.Sort Key1:=Range("A" & LBound(varInput) + lngStart), Order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
Application.ActiveSheet.Protect
Range("B1").Select
Application.StatusBar = False
End Sub
Sub PutData()
Dim strOutput As String, lngStart As Long, i As Integer
lngStart = Application.WorksheetFunction.CountA(Columns("A:A"))
If lngStart > 1 Then
Application.StatusBar = "Specify output CSV file..."
strOutput = Application.GetSaveAsFilename(vbNullString, "CSV files (*.csv), *.csv", 1, "Please specify output file...", "Save")
If Application.ActiveSheet.Range("A:A").Find(strOutput, LookIn:=xlValues) Is Nothing Then
Dim fs, fo, fi
Dim booGo As Boolean
Set fs = CreateObject("Scripting.FileSystemObject")
If fs.fileexists(strOutput) Then
booGo = (MsgBox("This will overwrite the existing file. Are you sure you wish to proceed?", vbYesNo + vbDefaultButton2 + vbQuestion, "Confirm") = vbYes)
Else
booGo = True
End If
If booGo Then
For i = 2 To lngStart
If i = 2 Then
Set fo = fs.opentextfile(strOutput, 2, True, tristatefalse)
Else
Set fo = fs.opentextfile(strOutput, 8, True, tristatefalse)
End If
Set fi = fs.opentextfile(Application.ActiveSheet.Cells(i, 1), 1, False, tristatefalse)
Do While fi.AtEndOfStream <> True
fo.writeline (fi.readline)
Loop
fi.Close
fo.Close
Application.StatusBar = "Stitched " & i - 1 & "/" & lngStart - 1 & " files..."
Next i
Application.StatusBar = "Complete"
MsgBox "Stitched " & lngStart - 1 & " files successfully." & vbCrLf & vbCrLf & "Please sanity-check the composite file.", vbInformation, "Complete"
Else
Application.StatusBar = "Cancelling..."
MsgBox "File 'stitch' cancelled by user operation.", vbInformation, "Cancelled"
End If
Else
Application.StatusBar = "Error in output file specified..."
MsgBox "Sorry, you can't output to a file that's on the input list.", vbExclamation, "Error"
End If
Else
Application.StatusBar = "Error in input files specified..."
MsgBox "Sorry, no input files have been specified.", vbExclamation, "Error"
End If
Range("C1").Select
Application.StatusBar = False
End Sub