Consolidate Files Code requires AMENDING. Help PLEASE

Mister H

Well-known Member
Joined
Mar 6, 2002
Messages
1,507
Hi All:<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
I have been trying to alter this code to fit my requirements and I am almost there (with the help of this board and Google, THANKS)<o:p></o:p>
<o:p></o:p>
Here is the code i am using to consolidate files in the user selected folder:<o:p></o:p>
Code:
Sub ConsolidateFiles()
'Summary:    Merge files in a specific folder into one master sheet (stacked)
'            Moves imported files into another folder
Dim fname As String, fPath As String, fPathDone As String
Dim LR As Long, NR As Long
Dim wbData As Workbook, wsMaster As Worksheet
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.DisplayAlerts = False
    
    Sheets("Master").Visible = True
    Set wsMaster = ThisWorkbook.Sheets("Master")
With wsMaster
        NR = 1
        NR = .Range("A" & .Rows.Count).End(xlUp).Row
  
    MsgBox "Please select the folder that contains the Excel Files that you want to Consolidate"
    Do
        With Application.FileDialog(msoFileDialogFolderPicker)
            .AllowMultiSelect = False
            .Show
            If .SelectedItems.Count > 0 Then
                fPath = .SelectedItems(1) & "\"
                Exit Do
            Else
                If MsgBox("No folder chosen, do you wish to abort?", _
                    vbYesNo) = vbYes Then GoTo myNext
            End If
        End With
    Loop
    
    fPathDone = fPath & "Files That HAVE BEEN Consolidated\"
    On Error Resume Next
        MkDir fPathDone
    On Error GoTo 0
    fname = Dir(fPath & "*.xls*")
'Import a sheet from found files
    Do While Len(fname) > 0
        If fname <> ThisWorkbook.Name Then
            Set wbData = Workbooks.Open(fPath & fname)
      
            LR = Range("A" & Rows.Count).End(xlUp).Row
            If NR = 1 Then
                Range("A1:A" & LR).EntireRow.Copy .Range("A" & NR)
            Else
                Range("A2:A" & LR).EntireRow.Copy .Range("A" & NR)
            End If
            
            wbData.Close False
            NR = .Range("A" & .Rows.Count).End(xlUp).Row + 1
            Name fPath & fname As fPathDone & fname
            
'Move file to folder titled:  Files That HAVE BEEN Consolidated
    fname = Dir
        End If
    Loop
End With
ErrorExit:
    Application.DisplayAlerts = True
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    
Sheets("Master").Select
        
    MsgBox "Your Excel files have now been consolidated into one file." & vbNewLine & vbNewLine & "All of the original files have been moved to the folder titled:  Files That HAVE BEEN Consolidated" & vbNewLine & vbNewLine & "Folder Location is:" & vbNewLine & fPath & vbNewLine & vbNewLine & "If the Original Files are no longer required please delete them or move them to the appropriate folder.", vbInformation

'Call up the Sort Option at the users discretion
    Cells.Select
    Cells.EntireColumn.AutoFit
    Selection.Rows.AutoFit
If MsgBox("Do you want to sort the data that has been compiled?", vbQuestion + vbYesNo) = vbNo Then Exit Sub
'Application.Dialogs(xlDialogSort).Show 'sort with No Column Headers
Application.Dialogs(xlDialogSort).Show arg1:=xlTopToBottom, arg8:=xlYes 'sort with Column Headers
Range("A1").Select
Call CopyMasterSheetAsNewFile
myNext:
    Application.DisplayAlerts = True
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub

The problem I am having is that I want this code to be generic so that it can be used on differently formatted sheets. HOWEVER I want it to only import the sheet if it is the same name (or structure) as the first sheet that gets imported. Currently this code will import whatever sheet is active when it open the file. <o:p></o:p>
<o:p></o:p>
For example if I have 2 files containing 2 sheets (one titled Data and the other titled Data2) then when I run the code I was hoping that Excel can make note of the first sheet it imports and then goes through the rest of the files and only imports sheets with the same name. So if it opens file 1 and imports the sheet titled Data I want it to open file 2 and import the sheet titled Data as well. I HOPE I am making sense? Right now if the sheet titled Data2 was active when the file was closed that is the sheet it will import which then corrupts the consolidated data because now I have Data ad Data2 consolidated.


IF you are still reading :confused: and you have any suggestions I would definately APPRECIATE your input :biggrin:

THANKS,
Mark<o:p></o:p>
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

Forum statistics

Threads
1,224,506
Messages
6,179,159
Members
452,892
Latest member
yadavagiri

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