Results 1 to 14 of 14

Thread: Solved: Advanced consolidation help!!

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #7
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,645
    hi

    i tested on sample workbooks and i think below procedure did the trick.
    i recommend you do the same and first test the code on sample files.

    macro file is attached. opens a file named "Consolidated Reports.xls" which is already created.

    [VBA]
    Sub consWBs()
    'http://vbaexpress.com/forum/showthread.php?t=39367
    'requires a reference to Microsoft Scripting Runtime

    Dim fso As Object, fsoFolder As Object, fsoSubfolder As Object
    Dim wbMaster As Workbook, wbData As Workbook, wsMaster As Worksheet
    Dim folderPath As String, subfolderName As String, wbMasterName As String
    Dim fName As String, fPath As String, fPathDone As String
    Dim LR As Long, NR As Long


    With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .DisplayAlerts = False
    End With

    Set fso = CreateObject("Scripting.FileSystemObject")
    folderPath = "C:\Data\"
    Set fsoFolder = fso.GetFolder(folderPath)

    wbMasterName = "Consolidated Reports.xls"
    If IsWbOpen(wbMasterName) Then
    Set wbMaster = Workbooks(wbMasterName)
    Else
    Set wbMaster = Workbooks.Open(folderPath & wbMasterName)
    End If

    With wbMaster
    For Each fsoSubfolder In fsoFolder.SubFolders
    subfolderName = fsoSubfolder.Name
    Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = subfolderName
    Set wsMaster = ActiveSheet
    With wsMaster
    If MsgBox("Clear the old data first?", vbYesNo) = vbYes Then
    .Cells.Clear
    NR = 1
    Else
    NR = .Range("A" & .Rows.Count).End(xlUp).Row + 1 'appends data to existing data
    End If
    'Path and filename (edit this section to suit)
    fPath = folderPath & subfolderName & "\" 'remember final \ in this string
    fPathDone = fPath & "\Imported\" 'remember final \ in this string

    If Len(Dir(fPathDone, vbDirectory)) = 0 Then
    MkDir fPathDone
    End If

    fName = Dir(fPath & "*.xls*") '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
    If NR = 1 Then 'copy the data AND titles
    Range("A1:A" & LR).EntireRow.Copy .Range("A" & NR)
    Else 'copy the data only
    Range("A2:A" & LR).EntireRow.Copy .Range("A" & NR)
    End If
    wbData.Close False 'close file
    NR = .Range("A" & .Rows.Count).End(xlUp).Row + 1 'Next row
    Name fPath & fName As fPathDone & "\" & fName 'move file to IMPORTED folder
    fName = Dir 'ready next filename
    End If
    Loop
    End With
    Next
    End With

    With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .DisplayAlerts = True
    End With

    End Sub


    Function IsWbOpen(wbName As String) As Boolean
    'http://www.vbaexpress.com/kb/getarticle.php?kb_id=443

    Dim i As Long
    For i = Workbooks.Count To 1 Step -1
    If Workbooks(i).Name = wbName Then Exit For
    Next
    If i <> 0 Then IsWbOpen = True

    End Function

    [/VBA]
    Attached Files Attached Files
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •