Insert Row

vmjan02

Well-known Member
Joined
Aug 15, 2012
Messages
1,132
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
I have a sheet by name "Headers" and the range from AA2 to AA25 so need to check from this list if any of the missing records found in sheet "FinalReport" from B4 to B28 and if found the vba will insert a row

eg:- now missing record for 5 so now the script will search 4 in FinalReport and then will insert a row after it find the 4 and after insert it i will do the average of 3 & 4

any way to do this. it is taking around 2 hr to do the task manually now.


CST_NW_District_Automation_V1.xlsm
ABCDEFGHIJKLM
42024-10-087.0099.3599.74100.0099.21100.00100.00100.0099.40100.0094.98N/A
52024-10-088.0099.3599.81100.0099.23100.00100.00100.00100.00100.00100.00N/A
62024-10-089.0099.2699.71100.0099.23100.00100.00100.00100.00100.00100.00N/A
72024-10-0810.0099.3199.67100.0097.59100.0099.92100.00100.00100.00100.00N/A
82024-10-0811.0099.3599.76100.0098.56100.00100.00100.00100.00100.00100.00N/A
92024-10-0812.0099.3599.77100.0098.78100.00100.00100.00100.00100.00100.00N/A
102024-10-0813.0099.3599.82100.0098.79100.00100.00100.00100.00100.00100.00N/A
112024-10-0814.0099.3599.82100.0098.62100.00100.00100.00100.00100.00100.00N/A
122024-10-0815.0099.3599.83100.0099.20100.00100.00100.00100.00100.00100.00N/A
132024-10-0816.0099.3599.82100.0099.23100.00100.00100.00100.00100.00100.00N/A
142024-10-0817.0099.4599.83100.0099.23100.00100.00100.00100.00100.00100.00N/A
152024-10-0818.0099.4599.82100.0099.23100.00100.00100.00100.00100.00100.00N/A
162024-10-0819.0099.4599.81100.0099.23100.00100.00100.00100.00100.00100.00N/A
172024-10-0820.0099.4599.80100.0099.13100.00100.00100.00100.00100.00100.00N/A
182024-10-0821.0099.4599.73100.0098.20100.00100.00100.00100.00100.00100.00N/A
192024-10-0822.0099.4599.61100.0098.09100.00100.00100.00100.00100.00100.00N/A
202024-10-0823.0099.4598.76100.0092.42100.00100.00100.00100.00100.00100.00N/A
212024-10-090.0099.4598.77100.0092.45100.00100.00100.00100.00100.00100.00N/A
222024-10-091.0099.4598.77100.0092.45100.00100.00100.00100.00100.00100.00N/A
232024-10-092.0099.4598.85100.0092.98100.00100.00100.00100.00100.00100.00N/A
242024-10-093.0099.3699.83100.0099.23100.00100.00100.00100.00100.00100.00N/A
252024-10-094.0099.3599.84100.0099.23100.00100.00100.00100.00100.00100.00N/A
262024-10-096.0099.3599.71100.0099.23100.00100.00100.00100.00100.00100.00N/A
FinalReport




CST_NW_District_Automation_V1.xlsm
AA
20.00
31.00
42.00
53.00
64.00
75.00
86.00
97.00
108.00
119.00
1210.00
1311.00
1412.00
1513.00
1614.00
1715.00
1816.00
1917.00
2018.00
2119.00
2220.00
2321.00
2422.00
2523.00
Headers
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
I have a sheet by name "Headers" and the range from AA2 to AA25 so need to check from this list if any of the missing records found in sheet "FinalReport" from B4 to B28 and if found the vba will insert a row

eg:- now missing record for 5 so now the script will search 4 in FinalReport and then will insert a row after it find the 4 and after insert it i will do the average of 3 & 4

any way to do this. it is taking around 2 hr to do the task manually now.


CST_NW_District_Automation_V1.xlsm
ABCDEFGHIJKLM
42024-10-087.0099.3599.74100.0099.21100.00100.00100.0099.40100.0094.98N/A
52024-10-088.0099.3599.81100.0099.23100.00100.00100.00100.00100.00100.00N/A
62024-10-089.0099.2699.71100.0099.23100.00100.00100.00100.00100.00100.00N/A
72024-10-0810.0099.3199.67100.0097.59100.0099.92100.00100.00100.00100.00N/A
82024-10-0811.0099.3599.76100.0098.56100.00100.00100.00100.00100.00100.00N/A
92024-10-0812.0099.3599.77100.0098.78100.00100.00100.00100.00100.00100.00N/A
102024-10-0813.0099.3599.82100.0098.79100.00100.00100.00100.00100.00100.00N/A
112024-10-0814.0099.3599.82100.0098.62100.00100.00100.00100.00100.00100.00N/A
122024-10-0815.0099.3599.83100.0099.20100.00100.00100.00100.00100.00100.00N/A
132024-10-0816.0099.3599.82100.0099.23100.00100.00100.00100.00100.00100.00N/A
142024-10-0817.0099.4599.83100.0099.23100.00100.00100.00100.00100.00100.00N/A
152024-10-0818.0099.4599.82100.0099.23100.00100.00100.00100.00100.00100.00N/A
162024-10-0819.0099.4599.81100.0099.23100.00100.00100.00100.00100.00100.00N/A
172024-10-0820.0099.4599.80100.0099.13100.00100.00100.00100.00100.00100.00N/A
182024-10-0821.0099.4599.73100.0098.20100.00100.00100.00100.00100.00100.00N/A
192024-10-0822.0099.4599.61100.0098.09100.00100.00100.00100.00100.00100.00N/A
202024-10-0823.0099.4598.76100.0092.42100.00100.00100.00100.00100.00100.00N/A
212024-10-090.0099.4598.77100.0092.45100.00100.00100.00100.00100.00100.00N/A
222024-10-091.0099.4598.77100.0092.45100.00100.00100.00100.00100.00100.00N/A
232024-10-092.0099.4598.85100.0092.98100.00100.00100.00100.00100.00100.00N/A
242024-10-093.0099.3699.83100.0099.23100.00100.00100.00100.00100.00100.00N/A
252024-10-094.0099.3599.84100.0099.23100.00100.00100.00100.00100.00100.00N/A
262024-10-096.0099.3599.71100.0099.23100.00100.00100.00100.00100.00100.00N/A
FinalReport




CST_NW_District_Automation_V1.xlsm
AA
20.00
31.00
42.00
53.00
64.00
75.00
86.00
97.00
108.00
119.00
1210.00
1311.00
1412.00
1513.00
1614.00
1715.00
1816.00
1917.00
2018.00
2119.00
2220.00
2321.00
2422.00
2523.00
Headers
 
Upvote 0
Sorry, I meant to ask : what do you mean by " i will do the average of 3 & 4"

thanks
Rob
 
Upvote 0
Anyway, in the meantime, try this code (need to put inside a Module rather than on the Sheet).

Caveat is the Header numbers on your Headers sheet must be in numerical order, as you have shown in your data example to us.
It should insert the correct row, and will also add the Header number to the new row in order you can identify it easily in case it appears on the row after your last piece of data ends.
eg. if "7" was missing, you would insert a row after "6" (meaning a row at the bottom of your data). Ordinarily you would not see that as an inserted row without adding the header number in.

Hope its okay for you.

Rob

VBA Code:
Public chdr, cdata, x, y, lr, hlr As Long
Public Adata, Aheaders As Variant

Sub missing_headers()

lr = Sheets("FinalReport").Cells(Rows.Count, "A").End(xlUp).Row
hlr = Sheets("Headers").Cells(Rows.Count, "AA").End(xlUp).Row

ReDim Adata(4 To lr) 'array now mirrors row numbers starting row4

Sheets("Headers").Select 'load the array with Header numbers  NB. Header No's must be in sequence on Headers sheet..
Aheaders = Sheets("Headers").Range(Cells(2, 27), Cells(hlr, 27)).Value '27 = Col "AA"

Sheets("FinalReport").Select 'load the array with FinalReport data
For x = 4 To lr
    Adata(x) = Sheets("FinalReport").Range("B" & x).Value
Next x

For chdr = UBound(Aheaders) To 1 Step -1  'count through header numbers backwards
    found = 0
    For cdata = UBound(Adata) To 4 Step -1  'check through data values backwards
        If Aheaders(chdr, 1) = Adata(cdata) Then
            found = 1
            Exit For
        End If
    Next cdata
    
    ' Header is missing, so search for previous header no. and insert row & missed header no.
    If found = 0 Then
        For cdata = lr To 4 Step -1
            If Aheaders(chdr - 1, 1) = Adata(cdata) Then
                Sheets("FinalReport").Cells(cdata + 1, 1).EntireRow.Insert
                Sheets("FinalReport").Cells(cdata + 1, 2) = Aheaders(chdr, 1)
            End If
        Next cdata
    End If
Next chdr

End Sub
 
Upvote 0
Excel Formula:
            If Aheaders(chdr - 1, 1) = Adata(cdata) Then   'getting error as runtime error 9


I tried to do for missing 5 and missing 1 it put me error
missing record can be multipay or any one or no missing.

1. Once the new row is added it will do the average of pervious 2 rows any error on the formula then will display "N/A"
eg:- if new row is added in row 20 then the formula will be =iferror(average(r18,r19),"N/A") this formula will be from column C to M formula to in values
eg:- if new row is added in row 22 then the formula will be =iferror(average(r20,r21),"N/A") this formula will be from column C to M formula to in values

2. After added the new row step 1 it will just copy and paste the pervious cell in column A
eg:- if new row is added in row 20 then then in column A copy cell A19 and pastie it in newly add row 20 cell A20
eg:- if new row is added in row 22 then then in column A copy cell A21 and pastie it in newly add row 22 cell A22

I hope now i am clear on this



CST_NW_District_Automation_V1.xlsm
ABCDEFGHIJKLM
42024-10-096.0099.3599.71100.0099.23100.00100.00100.00100.00100.00100.00N/A
52024-10-097.0099.3299.79100.0099.04100.00100.00100.00100.00100.00100.00N/A
62024-10-098.0099.2699.83100.0099.18100.00100.00100.00100.00100.00100.00N/A
72024-10-099.0099.2699.83100.0099.23100.00100.00100.00100.00100.00100.00N/A
82024-10-0910.0099.2699.82100.0099.23100.00100.00100.00100.00100.00100.00N/A
92024-10-0911.0099.2699.82100.0099.23100.00100.00100.00100.00100.00100.00N/A
102024-10-0912.0099.2699.77100.0099.23100.00100.00100.00100.00100.00100.00N/A
112024-10-0913.0099.2699.77100.0099.19100.00100.00100.00100.00100.00100.00N/A
122024-10-0914.0099.2299.8099.0498.98100.00100.00100.00100.00100.00100.00N/A
132024-10-0915.0099.2699.78100.0098.89100.0099.88100.00100.00100.00100.00N/A
142024-10-0916.0099.2699.80100.0099.10100.0099.53100.00100.00100.00100.00N/A
152024-10-0917.0099.2699.82100.0099.23100.00100.00100.00100.00100.0099.83N/A
162024-10-0918.0099.2699.83100.0099.23100.00100.00100.00100.00100.00100.00N/A
172024-10-0919.0099.2699.84100.0099.23100.00100.00100.00100.00100.00100.00N/A
182024-10-0920.0099.2699.82100.0099.23100.00100.00100.00100.00100.00100.00N/A
192024-10-0921.0099.2699.81100.0099.23100.00100.00100.00100.00100.00100.00N/A
202024-10-0922.0099.2699.82100.0099.23100.0099.98100.00100.00100.00100.00N/A
212024-10-0923.0099.2699.81100.0099.23100.0099.86100.00100.00100.00100.00N/A
222024-10-101.0099.2699.77100.0099.23100.0099.78100.00100.00100.00100.00N/A
232024-10-102.0099.0899.76100.0099.23100.0099.76100.00100.00100.00100.00N/A
242024-10-103.0098.7199.77100.0099.23100.0099.76100.00100.0094.70100.00N/A
252024-10-104.0098.7899.77100.0099.23100.0099.76100.00100.0099.24100.00N/A
262024-10-106.0098.7199.73100.0099.23100.0099.49100.00100.00100.00100.00N/A
FinalReport
 
Upvote 0
Strange, seemed to work for me, but keep in mind when I copy your data across to try it, the formatting is done as "General". What is your column "B" and Colum "AA" on the other sheet formatted as can I ask please ?

Rob
 
Upvote 0
sorry, let me summery it so its easy,

the aim of the script is to add any missing entry or entry's


1. The column AA is the list of data (screen shot 1)
I have kept to check if any missing records from this list need to be added as new row in sheet FinalReport


2. The script will do these steps a and b in Final Report
step a. Once the new row is added it will do the average of pervious 2 rows any error on the formula then will display "N/A"
eg:- if new row is added in row 20 then the formula will be =iferror(average(r18,r19),"N/A") this formula will be from column C to M formula to in values
eg:- if new row is added in row 22 then the formula will be =iferror(average(r20,r21),"N/A") this formula will be from column C to M formula to in values

step b. After added the new row step 1 it will just copy and paste the pervious cell in column A
eg:- if new row is added in row 20 then then in column A copy cell A19 and pastie it in newly add row 20 cell A20
eg:- if new row is added in row 22 then then in column A copy cell A21 and pastie it in newly add row 22 cell A22

I hope now i am clear. Tried my best to explain the requirement :)




CST_NW_District_Automation_V2.xlsm
AA
1Check
20.00
31.00
42.00
53.00
64.00
75.00
86.00
97.00
108.00
119.00
1210.00
1311.00
1412.00
1513.00
1614.00
1715.00
1816.00
1917.00
2018.00
2119.00
2220.00
2321.00
2422.00
2523.00
Headers





CST_NW_District_Automation_V2.xlsm
ABCDEFGHIJKLM
42024-10-087.0099.3599.74100.0099.21100.00100.00100.0099.40100.0094.98N/A
52024-10-088.0099.3599.81100.0099.23100.00100.00100.00100.00100.00100.00N/A
62024-10-089.0099.2699.71100.0099.23100.00100.00100.00100.00100.00100.00N/A
72024-10-0810.0099.3199.67100.0097.59100.0099.92100.00100.00100.00100.00N/A
82024-10-0811.0099.3599.76100.0098.56100.00100.00100.00100.00100.00100.00N/A
92024-10-0812.0099.3599.77100.0098.78100.00100.00100.00100.00100.00100.00N/A
102024-10-0813.0099.3599.82100.0098.79100.00100.00100.00100.00100.00100.00N/A
112024-10-0814.0099.3599.82100.0098.62100.00100.00100.00100.00100.00100.00N/A
122024-10-0815.0099.3599.83100.0099.20100.00100.00100.00100.00100.00100.00N/A
132024-10-0816.0099.3599.82100.0099.23100.00100.00100.00100.00100.00100.00N/A
142024-10-0817.0099.4599.83100.0099.23100.00100.00100.00100.00100.00100.00N/A
152024-10-0818.0099.4599.82100.0099.23100.00100.00100.00100.00100.00100.00N/A
162024-10-0819.0099.4599.81100.0099.23100.00100.00100.00100.00100.00100.00N/A
172024-10-0820.0099.4599.80100.0099.13100.00100.00100.00100.00100.00100.00N/A
182024-10-0821.0099.4599.73100.0098.20100.00100.00100.00100.00100.00100.00N/A
192024-10-0822.0099.4599.61100.0098.09100.00100.00100.00100.00100.00100.00N/A
202024-10-0823.0099.4598.76100.0092.42100.00100.00100.00100.00100.00100.00N/A
212024-10-091.0099.4598.77100.0092.45100.00100.00100.00100.00100.00100.00N/A
222024-10-092.0099.4598.85100.0092.98100.00100.00100.00100.00100.00100.00N/A
232024-10-093.0099.3699.83100.0099.23100.00100.00100.00100.00100.00100.00N/A
242024-10-094.0099.3599.84100.0099.23100.00100.00100.00100.00100.00100.00N/A
252024-10-095.0099.3699.84100.0099.23100.00100.00100.00100.00100.00100.00N/A
262024-10-096.0099.3599.71100.0099.23100.00100.00100.00100.00100.00100.00N/A
FinalReport






1728560463021.png
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,244
Members
452,622
Latest member
Laura_PinksBTHFT

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