Merging multiple files into one file

Edberg

New Member
Joined
Feb 15, 2017
Messages
2
I am using the following code which I found in google and its working good if I open blank sheet1 workbook, open the macro, paste this code and run it.
But its not working if I save this macro as add-in and open another excel, run this add-in.
I guess there is something to do with ThisWorkbook function, any suggestion please. Thank You.
Meanwhile I am saving this code in "ThisWorkbook" of VBAproject.

Code I am using:

Code:
Option Explicit

Sub Consolidate()


Dim fName As String, fPath As String, fPathDone As String
Dim LR As Long, NR As Long
Dim wbData As Workbook, wsMaster As Worksheet


'Setup
    Application.ScreenUpdating = False  'speed up macro execution
    Application.EnableEvents = False    'turn off other macros for now
    Application.DisplayAlerts = False   'turn off system messages for now
    
    Set wsMaster = ThisWorkbook.Sheets("Sheet1")    'sheet report is built into


With wsMaster
    If MsgBox("Press Yes to Clear data in the current Sheet or Press No to Exit", vbYesNo) = vbYes Then
        .UsedRange.Offset(1).EntireRow.Clear
        NR = 2
    Else
        Exit Sub
    End If


'Path and filename (edit this section to suit)
    fPath = "C:\Users"
    
    'remember final \ in this string
    fPathDone = fPath & "Imported\"     'remember final \ in this string
    On Error Resume Next
        MkDir fPathDone                 'creates the completed folder if missing
    On Error GoTo 0
    fName = Dir(fPath & "*.csv*")        'listing of desired files, edit filter as desired


'Import a sheet from found files
    Do While Len(fName) > 0
        If fName <> ThisWorkbook.Name Then              'don't reopen this file accidentally
            Set wbData = Workbooks.Open(fPath & fName)  'Open file


        'This is the section to customize, replace with your own action code as needed
            LR = Range("A" & Rows.Count).End(xlUp).Row  'Find last row
            Range("A2:A" & LR).EntireRow.Copy .Range("A" & NR)
            wbData.Close False                                'close file
            NR = .Range("A" & .Rows.Count).End(xlUp).Row + 1  'Next row
            Name fPath & fName As fPathDone & Format(Now, "yyyy-mm-dd h-mm-ss") & "_" & fName           'move file to IMPORTED folder
        End If
        fName = Dir                                       'ready next filename
    Loop
End With


Dim FSO As Object
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FileExists("C:\Users\Final.csv") = True Then
Name fPath & "Append\Final" & ".csv" As fPath & "Append\Final" & "_" & Format(Now, "yyyy-mm-dd h-mm-ss") & ".csv"
End If


ActiveWorkbook.SaveAs Filename:=fPath & "Append\Final" & ".csv", _
                    FileFormat:=xlCSV, CreateBackup:=False
                ActiveWorkbook.Close False
                
                
ErrorExit:    'Cleanup
    
    Application.DisplayAlerts = True         'turn system alerts back on
    Application.EnableEvents = True          'turn other macros back on
    Application.ScreenUpdating = True        'refreshes the screen
End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

Forum statistics

Threads
1,221,444
Messages
6,159,914
Members
451,603
Latest member
SWahl

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top