+ Reply to Thread
Results 1 to 3 of 3

Macro to extract file names when importing multiple files

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2024
    Posts
    2,888

    Macro to extract file names when importing multiple files

    I have a macro to import several files in Col A:H

    I would like the macro amended so as to extract the file names in Col I in the destination file for each of the files imported

    Your assistance in this regard is most appreciated




     Sub Open_MultipleFiles()
    ChDir "C:\Extract"
    Dim lr As Long
    
    With Sheets("Imported Data")
    lr = .Cells(.Rows.Count, "A").End(xlUp).Row
    Range("A2:J" & lr).ClearContents
    
    Dim fDialog As Object, varFile As Variant
    Dim nb As Workbook, tw As Workbook, ts As Worksheet
    With Application
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
        .CutCopyMode = False
    End With
    Set tw = ThisWorkbook
    Set ts = tw.ActiveSheet
    Set fDialog = Application.FileDialog(3)
    ChDir "C:\extract"
    With fDialog
      .Filters.Clear
      .Filters.Add "Excel files", "*.csv*"
       .Show
       
       For Each varFile In .SelectedItems
          Set nb = Workbooks.Open(Filename:=varFile, local:=True)
          
         With Sheets(1)
       .Range("A1:H500").Copy
        ThisWorkbook.Sheets("Imported Data").Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
        ThisWorkbook.Sheets("Imported Data").Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteFormats
    
    End With
          
            
            nb.Close False
       Next
    End With
    With Sheets("Imported Data")
    .Range("A1").EntireRow.Delete
    .Range("A:H").EntireColumn.AutoFit
    
      End With
      
    
    With Application
        .ScreenUpdating = True
        .Calculation = xlCalculationAutomatic
        .CutCopyMode = True
    End With
    
     End With
    ChDir "C:\my documents"
    
    End Sub

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: Macro to extract file names when importing multiple files

    try change
        ThisWorkbook.Sheets("Imported Data").Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteFormats
    to
        with ThisWorkbook.Sheets("Imported Data").Range("A" & Rows.Count).End(xlUp).Offset(1)
            .PasteSpecial xlPasteFormats
            .range("i1").value = varFile
        end with

  3. #3
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2024
    Posts
    2,888

    Re: Macro to extract file names when importing multiple files

    Many Thanks for the help, Code 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] Importing data from multiple files changing file names
    By rhodestransit in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 12-18-2020, 10:52 AM
  2. Importing multiple .csv files into one new excel file - problem with sheet names
    By jts5814 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-20-2016, 12:14 PM
  3. [SOLVED] Importing multiple .csv files into one Excel file.
    By cody.starr in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-05-2013, 03:36 AM
  4. [SOLVED] Importing multiple .csv files into one Excel file.
    By cody.starr in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-04-2013, 06:14 PM
  5. Replies: 0
    Last Post: 11-27-2012, 01:43 PM
  6. Importing data from multiple files to new file
    By agengler11 in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 11-11-2012, 11:38 AM
  7. [SOLVED] Importing multiple .del files into .csv file
    By Tomsnape in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-21-2012, 08:53 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