+ Reply to Thread
Results 1 to 3 of 3

Importing only new csv. files from folder.

  1. #1
    Registered User
    Join Date
    04-25-2020
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    7

    Importing only new csv. files from folder.

    I’m working on a excel workbook that imports new csv. files and names the new sheet similar to the new file. I found a code that does the trick, however when running the macro multiple times, it will import not only the new files but all files in the folder and therefore duplicating the already imported files. Can you help me with changing the code so that only the new files are imported.


    ub Macro10()
    Dim MyPath As String
    Dim FilesInPath As String
    Dim MyFiles() As String
    Dim SourceRcount As Long
    Dim Fnum As Long
    Dim mybook As Workbook
    Dim basebook As Workbook

    MyPath = "C:\Users\"

    If Right(MyPath, 1) <> "\" Then
    MyPath = MyPath & "\"
    End If

    FilesInPath = Dir(MyPath & "*.csv")
    If FilesInPath = "" Then
    MsgBox "No files found"
    Exit Sub
    End If

    On Error GoTo CleanUp

    Application.ScreenUpdating = False
    Set basebook = ThisWorkbook

    Fnum = 0
    Do While FilesInPath <> ""
    Fnum = Fnum + 1
    ReDim Preserve MyFiles(1 To Fnum)
    MyFiles(Fnum) = FilesInPath
    FilesInPath = Dir()
    Loop

    If Fnum > 0 Then
    For Fnum = LBound(MyFiles) To UBound(MyFiles)
    Set mybook = Workbooks.Open(MyPath & MyFiles(Fnum))
    mybook.Worksheets(1).Copy after:= _
    basebook.Sheets(basebook.Sheets.Count)

    On Error Resume Next
    ActiveSheet.Name = mybook.Name
    On Error GoTo 0

    mybook.Close savechanges:=False
    Next Fnum
    End If
    CleanUp:
    Application.ScreenUpdating = True
    End Sub

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Importing only new csv. files from folder.

    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    04-25-2020
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    7

    Re: Importing only new csv. files from folder.

    Dear AlphaFrog

    Thank you very mutch, it works perfectly

+ 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. [SOLVED] Power Query error when importing files from a folder
    By Howardc1001 in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 05-02-2019, 10:28 AM
  2. Replies: 1
    Last Post: 01-28-2019, 06:55 PM
  3. Importing txt files from folder into one cell only
    By RomesFall in forum Excel General
    Replies: 1
    Last Post: 02-28-2015, 08:41 PM
  4. [SOLVED] automatically importing data from all files in a folder
    By alexcrofut in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 02-10-2014, 03:47 PM
  5. [SOLVED] Importing All Files in a Folder
    By Nolanar in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-20-2012, 01:35 PM
  6. Importing multiple data files from one folder into one workbook
    By ArcticC in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-25-2011, 12:56 PM
  7. [SOLVED] Importing all the .txt files of a folder in a same WB but in different WSheets
    By samuel.chausse@laposte.net in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-27-2006, 12:40 PM

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