VBA Code Required

hrayani

Well-known Member
Joined
Jul 23, 2010
Messages
1,516
Office Version
  1. 2016
Platform
  1. Windows
Hello Friends,

I want to copy specific data in specific format from one worksheet to another (both worksheet in the same workbook)

Here is the short sample data

Running Orders New Style.xlsm
ABCDEFGHIJKLMNOPQRSTUV
2PO #REF #PO DATECustomerSupplierArticleQualityDyed or PrintedFiber ContentConstructionSIZEQTYUNITPO SHIP DATEACTUAL SHIP DATEREMARKSSAMPLINGPO # CONCATARTICLE CONCATEXTRA COLUMNVALUESTATUS
337742163329-Dec-21TEX 1MillsBed SetMicrofiber Satin - 100 GsmDisperse Print100% Polyester75D X 200D 107X66Normal143,760Set(s)2-May-22OCT - 2021377421Bed Set$699,576In Process
437742263329-Dec-21TEX 1MillsPillow PairMicrofiber Satin - 100 GsmDisperse Dyed + Dispers Print100% Polyester75D X 200D 107X66Multiple233,544Pair(s)2-May-22OCT - 2021377421 - 377422Bed Set - Pillow Pair$452,181In Process
537742363329-Dec-21TEX 1MillsBed SetMicrofiber Satin - 100 GsmDisperse Print100% Polyester75D X 200D 107X66Over94,240Set(s)2-May-22OCT - 2021377421 - 377422 - 377423Bed Set - Pillow Pair$673,612In Process
637742463329-Dec-21TEX 1MillsBed SetMicrofiber Satin - 100 GsmDisperse Print100% Polyester75D X 200D 107X66King37,110Set(s)2-May-22OCT - 2021377421 - 377422 - 377423 - 377424Bed Set - Pillow Pair$309,762In Process
737742563329-Dec-21TEX 1MillsSide PillowMicrofiber Satin - 100 GsmDisperse Dyed + Dispers Print100% Polyester75D X 200D 107X6640x145176,532Pc(s)2-May-22OCT - 2021377421 - 377422 - 377423 - 377424 - 377425Bed Set - Pillow Pair - Side Pillow$307,166In Process
898015463410-Dec-21TEX 1MillsBed SetMicrofiber Satin - 100 GsmDisperse Print100% Polyester75D X 200D 107X66Normal59,976Set(s)6-Jun-22OCT - 2021980154Bed Set$249,122In Process
998015563410-Dec-21TEX 1MillsBed SetMicrofiber Satin - 100 GsmDisperse Print100% Polyester75D X 200D 107X66King25,410Set(s)6-Jun-22OCT - 2021980154 - 980155Bed Set$170,777In Process
1098015663410-Dec-21TEX 1MillsBed SetMicrofiber Satin - 100 GsmDisperse Print100% Polyester75D X 200D 107X66Over47,976Set(s)6-Jun-22OCT - 2021980154 - 980155 - 980156Bed Set$254,075In Process
1198015763410-Dec-21TEX 1MillsPillow PairMicrofiber Satin - 100 GsmDisperse Print100% Polyester75D X 200D 107X66Multiple51,240Pair(s)6-Jun-22OCT - 2021980154 - 980155 - 980156 - 980157Bed Set - Pillow Pair$ 41,313In Process
1212345663510-Dec-21ROS 2UsmanFitted SheetCTN Jersey - 135 GsmReactive Dyed100% Cotton30'S Cotton100x2003,920Pc(s)13-Feb-22Best Price & Easy Sleep - Contract # 21176967123456Fitted Sheet$ 14,896Shipped
1312345763510-Dec-21ROS 2UsmanFitted SheetCTN Jersey - 135 GsmReactive Dyed100% Cotton30'S Cotton150x2002,400Pc(s)13-Feb-22Best Price & Easy Sleep - Contract # 21176967123456 - 123457Fitted Sheet$ 12,960Shipped
1422998863614-Dec-21LOSFabricsSheet SetCTN Renforce - 100 GsmPigment Printed Rotary100% Cotton30X30 / 76X50Single1,416Set(s)28-Feb-22229988Sheet Set$ 14,168In Process
1522990063614-Dec-21LOSFabricsSheet SetCTN Renforce - 100 GsmPigment Printed Rotary100% Cotton30X30 / 76X50Single3,456Set(s)28-Feb-22229988 - 229900Sheet Set$ 41,437In Process
1640317563721-Dec-21OFFTowelTerry Beach TowelCTN 320 GSMReactive Print100% Cotton70x150 cm60,160Pc(s)18-Apr-22Amount in euro increased to 13%403175Terry Beach Towel$205,982In Process
1740317663721-Dec-21OFFTowelPonchoCTN 320 GSMReactive Print100% Cotton60x120 cm56,092Pc(s)18-Apr-22Amount in euro increased to 13%403175 - 403176Terry Beach Towel - Poncho$205,998In Process
183100886387-Jan-22OFFTowelTerry TowelCTN 385 GSMReactive Dyed100% Cotton50x100 cm (2 Pc Set)142,566Set(s)10-Jun-22Young Living ▬ Amount in euro increased to 14%310088Terry Towel$367,307Shipped
193100896387-Jan-22OFFTowelTerry TowelCTN 385 GSMReactive Dyed100% Cotton70x140 cm124,382Pc(s)10-Jun-22Young Living ▬ Amount in euro increased to 14%310088 - 310089Terry Towel$314,786Shipped
2056789163912-Jan-22ROS 2UsmanFitted SheetCTN Jersey - 135 GsmReactive Dyed100% Cotton30'S Cotton100x2004,080Pc(s)13-May-22Best Price & Easy Sleep - Contract # 21176967567891Fitted Sheet$ 15,504Shipped
2156789263912-Jan-22ROS 2UsmanFitted SheetCTN Jersey - 135 GsmReactive Dyed100% Cotton30'S Cotton150x2002,220Pc(s)13-May-22Best Price & Easy Sleep - Contract # 21176967567891 - 567892Fitted Sheet$ 11,988Shipped
2212987564012-Jan-22ROS 2UsmanFitted SheetCTN Jersey - 135 GsmReactive Dyed100% Cotton30'S Cotton100x2003,840Pc(s)1-Jun-22Best Price & Easy Sleep - Contract # 21176967129875Fitted Sheet$ 14,592Shipped
2312987664012-Jan-22ROS 2UsmanFitted SheetCTN Jersey - 135 GsmReactive Dyed100% Cotton30'S Cotton150x2002,400Pc(s)1-Jun-22Best Price & Easy Sleep - Contract # 21176967129875 - 129876Fitted Sheet$ 12,960Shipped
ORDERS
Cell Formulas
RangeFormula
R3:R23R3=IF(ISBLANK(B3),"",IF(B3<>B2,A3,IF(ISNUMBER(SEARCH(A3,R2)),R2,R2&" - "&A3)))
S3:S23S3=IF(ISBLANK(B3),"",IF(B3<>B2,F3,IF(ISNUMBER(SEARCH(F3,S2)),S2,S2&" - "&F3)))


Sheet 1: Copy from Sheet ORDERS
Sheet2: Copy to Sheet RUNNING ORDER STATUS

I want the data to copied to sheet 2 with following conditions

1) Copy all rows showing "In Process" in Column V from sheet 1 to sheet 2 starting from cell B4
Columns to copy would be A,B,C,D,E,F,G,K,L,M,N & P (Total 12 Columns)

2) Sort Data - 1st by Customer in Alphabetical order then by Ascending order PO ship date

3) Insert a Line when a Ref # changes
a) On the inserted Line: Show 1st instance for columns PO #, REF #, PO Date, Customer, Supplier, Article, Quality, PO Shape Date Columns
b) On the inserted Line: Show "Multiple" if more than one entry is found for Size, Unit & Remarks Columns else show 1st Instance
c) On the inserted Line: Show Total for the Qty Column

4) In Column A add value 1 to all the copied rows & value 2 to all the inserted rows (This is because I will be filtering records further)

5) Content Banding When a Ref # Changes

6) Dark grey color to the Inserted Rows

So, the final look will be like this

Running Orders New Style.xlsm
ABCDEFGHIJKLM
4PO #REF #PO DATECustomerSupplierArticleQualitySIZEQTYUNITPO SHIP DATEREMARKS
5137742163329-Dec-21TEX 1MillsBed SetMicrofiber Satin - 100 GsmNormal143,760Set(s)2-May-22
6137742263329-Dec-21TEX 1MillsPillow PairMicrofiber Satin - 100 GsmMultiple233,544Pair(s)2-May-22
7137742363329-Dec-21TEX 1MillsBed SetMicrofiber Satin - 100 GsmOver94,240Set(s)2-May-22
8137742463329-Dec-21TEX 1MillsBed SetMicrofiber Satin - 100 GsmKing37,110Set(s)2-May-22
9137742563329-Dec-21TEX 1MillsSide PillowMicrofiber Satin - 100 Gsm40x145176,532Pc(s)2-May-22
10237742163329-Dec-22TEX 1MillsBed SetMicrofiber Satin - 100 GsmMultiple685,186Multiple2-May-22
11198015463410-Dec-21TEX 1MillsBed SetMicrofiber Satin - 100 GsmNormal59,976Set(s)6-Jun-22
12198015563410-Dec-21TEX 1MillsBed SetMicrofiber Satin - 100 GsmKing25,410Set(s)6-Jun-22
13198015663410-Dec-21TEX 1MillsBed SetMicrofiber Satin - 100 GsmOver47,976Set(s)6-Jun-22
14198015763410-Dec-21TEX 1MillsPillow PairMicrofiber Satin - 100 GsmMultiple51,240Pair(s)6-Jun-22
15298015463410-Dec-22TEX 1MillsBed SetMicrofiber Satin - 100 GsmMultiple184,602Multiple6-Jun-22
16122998863614-Dec-21LOSFabricsSheet SetCTN Renforce - 100 GsmSingle1,416Set(s)28-Feb-22
17122990063614-Dec-21LOSFabricsSheet SetCTN Renforce - 100 GsmSingle3,456Set(s)28-Feb-22
18222998863614-Dec-22LOSFabricsSheet SetCTN Renforce - 100 GsmSingle4,872Set(s)28-Feb-22
19140317563721-Dec-21OFFTowelTerry Beach TowelCTN 320 GSM70x150 cm60,160Pc(s)18-Apr-22Amount in euro increased to 13%
20140317663721-Dec-21OFFTowelPonchoCTN 320 GSM60x120 cm56,092Pc(s)18-Apr-22Amount in euro increased to 13%
21240317563721-Dec-22OFFTowelTerry Beach TowelCTN 320 GSMMultiple116,252Pc(s)18-Apr-22
22137742163329-Dec-21TEX 1MillsBed SetMicrofiber Satin - 100 GsmNormal143,760Set(s)2-May-22
23137742263329-Dec-21TEX 1MillsPillow PairMicrofiber Satin - 100 GsmMultiple233,544Pair(s)2-May-22
24137742363329-Dec-21TEX 1MillsBed SetMicrofiber Satin - 100 GsmOver94,240Set(s)2-May-22
25137742463329-Dec-21TEX 1MillsBed SetMicrofiber Satin - 100 GsmKing37,110Set(s)2-May-22
26137742563329-Dec-21TEX 1MillsSide PillowMicrofiber Satin - 100 Gsm40x145176,532Pc(s)2-May-22
27237742163329-Dec-22TEX 1MillsBed SetMicrofiber Satin - 100 GsmMultiple685,186Multiple2-May-22
28198015463410-Dec-21TEX 1MillsBed SetMicrofiber Satin - 100 GsmNormal59,976Set(s)6-Jun-22
29198015563410-Dec-21TEX 1MillsBed SetMicrofiber Satin - 100 GsmKing25,410Set(s)6-Jun-22
30198015663410-Dec-21TEX 1MillsBed SetMicrofiber Satin - 100 GsmOver47,976Set(s)6-Jun-22
31198015763410-Dec-21TEX 1MillsPillow PairMicrofiber Satin - 100 GsmMultiple51,240Pair(s)6-Jun-22
32298015463410-Dec-22TEX 1MillsBed SetMicrofiber Satin - 100 GsmMultiple184,602Multiple6-Jun-22
RUNNING ORDER STATUS


Any help would be appreciated,

Regards,

Humayun
 
Last edited:
Ok I will again use the XL2BB addin

This is the order sheet, from which the data is supposed to be copied
Running Orders New Style.xlsm
ABCDEFGHIJKLMNOPQRSTUV
1PO #REF #PO DATECustomerSupplierArticleQualityDyed or PrintedFiber ContentConstructionSIZEQTYUNITPO SHIP DATEACTUAL SHIP DATEREMARKSSAMPLINGPO # CONCATARTICLE CONCATEXTRA COLUMNVALUESTATUS
237742163329-Dec-21TEX 11MillsBed SetMicrofiber Satin - 100 GsmDisperse Print100% Polyester75D X 200D 107X66Normal143,760Set(s)2-May-22OCT - 2021377421Bed Set$699,576In Process
337742263329-Dec-21TEX 11MillsPillow PairMicrofiber Satin - 100 GsmDisperse Dyed + Dispers Print100% Polyester75D X 200D 107X66Multiple233,544Pair(s)2-May-22OCT - 2021377421 - 377422Bed Set - Pillow Pair$452,181In Process
437742363329-Dec-21TEX 11MillsBed SetMicrofiber Satin - 100 GsmDisperse Print100% Polyester75D X 200D 107X66Over94,240Set(s)2-May-22OCT - 2021377421 - 377422 - 377423Bed Set - Pillow Pair$673,612In Process
537742463329-Dec-21TEX 11MillsBed SetMicrofiber Satin - 100 GsmDisperse Print100% Polyester75D X 200D 107X66King37,110Set(s)2-May-22OCT - 2021377421 - 377422 - 377423 - 377424Bed Set - Pillow Pair$309,762In Process
637742563329-Dec-21TEX 11MillsSide PillowMicrofiber Satin - 100 GsmDisperse Dyed + Dispers Print100% Polyester75D X 200D 107X6640x145176,532Pc(s)2-May-22OCT - 2021377421 - 377422 - 377423 - 377424 - 377425Bed Set - Pillow Pair - Side Pillow$307,166In Process
798015463410-Dec-21TEX 11MillsBed SetMicrofiber Satin - 100 GsmDisperse Print100% Polyester75D X 200D 107X66Normal59,976Set(s)6-Jun-22OCT - 2021980154Bed Set$249,122In Process
898015563410-Dec-21TEX 11MillsBed SetMicrofiber Satin - 100 GsmDisperse Print100% Polyester75D X 200D 107X66King25,410Set(s)6-Jun-22OCT - 2021980154 - 980155Bed Set$170,777In Process
998015663410-Dec-21TEX 11MillsBed SetMicrofiber Satin - 100 GsmDisperse Print100% Polyester75D X 200D 107X66Over47,976Set(s)6-Jun-22OCT - 2021980154 - 980155 - 980156Bed Set$254,075In Process
1098015763410-Dec-21TEX 11MillsPillow PairMicrofiber Satin - 100 GsmDisperse Print100% Polyester75D X 200D 107X66Multiple51,240Pair(s)6-Jun-22OCT - 2021980154 - 980155 - 980156 - 980157Bed Set - Pillow Pair$ 41,313In Process
1112345663510-Dec-21ROS 2UsmanFitted SheetCTN Jersey - 135 GsmReactive Dyed100% Cotton30'S Cotton100x2003,920Pc(s)13-Feb-22Best Price & Easy Sleep - Contract # 21176967123456Fitted Sheet$ 14,896Shipped
1212345763510-Dec-21ROS 2UsmanFitted SheetCTN Jersey - 135 GsmReactive Dyed100% Cotton30'S Cotton150x2002,400Pc(s)13-Feb-22Best Price & Easy Sleep - Contract # 21176967123456 - 123457Fitted Sheet$ 12,960Shipped
1322998863614-Dec-21zLOSFabricsSheet SetCTN Renforce - 100 GsmPigment Printed Rotary100% Cotton30X30 / 76X50Single1,416Set(s)28-Feb-22229988Sheet Set$ 14,168In Process
1422990063614-Dec-21zLOSFabricsSheet SetCTN Renforce - 100 GsmPigment Printed Rotary100% Cotton30X30 / 76X50Single3,456Set(s)28-Feb-22229988 - 229900Sheet Set$ 41,437In Process
1540317563721-Dec-21OFFTowelTerry Beach TowelCTN 320 GSMReactive Print100% Cotton70x150 cm60,160Pc(s)18-Apr-22Amount in euro increased to 13%403175Terry Beach Towel$205,982In Process
1640317663721-Dec-21OFFTowelPonchoCTN 320 GSMReactive Print100% Cotton60x120 cm56,092Pc(s)18-Apr-22Amount in euro increased to 14%403175 - 403176Terry Beach Towel - Poncho$205,998In Process
173100886387-Jan-22OFFTowelTerry TowelCTN 385 GSMReactive Dyed100% Cotton50x100 cm (2 Pc Set)142,566Set(s)10-Jun-22Young Living ▬ Amount in euro increased to 14%310088Terry Towel$367,307Shipped
183100896387-Jan-22OFFTowelTerry TowelCTN 385 GSMReactive Dyed100% Cotton70x140 cm124,382Pc(s)10-Jun-22Young Living ▬ Amount in euro increased to 14%310088 - 310089Terry Towel$314,786Shipped
1956789163912-Jan-22ROS 2UsmanFitted SheetCTN Jersey - 135 GsmReactive Dyed100% Cotton30'S Cotton100x2004,080Pc(s)13-May-22Best Price & Easy Sleep - Contract # 21176967567891Fitted Sheet$ 15,504In Process
2056789263912-Jan-22ROS 2UsmanFitted SheetCTN Jersey - 135 GsmReactive Dyed100% Cotton30'S Cotton150x2002,220Pc(s)13-May-22Best Price & Easy Sleep - Contract # 21176967567891 - 567892Fitted Sheet$ 11,988In Process
ORDERS
Cell Formulas
RangeFormula
R2:R20R2=IF(ISBLANK(B2),"",IF(B2<>B1,A2,IF(ISNUMBER(SEARCH(A2,R1)),R1,R1&" - "&A2)))
S2:S20S2=IF(ISBLANK(B2),"",IF(B2<>B1,F2,IF(ISNUMBER(SEARCH(F2,S1)),S1,S1&" - "&F2)))


And this is the result sheet

Running Orders New Style.xlsm
ABCDEFGHIJKLM
3PO #REF #PO DATECustomerSupplierArticleQualitySIZEQTYUNITPO SHIP DATEREMARKS
4140317563721-Dec-21OFFTowelTerry Beach TowelCTN 320 GSM70x150 cm60,160Pc(s)18-Apr-22Amount in euro increased to 13%
5140317663721-Dec-21OFFTowelPonchoCTN 320 GSM60x120 cm56,092Pc(s)18-Apr-22Amount in euro increased to 14%
6240317563721-Dec-21OFFTowelTerry Beach TowelCTN 320 GSMMultiple116,252Pc(s)18-Apr-22Multiple
7156789163912-Jan-22ROS 2UsmanFitted SheetCTN Jersey - 135 Gsm100x2004,080Pc(s)13-May-22Best Price & Easy Sleep - Contract # 21176967
8156789263912-Jan-22ROS 2UsmanFitted SheetCTN Jersey - 135 Gsm150x2002,220Pc(s)13-May-22Best Price & Easy Sleep - Contract # 21176967
9256789163912-Jan-22ROS 2UsmanFitted SheetCTN Jersey - 135 GsmMultiple6,300Pc(s)13-May-22Best Price & Easy Sleep - Contract # 21176967
10137742163329-Dec-21TEX 11MillsBed SetMicrofiber Satin - 100 GsmNormal143,760Set(s)2-May-22
11137742263329-Dec-21TEX 11MillsPillow PairMicrofiber Satin - 100 GsmMultiple233,544Pair(s)2-May-22
12137742363329-Dec-21TEX 11MillsBed SetMicrofiber Satin - 100 GsmOver94,240Set(s)2-May-22
13137742463329-Dec-21TEX 11MillsBed SetMicrofiber Satin - 100 GsmKing37,110Set(s)2-May-22
14137742563329-Dec-21TEX 11MillsSide PillowMicrofiber Satin - 100 Gsm40x145176,532Pc(s)2-May-22
15237742163329-Dec-21TEX 11MillsBed SetMicrofiber Satin - 100 GsmMultiple685,186Multiple2-May-22
16198015463410-Dec-21TEX 11MillsBed SetMicrofiber Satin - 100 GsmNormal59,976Set(s)6-Jun-22
17198015563410-Dec-21TEX 11MillsBed SetMicrofiber Satin - 100 GsmKing25,410Set(s)6-Jun-22
18198015663410-Dec-21TEX 11MillsBed SetMicrofiber Satin - 100 GsmOver47,976Set(s)6-Jun-22
19198015763410-Dec-21TEX 11MillsPillow PairMicrofiber Satin - 100 GsmMultiple51,240Pair(s)6-Jun-22
20298015463410-Dec-21TEX 11MillsBed SetMicrofiber Satin - 100 GsmMultiple184,602Multiple6-Jun-22
21122998863614-Dec-21zLOSFabricsSheet SetCTN Renforce - 100 GsmSingle1,416Set(s)28-Feb-22
22122990063614-Dec-21zLOSFabricsSheet SetCTN Renforce - 100 GsmSingle3,456Set(s)28-Feb-22
23222998863614-Dec-21zLOSFabricsSheet SetCTN Renforce - 100 GsmSingle4,872Set(s)28-Feb-22
RUNNING ORDER STATUS


And this is the final code I am using

VBA Code:
Sub CopyData()
    Application.ScreenUpdating = False
    Dim LastRow As Long, srcWS As Worksheet, desWS As Worksheet, rng As Range, x As Long, i As Long, fRow As Long, lRow As Long, y As Long: y = 19
    Set srcWS = Sheets("ORDERS")
    Set desWS = Sheets("RUNNING ORDER STATUS")
   
    desWS.UsedRange.Rows.Delete
    With srcWS
        .Range("A1").CurrentRegion.AutoFilter 22, "In Process"
        Intersect(.AutoFilter.Range, .Range("A:G,K:N,P:P")).Copy desWS.Range("B3")
        .Range("A1").AutoFilter
    End With
    LastRow = desWS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
    With desWS.Sort
        .SortFields.Clear
        .SortFields.Add Key:=Range("E3:E" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .SortFields.Add Key:=Range("L3:L" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .SetRange Range("B3:M" & LastRow)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    For x = LastRow To 5 Step -1
        With desWS
            If .Cells(x, 3) <> .Cells(x - 1, 3) Then
                .Rows(x).EntireRow.Insert
            End If
        End With
    Next x
    LastRow = desWS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    With desWS.Range("B4:B" & LastRow).SpecialCells(xlCellTypeConstants)
        For i = 1 To .Areas.Count
            fRow = .Areas(i).Cells(1).Row
            lRow = .Areas(i).Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            With desWS
                .Range("B" & lRow + 1 & ":M" & lRow + 1).Value = .Range("B" & fRow & ":M" & fRow).Value
                .Range("J" & lRow + 1).Value = WorksheetFunction.Sum(.Range("J" & fRow & ":J" & lRow))
                .Range("A" & fRow & ":A" & lRow) = 1
                .Range("A" & fRow & ":M" & lRow).Interior.ColorIndex = y
                .Range("A" & lRow + 1) = 2
                .Range("A" & lRow + 1 & ":M" & lRow + 1).Interior.ColorIndex = 15
                Set rng = .Range("I" & fRow & ":I" & lRow)
                If rng.Count = WorksheetFunction.CountIf(rng, rng(1)) Then
                    .Range("I" & lRow + 1) = rng(1)
                Else
                    .Range("I" & lRow + 1) = "Multiple"
                End If
                Set rng = .Range("K" & fRow & ":K" & lRow)
                If rng.Count = WorksheetFunction.CountIf(rng, rng(1)) Then
                    .Range("K" & lRow + 1) = rng(1)
                Else
                    .Range("K" & lRow + 1) = "Multiple"
                End If
                Set rng = .Range("M" & fRow & ":M" & lRow)
                If rng.Count = WorksheetFunction.CountIf(rng, rng(1)) Then
                    .Range("M" & lRow + 1) = rng(1)
                ElseIf rng.Count = WorksheetFunction.CountIf(rng, "") Then
                    .Range("M" & lRow + 1) = ""
                Else
                    .Range("M" & lRow + 1) = "Multiple"
                End If
                If y = 19 Then
                    y = 20
                Else
                    y = 19
                End If
            End With
        Next i
       
    End With
    Application.ScreenUpdating = True
End Sub

I only require 2 changes in the above code

1) At the moment, code is copying the data with the headings starting from row # 3. What I want is, I will type in the headings manually once & format it once manually. I want the code to copy data without the headings starting from row # 4

2) When I re-run the code, don't do anything From row # 1 to 3. Like leave the headings & its formats as it is & erase everything from row # 4 till the last row, before copying fresh data so that it does not get overlap with the existing data
 
Upvote 0

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Try:
VBA Code:
Sub CopyData()
    Application.ScreenUpdating = False
    Dim LastRow As Long, srcWS As Worksheet, desWS As Worksheet, rng As Range, x As Long, i As Long, fRow As Long, lRow As Long, y As Long: y = 19
    Set srcWS = Sheets("ORDERS")
    Set desWS = Sheets("RUNNING ORDER STATUS")
    desWS.UsedRange.Rows.Delete
    With srcWS
        .Range("A1").CurrentRegion.AutoFilter 22, "In Process"
        Intersect(.AutoFilter.Range, .Range("A:G,K:N,P:P")).Copy desWS.Range("B1")
        .Range("A1").AutoFilter
    End With
    LastRow = desWS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    With desWS.Sort
        .SortFields.Clear
        .SortFields.Add Key:=Range("E2:E" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .SortFields.Add Key:=Range("L2:L" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .SetRange Range("B1:M" & LastRow)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
    For x = LastRow To 3 Step -1
        With desWS
            If .Cells(x, 3) <> .Cells(x - 1, 3) Then
                .Rows(x).EntireRow.Insert
            End If
        End With
    Next x
    LastRow = desWS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    With desWS.Range("B2:B" & LastRow).SpecialCells(xlCellTypeConstants)
        For i = 1 To .Areas.Count
            fRow = .Areas(i).Cells(1).Row
            lRow = .Areas(i).Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            With desWS
                .Range("B" & lRow + 1 & ":M" & lRow + 1).Value = .Range("B" & fRow & ":M" & fRow).Value
                .Range("J" & lRow + 1).Value = WorksheetFunction.Sum(.Range("J" & fRow & ":J" & lRow))
                .Range("A" & fRow & ":A" & lRow) = 1
                .Range("A" & fRow & ":M" & lRow).Interior.ColorIndex = y
                .Range("A" & lRow + 1) = 2
                .Range("A" & lRow + 1 & ":M" & lRow + 1).Interior.ColorIndex = 15
                Set rng = .Range("I" & fRow & ":I" & lRow)
                If rng.Count = WorksheetFunction.CountIf(rng, rng(1)) Then
                    .Range("I" & lRow + 1) = rng(1)
                Else
                    .Range("I" & lRow + 1) = "Multiple"
                End If
                Set rng = .Range("K" & fRow & ":K" & lRow)
                If rng.Count = WorksheetFunction.CountIf(rng, rng(1)) Then
                    .Range("K" & lRow + 1) = rng(1)
                Else
                    .Range("K" & lRow + 1) = "Multiple"
                End If
                Set rng = .Range("M" & fRow & ":M" & lRow)
                If rng.Count = WorksheetFunction.CountIf(rng, rng(1)) Then
                    .Range("M" & lRow + 1) = rng(1)
                ElseIf rng.Count = WorksheetFunction.CountIf(rng, "") Then
                    .Range("M" & lRow + 1) = ""
                Else
                    .Range("M" & lRow + 1) = "Multiple"
                End If
                If y = 19 Then
                    y = 20
                Else
                    y = 19
                End If
            End With
        Next i
       
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks mumps but it did not work as it is now pasting the data to the destination sheet starting from row # 1 with the headers
Whereas I wanted it to copy the data without the headers & start pasting in the destination sheet starting from Row # 4

Anyways, Just a bit of amendment in your code did the trick

VBA Code:
 Intersect(.AutoFilter.Range, .Range("A:G,K:N,P:P")).Offset(1, 0).Copy desWS.Range("B4")

since I did not want to copy the headers from the source sheet, all I did is added the offset function telling the code not to copy row # 1 and set the desWS.Range("B4") so that it starts pasting from row # 4

Thanks again for your time & effort you have put in to help me out. Job is done now :)

Just out of curiosity & for learning purpose, can you please tell me what the below part of the code is actually doing

VBA Code:
For x = LastRow To 5 Step -1
        With desWS
             If .Cells(x, 3) <> .Cells(x - 1, 3) Then
                .Rows(x).EntireRow.Insert
            End If
        End With
 
Upvote 0
That code loops through the rows starting from the last row going up to the fifth row and inserts a blank row at each change in column C.
 
Upvote 0
That code loops through the rows starting from the last row going up to the fifth row and inserts a blank row at each change in column C.

After reading this reply, I just tried to play with the code a bit. Like I changed the column number from 3 to 13 & it did exactly as you said so
Many Thanks, Great Learning for me
 
Upvote 0
You are very welcome. :)
Hi mumps,

One more question as I am still in learning stage
Can we change this part of the code to count unique entries instead of sum

VBA Code:
.Range("K" & lRow + 1).Value = WorksheetFunction.Sum(.Range("K" & fRow & ":K" & lRow))

I tried changing it to count & it does, but I can't get it to work to count unique values
 
Upvote 0
Is this what you want?
VBA Code:
Sub CopyData()
    Application.ScreenUpdating = False
    Dim LastRow As Long, srcWS As Worksheet, desWS As Worksheet, rng As Range, dic As Object
    Dim cel As Range, x As Long, i As Long, fRow As Long, lRow As Long, y As Long: y = 19
    Set srcWS = Sheets("ORDERS")
    Set desWS = Sheets("RUNNING ORDER STATUS")
    desWS.UsedRange.Rows.Delete
    With srcWS
        .Range("A1").CurrentRegion.AutoFilter 22, "In Process"
        Intersect(.AutoFilter.Range, .Range("A:G,K:N,P:P")).Copy desWS.Range("B1")
        .Range("A1").AutoFilter
    End With
    LastRow = desWS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    With desWS.Sort
        .SortFields.Clear
        .SortFields.Add Key:=Range("E2:E" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .SortFields.Add Key:=Range("L2:L" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .SetRange Range("B1:M" & LastRow)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
    For x = LastRow To 3 Step -1
        With desWS
            If .Cells(x, 3) <> .Cells(x - 1, 3) Then
                .Rows(x).EntireRow.Insert
            End If
        End With
    Next x
    LastRow = desWS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    With desWS.Range("B2:B" & LastRow).SpecialCells(xlCellTypeConstants)
        For i = 1 To .Areas.Count
            fRow = .Areas(i).Cells(1).Row
            lRow = .Areas(i).Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            With desWS
                .Range("B" & lRow + 1 & ":M" & lRow + 1).Value = .Range("B" & fRow & ":M" & fRow).Value
                .Range("J" & lRow + 1).Value = WorksheetFunction.Sum(.Range("J" & fRow & ":J" & lRow))
                .Range("A" & fRow & ":A" & lRow) = 1
                .Range("A" & fRow & ":M" & lRow).Interior.ColorIndex = y
                .Range("A" & lRow + 1) = 2
                .Range("A" & lRow + 1 & ":M" & lRow + 1).Interior.ColorIndex = 15
                Set rng = .Range("I" & fRow & ":I" & lRow)
                If rng.Count = WorksheetFunction.CountIf(rng, rng(1)) Then
                    .Range("I" & lRow + 1) = rng(1)
                Else
                    .Range("I" & lRow + 1) = "Multiple"
                End If
                Set rng = .Range("K" & fRow & ":K" & lRow)
                Set dic = CreateObject("Scripting.Dictionary")
                For Each cel In rng
                    If Not dic.exists(cel.Value) Then
                        dic.Add cel.Value, Nothing
                    End If
                Next cel
                .Range("K" & lRow + 1) = dic.Count
                Set rng = .Range("M" & fRow & ":M" & lRow)
                If rng.Count = WorksheetFunction.CountIf(rng, rng(1)) Then
                    .Range("M" & lRow + 1) = rng(1)
                ElseIf rng.Count = WorksheetFunction.CountIf(rng, "") Then
                    .Range("M" & lRow + 1) = ""
                Else
                    .Range("M" & lRow + 1) = "Multiple"
                End If
                If y = 19 Then
                    y = 20
                Else
                    y = 19
                End If
            End With
        Next i
       
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi Mumps,
Bingo! Works Just Perfect
I have done a lot of tweaking with code you provided & everything is working just PERFECT

Here is the final code. Please check the red part containing the count unique value & guide me, is it the right way of doing it
What I did is, tell the code that if the unique count = 1 then just show rng(1) otherwise count unique & show units besides the count number
It is working as intended - but, still I would request you to check it & let me know if the approach is right ?

VBA Code:
Sub copydata()
    Application.ScreenUpdating = False
    Dim LastRow As Long, srcWS As Worksheet, desWS As Worksheet, rng As Range, x As Long, i As Long, fRow As Long, lRow As Long, y As Long: y = 19
    Set srcWS = Sheets("ORDERS")
    Set desWS = Sheets("RUNNING ORDER STATUS")
    
    If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
    
    Rows("4:1000").EntireRow.Hidden = False
    Range("A4:Z1000").Clear
    
    With srcWS
        .Range("A1").CurrentRegion.AutoFilter 22, "In Process"
        Intersect(.AutoFilter.Range, .Range("A:G,K:N,P:P")).Offset(1, 0).Copy desWS.Range("C4")
        .Range("A1").AutoFilter
    End With
    LastRow = desWS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
    With desWS.Sort
        .SortFields.Clear
        
        .SortFields.Add Key:=Range("G3:G" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .SortFields.Add Key:=Range("M3:M" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .SortFields.Add Key:=Range("D3:D" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        
        .SetRange Range("C3:N" & LastRow)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    For x = LastRow To 5 Step -1
        With desWS
            
            If Range("H1").Value = "LEVEL 1" And .Cells(x, 7) <> .Cells(x - 1, 7) Then .Rows(x).EntireRow.Insert
            If Range("H1").Value = "LEVEL 2" And .Cells(x, 4) <> .Cells(x - 1, 4) Then .Rows(x).EntireRow.Insert
            If Range("H1").Value = "LEVEL 3" And .Cells(x, 4) <> .Cells(x - 1, 4) Then .Rows(x).EntireRow.Insert
            
            
        End With
    Next x
    LastRow = desWS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    With desWS.Range("G4:G" & LastRow).SpecialCells(xlCellTypeConstants)
        For i = 1 To .Areas.Count
            fRow = .Areas(i).Cells(1).Row
            lRow = .Areas(i).Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            
With desWS

    .Range("C" & lRow + 1 & ":N" & lRow + 1).Value = .Range("C" & fRow & ":N" & fRow).Value
                
    .Range("K" & lRow + 1).Value = WorksheetFunction.Sum(.Range("K" & fRow & ":K" & lRow))
                
    .Range("B" & fRow & ":B" & lRow) = 1
    .Range("B" & fRow & ":N" & lRow).Interior.ColorIndex = y
    .Range("B" & fRow & ":N" & lRow).Interior.TintAndShade = 0.5
                
.Range("B" & lRow + 1) = 2
                
If Range("H1").Value = "LEVEL 1" Then .Range("B" & lRow + 1 & ":N" & lRow + 1).Interior.ColorIndex = y
If Range("H1").Value = "LEVEL 1" Then .Range("B" & lRow + 1 & ":N" & lRow + 1).Interior.TintAndShade = 0.5
If Range("H1").Value = "LEVEL 1" Then .Range("B" & lRow + 1 & ":N" & lRow + 1).Font.Bold = False
                
If Range("H1").Value = "LEVEL 2" Then .Range("B" & lRow + 1 & ":N" & lRow + 1).Interior.ColorIndex = y
If Range("H1").Value = "LEVEL 2" Then .Range("B" & lRow + 1 & ":N" & lRow + 1).Interior.TintAndShade = 0.5
If Range("H1").Value = "LEVEL 2" Then .Range("B" & lRow + 1 & ":N" & lRow + 1).Font.Bold = False
                
If Range("H1").Value = "LEVEL 3" Then .Range("B" & lRow + 1 & ":N" & lRow + 1).Interior.ColorIndex = 15
If Range("H1").Value = "LEVEL 3" Then .Range("B" & lRow + 1 & ":N" & lRow + 1).Font.Bold = True
                
                
    .Range("C" & fRow & ":N" & lRow).Borders(xlInsideVertical).Weight = xlThin
    .Range("C" & fRow & ":N" & lRow).Borders(xlInsideVertical).ColorIndex = 15
                
    .Range("C" & lRow + 1 & ":N" & lRow + 1).Borders(xlInsideVertical).Weight = xlThin
    .Range("C" & lRow + 1 & ":N" & lRow + 1).Borders(xlInsideVertical).ColorIndex = 15
    
'(START) Apply horizontal broders___________________________________________________

If Range("H1").Value = "LEVEL 1" Then
    .Range("C" & fRow & ":N" & lRow + 2).Borders(xlInsideHorizontal).Weight = xlThin
    .Range("C" & fRow & ":N" & lRow + 2).Borders(xlInsideHorizontal).ColorIndex = 15
End If
'(END) Apply horizontal broders_____________________________________________________
                
    Set rng = .Range("J" & fRow & ":J" & lRow)
                If rng.Count = WorksheetFunction.CountIf(rng, rng(1)) Then
                    .Range("J" & lRow + 1) = rng(1)
                Else
                    .Range("J" & lRow + 1) = "Multiple"
                End If
                
'    Set rng = .Range("L" & fRow & ":L" & lRow)
'                If rng.Count = WorksheetFunction.CountIf(rng, rng(1)) Then
'                    .Range("L" & lRow + 1) = rng(1)
'                Else
'                    .Range("L" & lRow + 1) = "Multiple"
'                End If
                

[COLOR=rgb(235, 107, 86)][B]Set rng = .Range("L" & fRow & ":L" & lRow)
                Set dic = CreateObject("Scripting.Dictionary")
                For Each cel In rng
                    If Not dic.exists(cel.Value) Then
                        dic.Add cel.Value, Nothing
                    End If
                    Next cel
                .Range("L" & lRow + 1) = dic.Count & " Unit(s)"
             If .Range("L" & lRow + 1) = 1 & " Unit(s)" Then
                     .Range("L" & lRow + 1) = rng(1)
                End If[/B][/COLOR]
                
                
    Set rng = .Range("N" & fRow & ":N" & lRow)
                If rng.Count = WorksheetFunction.CountIf(rng, rng(1)) Then
                    .Range("N" & lRow + 1) = rng(1)
                ElseIf rng.Count = WorksheetFunction.CountIf(rng, "") Then
                    .Range("N" & lRow + 1) = ""
                Else
                    .Range("N" & lRow + 1) = "Multiple"
                End If
                
                If y = 19 Then
                    y = 20
                Else
                    y = 19
                End If
            End With
        Next i

    End With

    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,212
Members
452,618
Latest member
Tam84

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