Hi,
I found this wonderful code on the internet and it opens a group of CSV files in a temp folder, copies the date into a worksheet in a workbook. Now I would like to delete the csv files from the temp folder however I'm getting a "Permissions Denied" error on my Kill coding. After researching I confirmed that I do have access because I was able to manually navigate to the temp download folder and delete the csv files. No problem. I did have the "Kill" code after the "End If", could that be the problem?
So I wondering if someone could help me add to the code to check to see if the csv files are open then close them then delete them.
Any help, suggestions, or examples would be greatly appreciated!
Thank you, Bevg
Here is the code:
Sub ImportMultipleCSV()
Dim myfiles
Dim i As Integer
myfiles = Application.GetOpenFilename(filefilter:="Report (*.csv), *.csv", MultiSelect:=True)
If IsArray(myfiles) Then
For i = LBound(myfiles) To UBound(myfiles)
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & myfiles(i), Destination:=Range("A" & Rows.Count).End(xlUp).Offset(1, 0))
.Name = "Sample"
.FieldNames = False
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 2
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileOtherDelimiter = ","
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Next i
Else
MsgBox "No File Selected"
End If
Dim xConnect As Object
For Each xConnect In ActiveWorkbook.Connections
If xConnect.Name <> "ThisWorkbookDataModel" Then xConnect.Delete
Next xConnect
End Sub
Bookmarks