Thingol
Technical User
- Jan 2, 2002
- 169
Hi All,
I'm trying to create a batch-conversion script that converts a set of text files to a set of excel workbooks. I've currently made a script that I thought would do the trick, but upon saving I get the error message:
[highlight]Runtime error 13: Type mismatch[/highlight]
Below is the script I'm using (the line at which the error occurs is [highlight]highlighted[/highlight]):
Any help would be greatly appreciated!
Best regards,
Martijn Senden.
In the Beginning there was nothing, which exploded.
--Terry Pratchett, Lords and Ladies--
I'm trying to create a batch-conversion script that converts a set of text files to a set of excel workbooks. I've currently made a script that I thought would do the trick, but upon saving I get the error message:
[highlight]Runtime error 13: Type mismatch[/highlight]
Below is the script I'm using (the line at which the error occurs is [highlight]highlighted[/highlight]):
Any help would be greatly appreciated!
Best regards,
Martijn Senden.
Code:
Option Explicit
Sub Text_file_convert()
Dim Title As String
Dim Filter As String, Title As String, msg As String
Dim i As Integer, FilterIndex As Integer
Dim FileName As Variant
Dim Path As String
Dim Drive As String
Dim msg As String
' File filters
Filter = "Text Files (*.txt),*.txt,"
' Set default filter to *.txt
FilterIndex = 1
' Set the title of the dialog
Title = "Choose the files you want to open"
' Choose the drive and path
Path = ThisWorkbook.Path
Drive = Left(Path, 1)
ChDrive (Drive)
ChDir (Path)
With Application
' Set the array of file names to the selected filenames (allow multiple)
FileName = .GetOpenFilename(Filter, FilterIndex, Title, , True)
' Reset the initial drive / path
ChDrive (Left(.DefaultFilePath, 1))
ChDir (.DefaultFilePath)
End With
' Exit when cancelled
If Not IsArray(FileName) Then
MsgBox "No file was selected."
Exit Sub
End If
' Open files
For i = LBound(FileName) To UBound(FileName)
msg = msg & FileName(i) & vbCrLf
Workbooks.OpenText Filename:=FileName(i), DataType:=xlDelimited, tab:=True
[highlight]Workbooks(FileName).SaveAs FileFormat:=xlWorkbookNormal[/highlight]
Workbooks(FileName).Close
Next i
MsgBox msg, vbInformation, "Converted " & i & " files"
End Sub
In the Beginning there was nothing, which exploded.
--Terry Pratchett, Lords and Ladies--