• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Compiling Data into 1 sheet from two different worksheet

Ateeb Ali

Member
Dear Sir
Attached are three files;

1. Chandoo1, it has data from row A12:L3069, it also have blank spaces
2. Chandoo2, it also has data from row A12:L3069, it also have blank spaces
3. Master fIle, Here I want to auto compile both data and show in serial order with respect to TCS# Blank rows should be ignored or should show in end after valid data
 

Attachments

  • Chandoo1.xlsx
    806.1 KB · Views: 3
  • Chandoo2.xlsx
    806 KB · Views: 3
  • Master File.xlsx
    806.3 KB · Views: 2
Do you have a code that you have written or tried? This is something very basic and available all over the internet. A Google search would not do any harm instead of waiting for others to do your work.. :)

Ron de Bruin have some excellent variants that you can start with and customize to your need.

https://www.rondebruin.nl/win/s3/win008.htm

If you get stuck somewhere, we are all happy to help.
 
Dear Sir
Yes I have tried to read all but unable to write any code still thats why need help in this forum

It may be basic but for me, I am still failed to get it through.
 
One thing to add that the workbook have several sheets.
I need to compile just one sheet of each work book, sheet named in both workbopok i.e chandoo1 & chandoo2 is as "Test"

It means chandoo1.xlsb!test
It means chandoo2.xlsb!test
Need to compile data in master file of both these sheets
 
this code not working, also I dont want to write manual path, The master in same folder where chandoo1 and chandoo 2 exist, there will be no other file in this folder, so the path should be "thisworkbook.path"
Code:
Option Explicit

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

    'Fill in the path\folder where the files are
    MyPath = "C:\Users\ltpurc08\Desktop\Thread Consumption Software\Final Files Share Folder"

    'Add a slash at the end if the user forget it
    If Right(MyPath, 1) <> "\" Then
        MyPath = MyPath & "\"
    End If

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

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

    'Change ScreenUpdating, Calculation and EnableEvents
    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 array(myFiles)
    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

                With mybook.Worksheets(1)
                    Set sourceRange = .Range("A1:C1")
                End With

                If Err.Number > 0 Then
                    Err.Clear
                    Set sourceRange = Nothing
                Else
                    'if SourceRange use 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 "Sorry there are not enough rows in the sheet"
                        BaseWks.Columns.AutoFit
                        mybook.Close savechanges:=False
                        GoTo ExitTheSub
                    Else

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

                        'Set the destrange
                        Set destrange = BaseWks.Range("B" & rnum)

                        'we copy the values from the sourceRange to the destrange
                        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 ScreenUpdating, Calculation and EnableEvents
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .Calculation = CalcMode
    End With
End Sub
 
Try this:
Code:
Sub AppendData()
    Dim sPath As String, fName As String
    Dim dWBook As Workbook, sWBook As Workbook
    Dim lRow As Long

    With Application
        .ScreenUpdating = False
        .DisplayAlerts = False
    End With
   
    Set dWBook = ThisWorkbook
    sPath = dWBook.Path & Application.PathSeparator
    fName = Dir("*.xlsx*")
   
    Do While fName <> ""
        lRow = dWBook.Sheets("Chandoo1").Cells(Rows.Count, 1).End(xlUp).Row
        If dWBook.FullName = sPath & fName Then GoTo NextFile
        Set sWBook = Workbooks.Open(sPath & fName)
        With sWBook
            .Sheets("Test").Range("A9:L3069").Copy dWBook.Sheets("Chandoo1").Cells(lRow + 1, 1) '.PasteSpecial xlPasteValues
            lRow = dWBook.Sheets("Chandoo1").Cells(Rows.Count, 1).End(xlUp).Row
            .Close SaveChanges:=False
        End With
NextFile: fName = Dir
    Loop
   
    With dWBook.Sheets("Chandoo1")
        lRow = .Cells(Rows.Count, 1).End(xlUp).Row
        Range("A9:L" & lRow).Sort key1:=Range("A9:A" & lRow), _
        order1:=xlAscending, Header:=xlNo
    End With
   
    With Application
        .ScreenUpdating = True
        .DisplayAlerts = True
    End With
End Sub
 
Boss, it is giving this debug error
Run Time error 9
subscript out or range
Code:
lRow = dWBook.Sheets("Chandoo1").Cells(Rows.Count, 1).End(xlUp).Row
 
Sir, I have tried all but its not working, now giving this error
File not found
Code:
        Set sWBook = Workbooks.Open(sPath & fName)
 
Code is working fine for me on the sample files you have provided.

Questions:-
1. Where have you placed the code in? Module or any specific sheet?
2. You have provided sample files in .xlsx format and in one of your comments you say they are .xlsb format
"It means chandoo1.xlsb!test
It means chandoo2.xlsb!test"​
3. What is the file format of your source files? .xlsx or .xlsb
4. Will there be multiple format of source files or just one? .xls, .xlsx, .xlsb, .xlsm etc.

Thanks/Ajesh
 
Back
Top