Results 1 to 7 of 7

Problem using Name As to move files after reading CSV Data into a WorkSheet

Threaded View

  1. #1
    Registered User
    Join Date
    03-02-2013
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    5

    Problem using Name As to move files after reading CSV Data into a WorkSheet

    Hi,

    Just learning VBA and have pieced together this subroutine from various sources that did similar things but not quite what I wanted. Any help appreciated. The sub reads all *.csv files in a Folder and places the contents into a single Worksheet, one below the other. That bit works great.

    Then I want to move each file that has been read to a new Directory so it still exists but will not be imported again when the macro is run next.

    I cannot get the Name As section of the code to work. The Macro always stops with Run Time Error 75 - Path/File access error. I have tried simple Path & File names (as can be seen commented out in the code) and it still does not work. I tried using FileCopy and that works no problem but that doesn't move the files.

    I'm using Excel 2010 on Win7 and when I copy files myself in Explorer, I have to "provide administrator permission" to move & delete files etc.
    The csv files are exported from another program that is run "as Administrator".

    Here is the code, many thanks for any help ...
    Sub ImportTest()
    '
    Dim wbCSV   As Workbook
    Dim wsTradeData  As Worksheet: Set wsTradeData = ThisWorkbook.Sheets("Trade Data")
    Dim fName As String
    Dim file As String
    Dim fPath As String
    Dim NewPath As String
    
    'fPath = "C:\Program Files (x86)\MT4 - GoTrader 1\experts\files\"
    'NewPath = "C:\Users\Brian\Desktop\Trade Journals\Trade Files\"
    fPath = "C:\"
    NewPath = "C:\Users\Brian"
    file = "*.csv"
    
    'Stop Screen Flickering & speed up macro
    Application.ScreenUpdating = False
    
    'Finds the name of the first file of type csv in the current directory
    fName = Dir(fPath & file)
    
        Do While Len(fName) > 0
            'open a new Workbook for the CSV file
            Set wbCSV = Workbooks.Open(fPath & fName)
            
            With ActiveSheet
                .Range("A2:AZ" & Range("A" & Rows.Count).End(xlUp).Row).Copy wsTradeData.Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
            End With
            
            'close the CSV Workbook without saving
            wbCSV.Close False
            
            Name fPath & fName As NewPath & fName
            'FileCopy fPath & fName, NewPath & fName 'This code works but only copies
            
            'get next CSV file Name
            fName = Dir()
        Loop
     
    Application.ScreenUpdating = True
    End Sub
    rgds mcdirt
    Last edited by JosephP; 03-04-2013 at 11:26 AM. Reason: added code tags-please use them in future

Thread Information

Users Browsing this Thread

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

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