Hi All
I have a series of .csv files which I need import and save as .xlsx files. The .csv files over time will change with deletions and additions and the files getting additional data periodically. I have recorded and utilised existing code to cycle through the list of .csv files in the directory to import the data then save as an .xlsx file:
Is this the most efficient way of performing this task?
What I can't manage to do is check to see if the .xlsx file exists before proceeding with the import and file creation. If the .xlsx file exists I want the file skipped and move onto the next .csv file in the CSV directory.
The file names for both are identical except for the .csv and .xlsx.
Here is my current test effort which is failing miserably to perform the cross check and output the resulting test as a MsgBox:
I just can't seem to get it to work. Does anyone have any pointers to get me on the right track?
Cheers
Shaun
I have a series of .csv files which I need import and save as .xlsx files. The .csv files over time will change with deletions and additions and the files getting additional data periodically. I have recorded and utilised existing code to cycle through the list of .csv files in the directory to import the data then save as an .xlsx file:
Code:
Sub LoopThroughFiles()
Dim StrFile As String
Dim FileName As String
Application.ScreenUpdating = False
StrFile = Dir("c:\Data\CSV\*")
Do While Len(StrFile) > 0
Workbooks.Add
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Data\CSV\" & StrFile, Destination:=Range("$A$1"))
.Name = StrFile
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = True
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(4, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
FileName = Left(StrFile, Len(StrFile) - 4)
ActiveWorkbook.SaveAs FileName:="c:\Data\Excel\" & FileName & ".xlsx", _
FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
Workbooks(FileName & ".xlsx").Close
Debug.Print StrFile
StrFile = Dir
Loop
Application.ScreenUpdating = True
End Sub
Is this the most efficient way of performing this task?
What I can't manage to do is check to see if the .xlsx file exists before proceeding with the import and file creation. If the .xlsx file exists I want the file skipped and move onto the next .csv file in the CSV directory.
The file names for both are identical except for the .csv and .xlsx.
Here is my current test effort which is failing miserably to perform the cross check and output the resulting test as a MsgBox:
Code:
Sub CheckFileExists()
Dim StrFile As String
Dim ChkFile As String
Dim ChkFilePath As String
StrFile = Dir("c:\Data\CSV\*")
ChkFilePath = "c:\Data\Excel\"
Do While Len(StrFile) > 0
ChkFile = Left(StrFile, Len(StrFile) - 4) & ".xlsm"
Debug.Print ChkFilePath; ChkFile
' If Dir(ChkFilePath & ChkFile) = vbNullString Then
' MsgBox "File Exists"
' Else
' MsgBox "File Does Not Exist"
' End If
Debug.Print StrFile
StrFile = Dir
'ChkFile = Left(StrFile, Len(StrFile) - 4) & ".xlsm"
Loop
End Sub
I just can't seem to get it to work. Does anyone have any pointers to get me on the right track?
Cheers
Shaun