copy lastrow for 12000 rows based on first row for each range

Maklil

Board Regular
Joined
Jun 23, 2022
Messages
144
Office Version
  1. 2019
Platform
  1. Windows
Hello
I have about 12000 rows for two sheets . sheet DATA contains data and sheet DETAILS contains the same structure but for each range is separated . so should match data between two sheets from columns B:D if the data are matched , then will copy the lastrow for each separated range from sheet DETAILS to sheet OUTPUT but based on condition , if the value in lastrow is the same value for first row(under header means second row for each range ) or the value in lastrow equal to zero should not copy at all, otherwise should copy the others , and if the existed data in sheet DATA but not existed as in separated range at all also should copy to sheet output .
with considering the data will change in sheets DATA,DETAILS ,then should clear data in sheet OUTPUT before brings data to updating any change for the others sheets .
copy last row.xlsm
ABCDE
1DATECUSTOMERINV NOITEMQTY
211/11/2022CSS-100INV-A123ITTT-100/AS-1200
311/12/2022CSS-100INV-A123ITTT-100/AS-2300
411/13/2022CSS-102INV-A125ITTT-100/AS-2400
511/14/2022CSS-103INV-A126ITTT-100/AS-2500
611/15/2022CSS-100INV-A124ITTT-100/AS-2600
711/16/2022CSS-105INV-A128ITTT-100/AS-6700
811/17/2022CSS-100INV-A129ITTT-100/AS-7800
911/18/2022CSS-107INV-A130ITTT-100/AS-4900
1011/19/2022CSS-108INV-A131ITTT-100*12GF1000
1111/20/2022CSS-109INV-A132ITTT-100*12GF1100
1211/21/2022CSS-109INV-A132ITTT-100*12GF10001200
1311/22/2022CSS-111INV-A133IHGE20001300
1411/23/2022CSS-111INV-A134ITTT-100/AS-21400
1511/24/2022CSS-102INV-A135ITTT-100/AS-31500
1611/25/2022CSS-107INV-A137ITTT-100/AS-81600
1711/26/2022CSS-108INV-A138ITTT-100/AS-101601
DATA



copy last row.xlsm
ABCDE
1DATECUSTOMERINV NOITEMQTY
211/11/2022CSS-100INV-A123ITTT-100/AS-1200
312/27/2022CSS-100INV-A123ITTT-100/AS-1100
412/27/2022CSS-100INV-A123ITTT-100/AS-150
5
6DATECUSTOMERINV NOITEMQTY
711/12/2022CSS-100INV-A123ITTT-100/AS-2300
812/27/2022CSS-100INV-A123ITTT-100/AS-2-20
9
10DATECUSTOMERINV NOITEMQTY
1111/13/2022CSS-102INV-A125ITTT-100/AS-2400
1211/14/2022CSS-102INV-A125ITTT-100/AS-2200
1312/27/2022CSS-102INV-A125ITTT-100/AS-2100
1412/28/2022CSS-102INV-A125ITTT-100/AS-2400
15
16DATECUSTOMERINV NOITEMQTY
1711/19/2022CSS-108INV-A131ITTT-100*12GF1000
1812/27/2022CSS-108INV-A131ITTT-100*12GF900
1912/28/2022CSS-108INV-A131ITTT-100*12GF0
DETAILS


this is what I want
copy last row.xlsm
ABCDE
1ITEMCUSTOMERINV NOITEMQTY
21CSS-100INV-A123ITTT-100/AS-150
32CSS-100INV-A123ITTT-100/AS-2-20
43CSS-103INV-A126ITTT-100/AS-2500
54CSS-100INV-A124ITTT-100/AS-2600
65CSS-105INV-A128ITTT-100/AS-6700
76CSS-100INV-A129ITTT-100/AS-7800
87CSS-107INV-A130ITTT-100/AS-4900
98CSS-109INV-A132ITTT-100*12GF1100
109CSS-109INV-A132ITTT-100*12GF10001200
1110CSS-111INV-A133IHGE20001300
1211CSS-111INV-A134ITTT-100/AS-21400
1312CSS-102INV-A135ITTT-100/AS-31500
1413CSS-107INV-A137ITTT-100/AS-81600
1514CSS-108INV-A138ITTT-100/AS-101601
output
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
In your sample data the First Line in Details always matches the Line in Data ? Is that something that can be relied on ?
Also will there always only be one line in Data per combination ?

For the 1st example INV-A123 ITTT-100/AS-1 Details has 3 lines but only the last one is in your output. Are you expecting only the last line in output ?

Is it possible for a record to exist in Details and not in Data ? What rule applies when that happens ?
 
Upvote 0
Hi Alex
In your sample data the First Line in Details always matches the Line in Data ? Is that something that can be relied on ?
yes
Also will there always only be one line in Data per combination ?
I'm not sure which sheet you're talking about , if you mean sheet DATA yes . as to sheet DETAILS must be in first line Data per combination depends on sheet DATA until match the first line Data per combination with the last line and implement the condition as I said
if the value in lastrow is the same value for first row(under header means second row for each range ) or the value in lastrow equal to zero should not copy at all, otherwise should copy the others


For the 1st example INV-A123 ITTT-100/AS-1 Details has 3 lines but only the last one is in your output. Are you expecting only the last line in output ?
yes should be the last line in output
Is it possible for a record to exist in Details and not in Data ?
surely not becuase sheet DETAILS depends on sheet DATA. in others meaining sheet DATA is source .
 
Upvote 0
I might not get a chance to do anything tonight being NY eve but will have a look tomorrow.
 
Upvote 0
no problem Alex .:)
I hope you can help me with this project as soon as possible .
 
Upvote 0
Give this a try:

VBA Code:
Sub GetLastTran()
    Dim dataSht As Worksheet, detailSht As Worksheet, outSht As Worksheet
    Dim dataRng As Range, detailRng As Range, outRng As Range
    Dim dataLRow As Long, detailLRow As Long, outLRow As Long
    Dim dataArr As Variant, detailArr As Variant, outArr() As Variant
    Dim dataAmt As Currency, detailAmt As Currency
    Dim i As Long, jCol As Long, detailRow As Long, outRow As Long
    
    Set dataSht = Worksheets("DATA")            ' <--- Change if not correct name
    Set detailSht = Worksheets("DETAILS")       ' <--- Change if not correct name
    Set outSht = Worksheets("output")           ' <--- Change if not correct name
    
    With dataSht
        dataLRow = .Range("A" & Rows.Count).End(xlUp).Row
        Set dataRng = .Range("A2:E" & dataLRow)
        dataArr = dataRng.Value2
    End With
    
    With detailSht
        detailLRow = .Range("A" & Rows.Count).End(xlUp).Row
        Set detailRng = .Range("A2:E" & detailLRow)
        detailArr = detailRng.Value2
    End With
    
    With outSht
        Set outRng = .Range("A2")
        outRng.CurrentRegion.Offset(1).ClearContents
        ReDim outArr(1 To UBound(dataArr, 1), 1 To UBound(dataArr, 2))
    End With
    
    Dim dictDetail As Object, dictKey As String

    Set dictDetail = CreateObject("Scripting.dictionary")
    ReDim Preserve detailArr(1 To UBound(detailArr), 1 To UBound(detailArr, 2) + 1)
    ' Load details range into Dictionary
    For i = 1 To UBound(detailArr)
        dictKey = detailArr(i, 2) & "|" & detailArr(i, 3) & "|" & detailArr(i, 4)
        ' Load latest row number
        If detailArr(i, 1) <> "" And UCase(detailArr(i, 1) <> "DATE") Then
            dictDetail(dictKey) = i
        End If
    Next i
    
    ' Load output array
    For i = 1 To UBound(dataArr)
        dataAmt = dataArr(i, 5)
        dictKey = dataArr(i, 2) & "|" & dataArr(i, 3) & "|" & dataArr(i, 4)
        If dictDetail.exists(dictKey) Then
            detailRow = dictDetail(dictKey)
            detailAmt = detailArr(detailRow, 5)
            If detailAmt <> 0 And detailAmt <> dataAmt Then
                ' Use Detail information
                outRow = outRow + 1
                For jCol = 1 To UBound(outArr, 2)
                    outArr(outRow, jCol) = detailArr(detailRow, jCol)
                Next jCol
            End If
        Else
            ' if not in Detail sheet use Data information
            outRow = outRow + 1
            For jCol = 1 To UBound(outArr, 2)
                outArr(outRow, jCol) = dataArr(i, jCol)
            Next jCol
        End If
    Next i
    
    ' Output results
    outRng.Resize(outRow, UBound(outArr, 2)).Value = outArr
    
    ' Recreate Index
    outRng.Value = 1
    Set outRng = outRng.Resize(outRow, 1)
    outRng.NumberFormat = "General"
    outRng.Cells(1).AutoFill Destination:=outRng, Type:=xlFillSeries

End Sub
 
Upvote 0
Solution
it's great !:)
works as I expected .
many thanks Alex .;)
 
Upvote 0

Forum statistics

Threads
1,215,103
Messages
6,123,112
Members
449,096
Latest member
provoking

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