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
 
thanks - but I still don't know how you have formatted your column B (FinalReport) and column AA (Headers) to know why my code breaks only on your machine. Please advise on that. and also how these numbers are generated ? did you type them in manually ?

also, when you say the formula averages the previous 2 rows - I think what you mean is it will average the previous 2 cells in the same column ? Is it correct please ?

Rob
 
Upvote 0

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
In addition to my above request, I updated my code to insert the values (average of 2 cells above each column) or N/A if applicable.

So on my testing with your data (I cannot see the format as I said above), it generates this result for example when 5 was missing :
eg. So cell C26 is the average of cells C24 + C25, D26 is average of D24 + D25 etc.

insert_row.xlsm
ABCDEFGHIJKLM
1
2
3
408/10/2024799.3599.7410099.2110010010099.410094.98N/A
508/10/2024899.3599.8110099.23100100100100100100N/A
608/10/2024999.2699.7110099.23100100100100100100N/A
708/10/20241099.3199.6710097.5910099.92100100100100N/A
808/10/20241199.3599.7610098.56100100100100100100N/A
908/10/20241299.3599.7710098.78100100100100100100N/A
1008/10/20241399.3599.8210098.79100100100100100100N/A
1108/10/20241499.3599.8210098.62100100100100100100N/A
1208/10/20241599.3599.8310099.2100100100100100100N/A
1308/10/20241699.3599.8210099.23100100100100100100N/A
1408/10/20241799.4599.8310099.23100100100100100100N/A
1508/10/20241899.4599.8210099.23100100100100100100N/A
1608/10/20241999.4599.8110099.23100100100100100100N/A
1708/10/20242099.4599.810099.13100100100100100100N/A
1808/10/20242199.4599.7310098.2100100100100100100N/A
1908/10/20242299.4599.6110098.09100100100100100100N/A
2008/10/20242399.4598.7610092.42100100100100100100N/A
2109/10/2024099.4598.7710092.45100100100100100100N/A
2209/10/2024199.4598.7710092.45100100100100100100N/A
2309/10/2024299.4598.8510092.98100100100100100100N/A
2409/10/2024399.3699.8310099.23100100100100100100N/A
2509/10/2024499.3599.8410099.23100100100100100100N/A
2609/10/2024599.35599.83510099.23100100100100100100N/A
2709/10/2024699.3599.7110099.23100100100100100100N/A
28
29
FinalReport

The data in Column B (and Column AA of Headers) for me are just numbers formatted as General.
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, act on it ..
    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, 1) = Sheets("FinalReport").Cells(cdata, 1) 'add date
                Sheets("FinalReport").Cells(cdata + 1, 2) = Aheaders(chdr, 1) 'add header no.
                On Error Resume Next
                For y = 3 To 13
                    If IsError((Sheets("FinalReport").Cells(cdata - 1, y) + Sheets("FinalReport").Cells(cdata, y)) / 2) Then
                        Sheets("FinalReport").Cells(cdata + 1, y) = "N/A"
                        On Error GoTo 0
                    Else
                        Sheets("FinalReport").Cells(cdata + 1, y) = (Sheets("FinalReport").Cells(cdata - 1, y) + Sheets("FinalReport").Cells(cdata, y)) / 2
                    End If
                Next y
            End If
        Next cdata
    End If
Next chdr

End Sub
 
Upvote 0
Solution
@RobP

this is perfect you nail it. thank you for saving hours of task. This is perfect
 
Upvote 0
great .. thanks for the feedback

Glad we could help.

Rob
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,249
Members
452,623
Latest member
Techenthusiast

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