Copy Range of Cells based on Criteria, Paste it on rows below it, Do This Until All conditions are met, Then Delete All Copied Rows

glaccounting

New Member
Joined
Jan 4, 2024
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
Hi All,

I would like to request your help. Not sure if this just needs a simple VBA code, but since I am very new to VBA, this has been giving me problems. Will really appreciate all of you guys help.
Here's the problem. I need to copy range of cells and paste it on the rows below it. But only if it met multiple criteria.
Currently, I have a data (combination of formulas & alphanumeric values) in B13:I17 and L13&L17. In this range, not all of it is filled up. Only certain parts of it contains data.
I also have another table within the sheet that contains the criteria references (range B62:E72).

1st Criteria - Cell L17 formula to be copied and pasted from Cell L18 until 1 Row before the Grand Total Rows (B59:N59) which would be until Cell L57.
2nd Criteria - If cell D67=0 then do nothing, if its D67>0, Range B13:I17 needs to be copied below rows. Formulas to be pasted as formulas, values to be pasted as values. However, there is one cell in the range which is an array formula that needs to be copied and pasted as values which is Cell D13. The number of times range B13:I17 needs to be copied should be equal to the value of D67.

3rd Criteria - If cell D68=0 then Range B13:I17 needs to be copied to the first available blank rows in column B. This time, there is no need for Cell D13 to be pasted as value. All formulas to be copied as formulas, all values to be copied as values as is.

If cell D68>0, then Range B13:I17 needs to be copied to the first available blank rows in column B. This time, there is no need for Cell D13 to be pasted as value. All formulas to be copied as formulas, all values to be copied as values as is. Additionally, D68 value would be set as the number of times of additional copying of Range B13:I17 to the next available blanks rows in Column B with Cell D13 to be pasted as value and not formula.

Do this rule for all the remaining non blank rows for range D68:D71.

After the above, save the file as .pdf - print Area - Range B1:N59
Then, revert the worksheet back to its original untouched form.

Here is a look of the initial worksheet.

OTC Sample Sheet v9.xlsm
BCDEFGHIJKLMN
11Repair History
12DateReferencePO-Contract NumberDescriptionQty.Unit CostTotal CostAccumulated DepreciationAccumulated Impairment LossesIssues/ Transfers/ Adjustment/sAdjusted CostNature of RepairAmount
1308-Apr-08JAP-08-04-000045271740358,500.00175,500.00-166,725.008,775.00
1431-Aug-23JGL-23-08-000258DEPRECIATION0.008,775.00
1530-Sep-23JGL-23-09-000280DEPRECIATION0.008,775.00
1631-Oct-23JGL-23-10-000249DEPRECIATION0.008,775.00
1730-Nov-23JGL-23-11-000273DEPRECIATION0.008,775.00
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59GRAND TOTAL175,500.00-166,725.000.000.008,775.00
Testing (2)
Cell Formulas
RangeFormula
B13B13=VLOOKUP(C13,JEV:Tdate,2,FALSE)
C13C13=VLOOKUP(D13,PONUM:(JEV),2,FALSE)
D13D13=IFNA(LOOKUP(2,1/((COUNTIF($D12:D$12,PONUM)=0)*($D$8&$D$9=TYPE&ACCCODE)),PONUM),"")
F13F13=COUNTIFS(COST,G13,TYPE,$D$8,PONUM,$D13)
G13G13=IFERROR(INDEX(COST,MATCH(0,COUNTIF(G12:$G$12,COST)+IF(TYPE<>$D$8,1,0)+IF(PONUM<>$D13,1,0),0)),"")
H13H13=+F13*G13
I13I13=SUMIFS(BEGDEP,TYPE,$D$8,ACCCODE,$D$9,JEV,$C13,PONUM,$D13,COST,$G13)
I14I14=SUMIFS(BEGDEP,TYPE,$D$8,ACCCODE,$D$9,JEV,$C14,PONUM,D13,COST,G13)
I15I15=SUMIFS(BEGDEP,TYPE,$D$8,ACCCODE,$D$9,JEV,$C15,PONUM,D13,COST,G13)
I16I16=SUMIFS(BEGDEP,TYPE,$D$8,ACCCODE,$D$9,JEV,$C16,PONUM,D13,COST,G13)
I17I17=SUMIFS(BEGDEP,TYPE,$D$8,ACCCODE,$D$9,JEV,$C17,PONUM,D13,COST,G13)
L13,L59L13=SUM(H13:K13)
L14:L17L14=L13+(SUM(H14:K14))
H59:K59H59=SUBTOTAL(9,H13:H58)
Press CTRL+SHIFT+ENTER to enter array formulas.
Named Ranges
NameRefers ToCells
Data!_FilterDatabase=Data!$A$5:$P$30I13:I17, D13, F13:G13
ACCCODE=Data!$B$5:$B$30I13:I17, D13
BEGDEP=Data!$H$5:$H$30I13:I17
COST=Data!$G$5:$G$30I13:I17, F13:G13
JEV=Data!$D$5:$D$30I13:I17, B13:C13
PONUM=Data!$C$5:$C$30I13:I17, F13:G13, C13:D13
Tdate=Data!$E$5:$E$30B13
TYPE=Data!$A$5:$A$30I13:I17, D13, F13:G13


Criteria Reference Table
OTC Sample Sheet v9.xlsm
BCDE
62DESC.NO.ROWSTOTAL
63
64PO4315
65
66COST/PO
67271740100
681508184315
692811603210
70140037100
71
72TOTAL40
Testing (2)
Cell Formulas
RangeFormula
C64C64=SUM(--(FREQUENCY(IF(PONUM<>"",IF(TYPE=$D$8,IF(ACCCODE=$D$9,MATCH(PONUM,PONUM,0)))),ROW(PONUM)-ROW(Table2[[#Headers],[PO-Contract Number]])+1)>0))
D64,D67:D70D64=(C64-1)
E64,E67:E70E64=D64*5
B67B67=IFNA(LOOKUP(2,1/((COUNTIF($B66:B$66,PONUM)=0)*($D$8&$D$9=TYPE&ACCCODE)),PONUM),"")
C67:C70C67=SUM(--(FREQUENCY(IF(COST<>"",IF(TYPE=$D$8,IF(ACCCODE=$D$9,IF(PONUM=$B67,MATCH(COST,COST,0))))),ROW(COST)-ROW(Table2[[#Headers],[Acquisition Cost]])+1)>0))
B68:B70B68=IFNA(LOOKUP(2,1/((COUNTIF($B$66:B67,PONUM)=0)*($D$8&$D$9=TYPE&ACCCODE)),PONUM),"")
E72E72=SUM(E64:E71)
Press CTRL+SHIFT+ENTER to enter array formulas.
Named Ranges
NameRefers ToCells
Data!_FilterDatabase=Data!$A$5:$P$30B67:C70, C64
ACCCODE=Data!$B$5:$B$30B67:C70, C64
COST=Data!$G$5:$G$30C67:C70
PONUM=Data!$C$5:$C$30B67:C70, C64
TYPE=Data!$A$5:$A$30B67:C70, C64



and the finished output.
OTC Sample Sheet v9.xlsm
BCDEFGHIJKLMN
12DateReferencePO-Contract NumberDescriptionQty.Unit CostTotal CostAccumulated DepreciationAccumulated Impairment LossesIssues/ Transfers/ Adjustment/sAdjusted CostNature of RepairAmount
1308-Apr-08JAP-08-04-000045271740358,500.00175,500.00-166,725.008,775.00
1431-Aug-23JGL-23-08-000258DEPRECIATION0.008,775.00
1530-Sep-23JGL-23-09-000280DEPRECIATION0.008,775.00
1631-Oct-23JGL-23-10-000249DEPRECIATION0.008,775.00
1730-Nov-23JGL-23-11-000273DEPRECIATION0.008,775.00
1825-Jan-16JGL-16-01-000196150818624,335.00146,010.00-138,709.5016,075.50
1931-Aug-23JGL-23-08-000258DEPRECIATION0.0016,075.50
2030-Sep-23JGL-23-09-000280DEPRECIATION0.0016,075.50
2131-Oct-23JGL-23-10-000249DEPRECIATION0.0016,075.50
2230-Nov-23JGL-23-11-000273DEPRECIATION0.0016,075.50
2325-Jan-16JGL-16-01-000196150818125,000.0025,000.00-23,750.0017,325.50
2431-Aug-23JGL-23-08-000258DEPRECIATION0.0017,325.50
2530-Sep-23JGL-23-09-000280DEPRECIATION0.0017,325.50
2631-Oct-23JGL-23-10-000249DEPRECIATION0.0017,325.50
2730-Nov-23JGL-23-11-000273DEPRECIATION0.0017,325.50
2825-Jan-16JGL-16-01-000196150818152,390.0052,390.00-49,770.5019,945.00
2931-Aug-23JGL-23-08-000258DEPRECIATION0.0019,945.00
3030-Sep-23JGL-23-09-000280DEPRECIATION0.0019,945.00
3131-Oct-23JGL-23-10-000249DEPRECIATION0.0019,945.00
3230-Nov-23JGL-23-11-000273DEPRECIATION0.0019,945.00
3325-Jan-16JGL-16-01-000196150818219,000.0038,000.00-36,100.0021,845.00
3431-Aug-23JGL-23-08-000258DEPRECIATION0.0021,845.00
3530-Sep-23JGL-23-09-000280DEPRECIATION0.0021,845.00
3631-Oct-23JGL-23-10-000249DEPRECIATION0.0021,845.00
3730-Nov-23JGL-23-11-000273DEPRECIATION0.0021,845.00
3830-Apr-09JAP-09-04-0006912811602100,000.00200,000.00-190,000.0031,845.00
3931-Aug-23JGL-23-08-000258DEPRECIATION0.0031,845.00
4030-Sep-23JGL-23-09-000280DEPRECIATION0.0031,845.00
4131-Oct-23JGL-23-10-000249DEPRECIATION0.0031,845.00
4230-Nov-23JGL-23-11-000273DEPRECIATION0.0031,845.00
4330-Apr-09JAP-09-04-0006912811603130,000.00390,000.00-370,500.0051,345.00
4431-Aug-23JGL-23-08-000258DEPRECIATION0.0051,345.00
4530-Sep-23JGL-23-09-000280DEPRECIATION0.0051,345.00
4631-Oct-23JGL-23-10-000249DEPRECIATION0.0051,345.00
4730-Nov-23JGL-23-11-000273DEPRECIATION0.0051,345.00
4830-Apr-09JAP-09-04-000691281160675,000.00450,000.00-427,500.0073,845.00
4931-Aug-23JGL-23-08-000258DEPRECIATION0.0073,845.00
5030-Sep-23JGL-23-09-000280DEPRECIATION0.0073,845.00
5131-Oct-23JGL-23-10-000249DEPRECIATION0.0073,845.00
5230-Nov-23JGL-23-11-000273DEPRECIATION0.0073,845.00
5304-Apr-14JAP-14-04-000042140037188,500.0088,500.00-84,075.0078,270.00
5431-Aug-23JGL-23-08-000258DEPRECIATION0.0078,270.00
5530-Sep-23JGL-23-09-000280DEPRECIATION0.0078,270.00
5631-Oct-23JGL-23-10-000249DEPRECIATION0.0078,270.00
5730-Nov-23JGL-23-11-000273DEPRECIATION0.0078,270.00
58
59GRAND TOTAL1,565,400.00-1,487,130.000.000.0078,270.00
Testing (2)
Cell Formulas
RangeFormula
B13,B53,B48,B43,B38,B33,B28,B23,B18B13=VLOOKUP(C13,JEV:Tdate,2,FALSE)
C13,C53,C48,C43,C38,C33,C28,C23,C18C13=VLOOKUP(D13,PONUM:(JEV),2,FALSE)
D13D13=IFNA(LOOKUP(2,1/((COUNTIF($D12:D$12,PONUM)=0)*($D$8&$D$9=TYPE&ACCCODE)),PONUM),"")
F13,F53,F48,F43,F38,F33,F28,F23,F18F13=COUNTIFS(COST,G13,TYPE,$D$8,PONUM,$D13)
G13G13=IFERROR(INDEX(COST,MATCH(0,COUNTIF(G12:$G$12,COST)+IF(TYPE<>$D$8,1,0)+IF(PONUM<>$D13,1,0),0)),"")
H13,H53,H48,H43,H38,H33,H28,H23,H18H13=+F13*G13
D18,D53,D38D18=IFNA(LOOKUP(2,1/((COUNTIF($D$12:D17,PONUM)=0)*($D$8&$D$9=TYPE&ACCCODE)),PONUM),"")
G18,G53,G48,G43,G38,G33,G28,G23G18=IFERROR(INDEX(COST,MATCH(0,COUNTIF(G$12:$G17,COST)+IF(TYPE<>$D$8,1,0)+IF(PONUM<>$D18,1,0),0)),"")
I13,I53,I48,I43,I38,I33,I28,I23,I18I13=SUMIFS(BEGDEP,TYPE,$D$8,ACCCODE,$D$9,JEV,$C13,PONUM,$D13,COST,$G13)
I14,I54,I49,I44,I39,I34,I29,I24,I19I14=SUMIFS(BEGDEP,TYPE,$D$8,ACCCODE,$D$9,JEV,$C14,PONUM,D13,COST,G13)
I15,I55,I50,I45,I40,I35,I30,I25,I20I15=SUMIFS(BEGDEP,TYPE,$D$8,ACCCODE,$D$9,JEV,$C15,PONUM,D13,COST,G13)
I16,I56,I51,I46,I41,I36,I31,I26,I21I16=SUMIFS(BEGDEP,TYPE,$D$8,ACCCODE,$D$9,JEV,$C16,PONUM,D13,COST,G13)
I17,I57,I52,I47,I42,I37,I32,I27,I22I17=SUMIFS(BEGDEP,TYPE,$D$8,ACCCODE,$D$9,JEV,$C17,PONUM,D13,COST,G13)
L13,L59L13=SUM(H13:K13)
L14:L57L14=L13+(SUM(H14:K14))
H59:K59H59=SUBTOTAL(9,H13:H58)
Press CTRL+SHIFT+ENTER to enter array formulas.
Named Ranges
NameRefers ToCells
Data!_FilterDatabase=Data!$A$5:$P$30I13:I57, D13, D18, D38, D53, F13:G13, F18:G18, F23:G23, F28:G28, F33:G33, F38:G38, F43:G43, F48:G48, F53:G53
ACCCODE=Data!$B$5:$B$30I13:I57, D13, D18, D38, D53
BEGDEP=Data!$H$5:$H$30I13:I57
COST=Data!$G$5:$G$30I13:I57, F13:G13, F18:G18, F23:G23, F28:G28, F33:G33, F38:G38, F43:G43, F48:G48, F53:G53
JEV=Data!$D$5:$D$30I13:I57, B13:C13, B18:C18, B23:C23, B28:C28, B33:C33, B38:C38, B43:C43, B48:C48, B53:C53
PONUM=Data!$C$5:$C$30I13:I57, F13:G13, F18:G18, F23:G23, F28:G28, F33:G33, F38:G38, F43:G43, F48:G48, F53:G53, C13:D13, C18:D18, C23, C28, C33, C38:D38, C43, C48, C53:D53
Tdate=Data!$E$5:$E$30B13, B18, B23, B28, B33, B38, B43, B48, B53
TYPE=Data!$A$5:$A$30I13:I57, D13, D18, D38, D53, F13:G13, F18:G18, F23:G23, F28:G28, F33:G33, F38:G38, F43:G43, F48:G48, F53:G53
 

Attachments

  • Initial Worksheet.PNG
    Initial Worksheet.PNG
    44.1 KB · Views: 3
  • Criteria Reference.PNG
    Criteria Reference.PNG
    5.6 KB · Views: 2
  • After 1st Criteria.PNG
    After 1st Criteria.PNG
    54.1 KB · Views: 3
  • After 2nd Criteria.PNG
    After 2nd Criteria.PNG
    54.1 KB · Views: 2
  • After 3rd Criteria.PNG
    After 3rd Criteria.PNG
    70.6 KB · Views: 4
  • Finished Ouput - Before Saving as PDF.PNG
    Finished Ouput - Before Saving as PDF.PNG
    97.1 KB · Views: 2
  • Reverting Back to Initial Worksheet.PNG
    Reverting Back to Initial Worksheet.PNG
    44.1 KB · Views: 2

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

Forum statistics

Threads
1,215,069
Messages
6,122,956
Members
449,096
Latest member
Anshu121

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