Macro collating data wrongly

aaravgaba

New Member
Joined
Sep 15, 2013
Messages
24
Hi Genius People,

I have the below code running for a sheet where I am collating data from various sheets.

The macro is running fine but the problems comes when it is pasting the values in the master sheet, where the last rows of the data are empty. Like in the below table the macro will paste po number till a3 but thereafter it will pick the PO number from the other sheet and will paste right under a3 thereby creating conflict in the table. I want that it should paste the data in each row relating to that very sheet. I know it is confusing but I am not able to attach the file to make it more specific. Thanks

Po numberpart numberstatusqty
a11234packed12
a2picked
a311215
1212despatched12
43445received10
3333packed5

<tbody>
</tbody>


Code:
Sub Get_Info_By_Headers()
    Dim sPath As String
    Dim sFil As String
    Dim owb As Workbook
    Dim twb As Workbook
    Dim ch
    Dim j As Long, a As Long
    With Application
        .Calculation = xlCalculationManual
        .EnableEvents = False
        .ScreenUpdating = False
    End With
    ch = Array("po number", "part number", "status", "quantity")
    Set twb = ThisWorkbook
    sPath = "C:\Users\dipak\Desktop\CRASH REPORT\"
    sFil = Dir(sPath & "*.xl*")
    Do While sFil <> "" And sFil <> twb.Name
        Set owb = Workbooks.Open(sPath & sFil)
            With owb.Sheets("data")
                For j = LBound(ch) To UBound(ch)
                    a = .Rows(1).Find(ch(j), , , 1).Column
                    .Range(.Cells(2, a), .Cells(.Cells(.Rows.Count, a).End(xlUp).Row, a)).Copy twb.Sheets("report").Cells(Rows.Count, j + 1).End(xlUp).Offset(1)
                Next j
            End With
        owb.Close False 'Close no save
        sFil = Dir
    Loop


    With Application
        .Calculation = xlAutomatic
        .EnableEvents = True
        .ScreenUpdating = True
    End With
End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
The macro copies the data in each column as it identified in the Find statement. As written, it would not paste anything in column A below the 'a3' entry because there is nothing in the source column below that entry. Can you describe what you want as a result, or post an illustration of it?
 
Upvote 0
Sorry i was not specific. I have many spreadsheets from where the data is taken. So what macro will do is to copy po number from another sheet and will paste po number right after a3 whereas it should leave the two empty cells to maintain the data integrity. So what happens is the final sheet depicts wrong po number against some other part number status and quantity. I want that it should paste the other sheet only after end of active cell of the sheet and not of the column only.
 
Upvote 0
I am reproducing the sheets in below table and the results i am getting and also the result i am anticipating.


first sheet

po numberpart numberstatusquantity
a11234packed12
a2picked
a311215
1212despatched12
43445received10
3333packed5

<tbody>
</tbody>

SECOND SHEET
po numberpart numberstatusquantity
aaaaAARAVPACKED
vvvDEEPAK
dddC
eeeDPACKED5
PACKED5
ghhhPACKED5

<tbody>
</tbody>


THIRD SHEET
po numberpart numberstatusquantity
11ZRECEIVED8
12XRECEIVED8
13CRECEIVED8
14VRECEIVED8
15GRECEIVED8
16NRECEIVED8

<tbody>
</tbody>

RESULT I SHOULD GET AND I ANTICIPATE

Po numberpart numberstatusquantity
a11234packed12
a2 picked
a31121 5
1212despatched12
43445received10
3333packed5
aaaaAARAVPACKED
vvvDEEPAKPACKED
dddC
eeeD 5
PACKED5
ghhh PACKED5
11ZRECEIVED8
12XRECEIVED8
13CRECEIVED8
14VRECEIVED8
15GRECEIVED8
16NRECEIVED8

<colgroup><col width="64" span="4" style="width:48pt"> </colgroup><tbody>
</tbody>

RESULT I ACTUALLY GET

PO NUMBERPART NUMBERSTATUSQUANTITY
a11234packed12
a2 picked
a31121 5
aaaa1212despatched12
vvv43445received10
ddd3333packed5
eeeAARAVPACKED
DEEPAKPACKED
ghhhC
11D 5
12ZPACKED5
13XPACKED5
14CRECEIVED8
15VRECEIVED8
16GRECEIVED8
NRECEIVED8
RECEIVED8
RECEIVED8

<colgroup><col><col span="3"></colgroup><tbody>
</tbody>

Hope this will explain my problem. sorry to be a pain.
 
Upvote 0
See is this mod will work for you.

Code:
Sub Get_Info_By_Headers2()
    Dim sPath As String
    Dim sFil As String
    Dim owb As Workbook
    Dim twb As Workbook
    Dim ch
    Dim j As Long, a As Long, lr As Long
    With Application
        .Calculation = xlCalculationManual
        .EnableEvents = False
        .ScreenUpdating = False
    End With
    ch = Array("po number", "part number", "status", "quantity")
    Set twb = ThisWorkbook
    sPath = "C:\Users\dipak\Desktop\CRASH REPORT\"
    sFil = Dir(sPath & "*.xl*")
    Do While sFil <> "" And sFil <> twb.Name
        Set owb = Workbooks.Open(sPath & sFil)
            With owb.Sheets("data")
                lr = twb.Sheets("report").Cells.Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row + 1
                For j = LBound(ch) To UBound(ch)                    
                    a = .Rows(1).Find(ch(j), , , 1).Column
                    .Range(.Cells(2, a), .Cells(.Cells(.Rows.Count, a).End(xlUp).Row, a)).Copy twb.Sheets("report").Cells(lr, j + 1)
                Next j
            End With
        owb.Close False 'Close no save
        sFil = Dir
    Loop
    With Application
        .Calculation = xlAutomatic
        .EnableEvents = True
        .ScreenUpdating = True
    End With
End Sub
 
Last edited:
Upvote 0
Glad you could use it,
regards, JLG

Sorry for being greedy but is it possible if I want to include the name of the file from where the data is being taken to be included in one of the columns?

I feel you will definitely have the solution.

Thanks.
 
Upvote 0
Hi all

I was able to get the result (close to what I want not exact) by the following code.

Currently it is only putting the file name in bh2 to bh10. How can i make it put the file name in Bh2 to till the last active row of the sheet?

owb.Sheets("data").Range("bh2:bh10").Value = owb.Name


Code:
Sub Get_Info_By_Headers()
    Dim sPath As String
    Dim sFil As String
    Dim owb As Workbook
    Dim twb As Workbook
    Dim ch
    
    
    Dim j As Long, a As Long
    With Application
        .Calculation = xlCalculationManual
        .EnableEvents = False
        .ScreenUpdating = False
    End With
    ch = Array("po number", "part number", "status", "quantity", "project")
    Set twb = ThisWorkbook
    sPath = "C:\Users\dipak\Desktop\CRASH REPORT\"
    sFil = Dir(sPath & "*.xl*")
    Do While sFil <> "" And sFil <> twb.Name
        Set owb = Workbooks.Open(sPath & sFil)
            With owb.Sheets("data")
            owb.Sheets("DATA").Cells(1, 60) = "project"
            'owb.Sheets("DATA").Cells(2, 60) = owb.Name
            owb.Sheets("data").Range("bh2:bh10").Value = owb.Name
            
            
                lr = twb.Sheets("report").Cells.Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row + 1
                For j = LBound(ch) To UBound(ch)
                    a = .Rows(1).Find(ch(j), , , 1).Column
                    .Range(.Cells(2, a), .Cells(.Cells(.Rows.Count, a).End(xlUp).Row, a)).Copy twb.Sheets("report").Cells(lr, j + 1)
                    
                    Next j
            End With
        owb.Close False 'Close no save
        sFil = Dir
    Loop


    With Application
        .Calculation = xlAutomatic
        .EnableEvents = True
        .ScreenUpdating = True
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,593
Messages
6,125,715
Members
449,254
Latest member
Eva146

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