+ Reply to Thread
Results 1 to 5 of 5

Add close and delete statement whenusing a array to import csv files into one worksheet

  1. #1
    Registered User
    Join Date
    12-11-2012
    Location
    Seattle
    MS-Off Ver
    Excel 2007
    Posts
    51

    Add close and delete statement whenusing a array to import csv files into one worksheet

    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

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Add close and delete statement whenusing a array to import csv files into one workshee

    Hi there,

    Does the following addition to your code do what you need? It seems to work at my end.

    Please Login or Register  to view this content.

    Hope this helps - please let me know how you get on.

    Regards,

    Greg M

  3. #3
    Registered User
    Join Date
    12-11-2012
    Location
    Seattle
    MS-Off Ver
    Excel 2007
    Posts
    51

    Re: Add close and delete statement whenusing a array to import csv files into one workshee

    Thank you for your advice.

    I inserted the Kill code at the location suggested and only one file appeared when four use to appear in the separate worksheet. Only one file was deleted and I'm receiving a Run-time error '13': Type mismatch error

    Then I moved the code after the "End If" statement and I received the Run-time error 9 Subscript out of range

    and the Kill PathName:=myfiles(i) code is highlighted in yellow.

    I tried the code in different variations
    Kill Myfiles(i)
    Kill myfiles
    Kill C:\Users\folder\AppData\Local\Packages\folder\TempState\Downloads\*.*

    and still receive the same error. This is frustrating

  4. #4
    Registered User
    Join Date
    12-11-2012
    Location
    Seattle
    MS-Off Ver
    Excel 2007
    Posts
    51

    Re: Add close and delete statement whenusing a array to import csv files into one workshee

    Hi,
    I found the solution.
    The Kill code needs to be placed AFTER the "Next xConnect" code and the actual path to the files to be deleted needs to be supplied in order for the code to work correctly

    Kill "C:\Users\folder\folder\folder\folder\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\TempState\Downloads\*.*"
    MsgBox "File Deleted Successfully "

    Bev

  5. #5
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Add close and delete statement whenusing a array to import csv files into one workshee

    Hi again,

    I think you'll find that you can avoid specifying the FullName of each of the files by using:

    Please Login or Register  to view this content.

    Regards,

    Greg M

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Import multiple text files delete rows between lines with File Name
    By coronakyk in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-10-2014, 09:53 PM
  2. [SOLVED] Batch job. Open a lot of excel files, delete the same values from each, save, then close
    By greendale in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 11-06-2013, 03:31 PM
  3. Close Workbook,Rename Worksheet, Delete Worksheet.
    By Ariff_Chowdhury in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-24-2013, 09:21 PM
  4. [SOLVED] Delete XML maps while import xml files
    By capnhud in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-06-2012, 05:23 PM
  5. [SOLVED] Delete Insert Worksheet on Open/Reset on Close
    By Jane in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-10-2006, 09:15 AM
  6. [SOLVED] Import csv files, delete rows, arrange columns ....
    By xadnora in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-11-2005, 12:06 PM
  7. [SOLVED] Best way to import fixed-width delimited text files into an array?
    By KR in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-03-2005, 11:06 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1