-1

It's my first in using VBA, excuse my rustiness.

My Situation: I have a master excel (2010) file to which I need to import excel files from a folder. The existing code works almost fine but does not look into the sub-folders. Along with below important requirements. Please help!

Desired Output:

  1. The code should not create a new excel sheet, instead import the data into the same master excel file that the code sits in.

  2. Currently, the code fetches excel files only from the parent folder. I need it to look into any sub-folders with excel files in them too

  3. The current code changes the destination files format, I would like to keep the destination format as is.

  4. Should not copy empty rows from the defined range

Existing code (From MSDN: Ron de Bruin)

Sub MergeAllWorkbooks()
Dim MyPath As String, FilesInPath As String
Dim MyFiles() As String
Dim SourceRcount As Long, FNum As Long
Dim mybook As Workbook, BaseWks As Worksheet
Dim sourceRange As Range, destrange As Range
Dim rnum As Long, CalcMode As Long

' Change this to the path\folder location of your files.
MyPath = "C:\Users\zatin.dharmapuri\Desktop\Reviews"

' Add a slash at the end of the path if needed.
If Right(MyPath, 1) <> "\" Then
    MyPath = MyPath & "\"
End If

' If there are no Excel files in the folder, exit.
FilesInPath = Dir(MyPath & "*.xl*")
If FilesInPath = "" Then
    MsgBox "No files found"
    Exit Sub
End If

' Fill the myFiles array with the list of Excel files
' in the search folder.
FNum = 0
Do While FilesInPath <> ""
    FNum = FNum + 1
    ReDim Preserve MyFiles(1 To FNum)
    MyFiles(FNum) = FilesInPath
    FilesInPath = Dir()
Loop

' Set various application properties.
With Application
    CalcMode = .Calculation
    .Calculation = xlCalculationManual
    .ScreenUpdating = False
    .EnableEvents = False
End With

' Add a new workbook with one sheet.
Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
rnum = 1

' Loop through all files in the myFiles array.
If FNum > 0 Then
    For FNum = LBound(MyFiles) To UBound(MyFiles)
        Set mybook = Nothing
        On Error Resume Next
        Set mybook = Workbooks.Open(MyPath & MyFiles(FNum))
        On Error GoTo 0

        If Not mybook Is Nothing Then
            On Error Resume Next

            ' Change this range to fit your own needs.
            With mybook.Worksheets(1)
                Set sourceRange = .Range("B2:G50")
            End With

            If Err.Number > 0 Then
                Err.Clear
                Set sourceRange = Nothing
            Else
                ' If source range uses all columns then
                ' skip this file.
                If sourceRange.Columns.Count >= BaseWks.Columns.Count Then
                    Set sourceRange = Nothing
                End If
            End If
            On Error GoTo 0

            If Not sourceRange Is Nothing Then

                SourceRcount = sourceRange.Rows.Count

                If rnum + SourceRcount >= BaseWks.Rows.Count Then
                    MsgBox "There are not enough rows in the target worksheet."
                    BaseWks.Columns.AutoFit
                    mybook.Close savechanges:=False
                    GoTo ExitTheSub
                Else

                    ' Copy the file name.
                    With sourceRange
                        BaseWks.Cells(rnum, "L"). _
                                Resize(.Rows.Count).Value = MyFiles(FNum)
                    End With

                    ' Set the destination range.
                    Set destrange = BaseWks.Range("A" & rnum)

                    ' Copy the values from the source range
                    ' to the destination range.
                    With sourceRange
                        Set destrange = destrange. _
                                        Resize(.Rows.Count, .Columns.Count)
                    End With
                    destrange.Value = sourceRange.Value

                    rnum = rnum + SourceRcount
                End If
            End If
            mybook.Close savechanges:=False
        End If

    Next FNum
    BaseWks.Columns.AutoFit
End If

ExitTheSub: ' Restore the application properties. With Application .ScreenUpdating = True .EnableEvents = True .Calculation = CalcMode End With End Sub

  • 2
    This is a bit too broad for this site, you are practically asking for a ready made code. Try stepping through the code with F8, and see where it does something it shouldn't or where it misses something, and change the code. Come back if you have a more specific question, and we will gladly help. – vacip Jan 19 '17 at 12:03
  • Like I mentioned in my opening line of the question, **It's my first in using VBA, excuse my rustiness.**, I am not a programmer of any sorts and this is my **First** attempt at VBA. I would have done what you suggested only if I had at least a day's experience in it. Hence, I posted to this forum. – Zatin Dharmapuri Jan 19 '17 at 12:15
  • 1
    Google "sub folder search vba" and the top six results are from SO. You will have to chase through duplicates to get the 4 year old answer. – Mark Fitzgerald Jan 19 '17 at 12:23
  • 2
    Zlatin, I understood what you wrote. I'm sorry to say, but it seems that this is not the place for you yet. We don't teach the basics, and we don't do freelance programming for free. Either learn the basics, or hire a professional to do the job for you. If you decide to learn and have a go at it yourself, we will gladly help with any **specific** programming related questions you might have. – vacip Jan 19 '17 at 12:28
  • Possible duplicate of [get list of subdirs in vba](http://stackoverflow.com/questions/9827715/get-list-of-subdirs-in-vba) – Mark Fitzgerald Jan 19 '17 at 12:29
  • I get it. I'll try sorting it out. Thanks! – Zatin Dharmapuri Jan 19 '17 at 12:58

1 Answers1

0

There are several ways to do this. Probably the easiest is to use Ron deBruin's AddIn.

http://www.rondebruin.nl/win/addins/rdbmerge.htm

Check the box that says 'Include Sub Folders'.

enter image description here

ASH
  • 20,759
  • 19
  • 87
  • 200
  • Thanks for the response. I was using Ron deBruin's VBA originally and finally figured my own way out with the necessary modifications I requested here. His Add-in is helpful too but I have a situation that it does not fit well into. Again, I really appreciate your time in trying to help, where initially I was not able to get it answered. I don't know if I can ask you here a related question regarding the modified code that I'm not able to crack it. – Zatin Dharmapuri Jan 30 '17 at 16:44