I need to save a selection of .csv files, under the same name, as tab delimited files.
I have the following code to select the files, specify the delimiter, and then iterate through the files, opening and saving them one by one.
The issue is the iteration.
Sub sandbox2()
Dim xFilesToOpen As Variant
Dim i As Integer
Dim xWb As Workbook
Dim xTempWb As Workbook
Dim xDelimiter As String
Dim xScreen As Boolean
Dim strFileFullName As String
On Error GoTo ErrHandler
xScreen = Application.ScreenUpdating
Application.ScreenUpdating = False
xFilesToOpen = Application.GetOpenFilename("Text Files (*.*), *.*", , , , True)
xDelimiter = Application.InputBox("Delimiter: ", , ",")
If TypeName(xFilesToOpen) = "Boolean" Then
MsgBox "No files were selected", , "Sorry dude"
GoTo ExitHandler
End If
For i = 1 To UBound(xFilesToOpen)
Set xTempWb = Workbooks.Open(xFilesToOpen(i))
strFileFullName = Replace(ActiveWorkbook.FullName, ".csv", "")
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:=strFileFullName + ".txt", FileFormat:=xlText, CreateBackup:=False
xTempWb.Close False
Application.DisplayAlerts = True
ActiveWorkbook.Close savechanges:=False
Set xTempWb = Nothing
Next i
ExitHandler:
Application.ScreenUpdating = xScreen
Set xWb = Nothing
Set xTempWb = Nothing
Exit Sub
ErrHandler:
MsgBox Err.Description, , "error"
Resume ExitHandler
End Sub
This code will work for the first file, but then I get the error
"Object variable or With block variable not set"
When looking at the folder, the first file is saved as a .txt file, but only the first.