Hi All:<?xml:namespace prefix = o ns = "urn:schemas-microsoft-comfficeffice" /><o></o>
<o></o>
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></o>
<o></o>
Here is the code i am using to consolidate files in the user selected folder:<o></o>
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></o>
<o></o>
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 and you have any suggestions I would definately APPRECIATE your input
THANKS,
Mark<o></o>
<o></o>
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></o>
<o></o>
Here is the code i am using to consolidate files in the user selected folder:<o></o>
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></o>
<o></o>
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 and you have any suggestions I would definately APPRECIATE your input
THANKS,
Mark<o></o>