Help with Inserting Multiple Data Rows (same data) after change in Value Occurs

Dinah

New Member
Joined
Sep 17, 2012
Messages
10
Hello,

I am trying to create a VBA statement in which 5 rows with data are input between values. The first and last of these should be gray if possible and the text will be in the middle 3 rows. For example:


Before:
COLUMN A COLUMN B
STATION REGION
5A TRO
5A TRO
5A TRO
5B TRO
5B TRO



End Result:
COLUMN A COLUMN B
STATION REGION
5A TRO
5A TRO
5A TRO
BLANK LINE COLORED GRAY
OBSERVATIONS
VIOLATIONS
TOTAL
BLANK LINE COLORED GRAY
5B TRO
5B TRO
BLANK LINE COLORED GRAY
OBSERVATIONS
VIOLATIONS
TOTAL
BLANK LINE COLORED GRAY
 
Hi hiker95,

I have now uploaded the xlsx sheet to BOX. Thank you for that, so easy. This is the link:

https://www.box.com/s/38nnpcxvli8wzvvzgdmd

When you open the document you will see (hopefully if formatting is the same) three red highlighted fields (Field Z = FDT_FIELD_, FIELD AB = FDT_DO_PRO, FIELD AL = ENTEROCOCC). These are the three I need to calculate for the entirety of the document with the formulas within the cells Z5: Z7, AB5:AB7 and AL5:AL7).

If you can't access the forumla this is what I'm looking for. I'm sure if I have code for one column I could try to make sense of the others - should be very similar.

Observations =COUNT(Z2:Z3) *Count number of occurences until change of value / station)*
Viol =SUM(COUNTIF(Z2:Z3,"<6"),COUNTIF(Z2:Z3,">9")) *Sum of all less than 6 and greater than 9 , account for all until change of station*
Viol Rate =(Z6/Z5)*100 *Viol/Observations x 100 for percentage outcome*


Below I have your modified code, of course the request I have now can be separate or anyway that is simpler. Thank you for any help!
***********************
Code used previously:
Option Explicit
Sub InsertBetweenV2()</SPAN>
' r is a counter</SPAN></SPAN>
' lr is the lastrow</SPAN></SPAN>
' i is an array to hold the inserted text</SPAN></SPAN>
Dim r As Long, lr As Long, i</SPAN>
' turn screen updating OFF</SPAN></SPAN>
Application.ScreenUpdating = False</SPAN>

' initialize the i array</SPAN></SPAN>
i = Array("", "OBS", "VIOL", "VIOL RATE", "STATEMENT", "")</SPAN>


' find the last used row in column "A" and add one to lr</SPAN></SPAN>
lr = Cells(Rows.Count, 1).End(xlUp).Row + 1</SPAN>

' when inserting/deleting rows it is good practice to start from the bottom and go up</SPAN></SPAN>
' loop from one cell down from the last cell in column A to row 3</SPAN></SPAN>
' the "Step - 1" means loop in reverse down to up</SPAN></SPAN>
For r = lr To 3 Step -1</SPAN>

' if cell A7 is not equal to cell A6 then</SPAN></SPAN>
If Cells(r, 1) <> Cells(r - 1, 1) Then</SPAN>

' at cell A7 insert 5 blank rows</SPAN></SPAN>
Rows(r).Resize(6).Insert</SPAN>

' transpose the i ary at A7</SPAN></SPAN>
Cells(r, 1).Resize(6).Value = Application.Transpose(i)</SPAN>

' the first inserted row is blank and its interior color should be GRAY</SPAN></SPAN>
Cells(r, 1).Resize(, 46).Interior.ColorIndex = 15</SPAN>

' in the six inserted rows, the three rows with the inserted text,</SPAN></SPAN>
' row 2 thru row 5 should have the text as BOLD</SPAN></SPAN>
Cells(r + 1, 1).Resize(4).Font.Bold = True</SPAN>

' the last of the five inserted rows is blank and its interior color should be GRAY</SPAN></SPAN>
Cells(r + 5</SPAN>, 1).Resize(, 46).Interior.ColorIndex = 15</SPAN>
End If</SPAN>
Next r</SPAN>

' auto fit column A the the widest text string added</SPAN></SPAN>
Columns(1).AutoFit</SPAN>

' turn screen updatting back ON</SPAN></SPAN>
Application.ScreenUpdating = True</SPAN>
End Sub</SPAN>
 
Upvote 0

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Dinah,

It will be easier to put the formulae in while the macro is running.

Please post another workbook with the raw data in Sheet1, and the results (per worksheet Chowan_Class_II_Estuarine) in another worksheet.
 
Upvote 0
Dinah,


Sample raw data in the first worksheet in the workbook (not all 1568 rows are shown):


Excel Workbook
AZABALAT
1STATIONFDT_FIELD_FDT_DO_PROENTEROCOCCPHOSPHORUS
25ABLW000.307.200000000005.200000000000.000000000000.14000000000
35ABLW000.306.200000000005.800000000000.000000000000.10000000000
45ABLW001.106.720000000007.400000000000.000000000000.10000000000
55ABLW001.106.600000000005.200000000000.000000000000.07000000000
65ABLW001.106.200000000005.900000000000.000000000000.10000000000
75ABLW001.106.600000000004.000000000000.000000000000.10000000000
85ABLW001.106.300000000006.600000000000.000000000000.00000000000
95ABLW009.806.940000000009.350000000000.000000000000.03000000000
105ABLW009.806.600000000003.890000000000.000000000000.11000000000
115ABLW009.806.790000000002.330000000000.000000000000.11000000000
125ABLW009.806.520000000003.120000000000.000000000000.10000000000
135ABLW009.806.890000000005.520000000000.000000000000.08000000000
145ABLW009.806.5800000000010.490000000000.000000000000.04000000000
155ABLW009.807.1000000000012.100000000000.000000000000.08000000000
165ABLW009.806.700000000005.700000000000.000000000000.07000000000
175ABLW009.800.000000000000.000000000000.000000000000.11000000000
185ABLW009.806.400000000002.700000000000.000000000000.08000000000
195ABLW009.806.600000000005.100000000000.000000000000.07000000000
205ABLW009.806.400000000007.000000000000.000000000000.05000000000
215ABLW009.806.7000000000011.500000000000.000000000000.06000000000
225ABLW009.806.600000000007.200000000000.000000000000.06000000000
235ABLW009.806.200000000005.100000000000.000000000000.12000000000
245ABLW009.806.600000000003.700000000000.000000000000.09000000000
255ABLW009.806.700000000003.900000000000.000000000000.07000000000
265ABLW009.806.800000000005.700000000000.000000000000.12000000000
275ABLW009.806.4000000000011.400000000000.000000000000.04000000000
285ABLW009.806.100000000008.000000000000.000000000000.12000000000
295ABLW009.805.900000000005.100000000000.000000000000.09000000000
305ABLW009.806.300000000002.400000000000.000000000000.16000000000
315ABLW009.806.700000000002.700000000000.000000000000.15000000000
325ABLW009.806.400000000003.400000000000.000000000000.08000000000
335ABLW009.806.3000000000010.900000000000.000000000000.04000000000
345ABLW009.806.300000000009.300000000000.000000000000.06000000000
355ABLW009.806.400000000003.400000000000.000000000000.08000000000
365ABLW009.806.500000000002.000000000000.000000000000.12000000000
375ABLW009.806.600000000002.800000000000.000000000000.13000000000
385ABLW009.806.200000000006.200000000000.000000000000.05000000000
395ABLW009.806.7000000000010.600000000000.000000000000.04000000000
405ABLW009.806.700000000007.600000000000.000000000000.06000000000
415ABLW009.806.500000000002.000000000000.000000000000.14000000000
425ABLW009.807.100000000003.300000000000.000000000000.12000000000
435ABLW009.806.000000000006.000000000000.000000000000.08000000000
Sheet1





After the macro:


Excel Workbook
AZABALAT
1STATIONFDT_FIELD_FDT_DO_PROENTEROCOCCPHOSPHORUS
25ABLW000.307.2005.2000.0000.14000000000
35ABLW000.306.2005.8000.0000.10000000000
4
5OBS2.0002.0002.000
6VIOL0.0001.0000.000
7VIOL RATE0.00050.0000.000
8STATEMENT
9
105ABLW001.106.7207.4000.0000.10000000000
115ABLW001.106.6005.2000.0000.07000000000
125ABLW001.106.2005.9000.0000.10000000000
135ABLW001.106.6004.0000.0000.10000000000
145ABLW001.106.3006.6000.0000.00000000000
15
16OBS5.0005.0005.000
17VIOL0.0000.0000.000
18VIOL RATE0.0000.0000.000
19STATEMENT
20
215ABLW009.806.9409.3500.0000.03000000000
225ABLW009.806.6003.8900.0000.11000000000
235ABLW009.806.7902.3300.0000.11000000000
245ABLW009.806.5203.1200.0000.10000000000
255ABLW009.806.8905.5200.0000.08000000000
265ABLW009.806.58010.4900.0000.04000000000
275ABLW009.807.10012.1000.0000.08000000000
285ABLW009.806.7005.7000.0000.07000000000
295ABLW009.800.0000.0000.0000.11000000000
305ABLW009.806.4002.7000.0000.08000000000
315ABLW009.806.6005.1000.0000.07000000000
325ABLW009.806.4007.0000.0000.05000000000
335ABLW009.806.70011.5000.0000.06000000000
345ABLW009.806.6007.2000.0000.06000000000
355ABLW009.806.2005.1000.0000.12000000000
365ABLW009.806.6003.7000.0000.09000000000
375ABLW009.806.7003.9000.0000.07000000000
385ABLW009.806.8005.7000.0000.12000000000
395ABLW009.806.40011.4000.0000.04000000000
405ABLW009.806.1008.0000.0000.12000000000
415ABLW009.805.9005.1000.0000.09000000000
425ABLW009.806.3002.4000.0000.16000000000
435ABLW009.806.7002.7000.0000.15000000000
445ABLW009.806.4003.4000.0000.08000000000
455ABLW009.806.30010.9000.0000.04000000000
465ABLW009.806.3009.3000.0000.06000000000
475ABLW009.806.4003.4000.0000.08000000000
485ABLW009.806.5002.0000.0000.12000000000
495ABLW009.806.6002.8000.0000.13000000000
505ABLW009.806.2006.2000.0000.05000000000
515ABLW009.806.70010.6000.0000.04000000000
525ABLW009.806.7007.6000.0000.06000000000
535ABLW009.806.5002.0000.0000.14000000000
545ABLW009.807.1003.3000.0000.12000000000
555ABLW009.806.0006.0000.0000.08000000000
56
57OBS35.00035.00035.000
58VIOL2.00015.0000.000
59VIOL RATE5.71442.8570.000
60STATEMENT
61
Sheet1





Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


Code:
Option Explicit
Sub InsertBetweenV3()
' hiker95, 09/24/2012
' http://www.mrexcel.com/forum/excel-questions/659755-help-inserting-multiple-data-rows-same-data-after-change-value-occurs-2.html
Dim Area As Range
Dim r As Long, lr As Long, sr As Long, er As Long, i
Application.ScreenUpdating = False
i = Array("", "OBS", "VIOL", "VIOL RATE", "STATEMENT", "")
Worksheets(1).Activate
lr = Cells(Rows.Count, 1).End(xlUp).Row
For r = lr To 3 Step -1
  If Cells(r, 1) <> Cells(r - 1, 1) Then
    Rows(r).Resize(6).Insert
  End If
Next r
lr = Cells(Rows.Count, 1).End(xlUp).Row
For Each Area In Range("A2", Range("A" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeConstants).Areas
  With Area
    sr = .Row
    er = sr + .Rows.Count - 1
    Cells(er + 1, 1).Resize(6).Value = Application.Transpose(i)
    Cells(er + 1, 1).Resize(, 46).Interior.ColorIndex = 15
    Cells(er + 2, 1).Resize(4).Font.Bold = True
    Cells(er + 6, 1).Resize(, 46).Interior.ColorIndex = 15
    Range("Z" & er + 2).Formula = "=COUNT(Z" & sr & ":Z" & er & ")"
    Range("Z" & er + 3).Formula = "=SUM(COUNTIF(Z" & sr & ":Z" & er & ", ""<6""),COUNTIF(Z" & sr & ":Z" & er & ","">9""))"
    Range("Z" & er + 4).Formula = "=(Z" & er + 3 & "/Z" & er + 2 & ")*100"
    Range("AB" & er + 2).Formula = "=COUNT(AB" & sr & ":AB" & er & ")"
    Range("AB" & er + 3).Formula = "=COUNTIF(AB" & sr & ":AB" & er + 2 & ",""<4"")"
    Range("AB" & er + 4).Formula = "=(AB" & er + 3 & "/AB" & er + 2 & ")*100"
    Range("AL" & er + 2).Formula = "=COUNT(AL" & sr & ":AL" & er & ")"
    Range("AL" & er + 3).Formula = "=COUNTIF(AL" & sr & ":AL" & er + 2 & ","">104"")"
    Range("AL" & er + 4).Formula = "=(AL" & er + 3 & "/AL" & er + 2 & ")*100"
  End With
Next Area
lr = Cells(Rows.Count, 1).End(xlUp).Row
Range("Z2:Z" & lr).NumberFormat = "0.000"
Range("AB2:AB" & lr).NumberFormat = "0.000"
Range("AL2:AL" & lr).NumberFormat = "0.000"
Application.ScreenUpdating = True
End Sub


Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm


Then run the InsertBetweenV3 macro.
 
Upvote 0
hiker95,

Thank you that is wonderful! I only had to make 2 changes. I added another column (AI to be included). However, the 'COUNT' function is including values of '0' in it's count. In the middle or writing to ask for more help - I figured it out! Here is my update:

Option Explicit</SPAN>
Sub InsertBetweenV3()</SPAN>
Dim Area As Range</SPAN>
Dim r As Long, lr As Long, sr As Long, er As Long, i</SPAN>
Application.ScreenUpdating = False</SPAN>
i = Array("", "OBS", "VIOL", "VIOL RATE", "STATEMENT", "")</SPAN>
Worksheets(1).Activate</SPAN>
lr = Cells(Rows.Count, 1).End(xlUp).Row</SPAN>
For r = lr To 3 Step -1</SPAN>
If Cells(r, 1) <> Cells(r - 1, 1) Then</SPAN>
Rows(r).Resize(6).Insert</SPAN>
End If</SPAN>
Next r</SPAN>
lr = Cells(Rows.Count, 1).End(xlUp).Row</SPAN>
For Each Area In Range("A2", Range("A" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeConstants).Areas</SPAN>
With Area</SPAN>
sr = .Row</SPAN>
er = sr + .Rows.Count - 1</SPAN>
Cells(er + 1, 1).Resize(6).Value = Application.Transpose(i)</SPAN>
Cells(er + 1, 1).Resize(, 46).Interior.ColorIndex = 15</SPAN>
Cells(er + 2, 1).Resize(4).Font.Bold = True</SPAN>
Cells(er + 6, 1).Resize(, 46).Interior.ColorIndex = 15</SPAN>
Range("Z" & er + 2).Formula = "=COUNTIF(Z" & sr & ":Z" & er &","">0"")"</SPAN></SPAN>
Range("Z" & er + 3).Formula = "=SUM(COUNTIF(Z" & sr & ":Z" & er & ", ""<6""),COUNTIF(Z" & sr & ":Z" & er & ","">9""))"</SPAN></SPAN>
Range("Z" & er + 4).Formula = "=(Z" & er + 3 & "/Z" & er + 2 & ")*100"</SPAN>
Range("AB" & er + 2).Formula = "=COUNTIF(AB" & sr & ":AB" & er &","">0"")"</SPAN></SPAN>
Range("AB" & er + 3).Formula = "=COUNTIF(AB" & sr & ":AB" & er & ",""<4"")"</SPAN></SPAN>
Range("AB" & er + 4).Formula = "=(AB" & er + 3 & "/AB" & er + 2 & ")*100"</SPAN>
Range("AI" & er + 2).Formula = "=COUNTIF(AI" & sr & ": AI" & er & ","">0"")"</SPAN></SPAN>
Range("AI" & er + 3).Formula = "=COUNTIF(AI" & sr & ": AI" & er & ",""<4"")"</SPAN></SPAN>
Range("AI" & er + 4).Formula = "=( AI" & er + 3 & "/ AI" & er + 2 & ")*100"</SPAN></SPAN>
Range("AL" & er + 2).Formula = "=COUNTIF(AL" & sr & ":AL" & er & ","">0"")"</SPAN></SPAN>
Range("AL" & er + 3).Formula = "=COUNTIF(AL" & sr & ":AL" & er & ","">104"")"</SPAN></SPAN>
Range("AL" & er + 4).Formula = "=(AL" & er + 3 & "/AL" & er + 2 & ")*100"</SPAN>
End With</SPAN>
Next Area</SPAN>
lr = Cells(Rows.Count, 1).End(xlUp).Row</SPAN>
Range("Z2:Z" & lr).NumberFormat = "0.000"</SPAN>
Range("AB2:AB" & lr).NumberFormat = "0.000"</SPAN>
Range("AI2:AI" & lr).NumberFormat = "0.000"</SPAN>
Range("AL2:AL" & lr).NumberFormat = "0.000"</SPAN>
Application.ScreenUpdating = True</SPAN>
End Sub</SPAN>


It may be a pain but if you ever have time would you write the meaning of the code per line. I'm sorry I don't know the right terminology. Again, thank you this has been a TREMENDOUS help. Let me know if I can give you points or whatever the reward here is!

Dinah
 
Upvote 0
Dinah,

would you write the meaning of the code per line


See below in bold the meaning of the code:


Rich (BB code):
Option Explicit
Sub InsertBetweenV3()
' hiker95, 09/24/2012
' http://www.mrexcel.com/forum/excel-questions/659755-help-inserting-multiple-data-rows-same-data-after-change-value-occurs-2.html
Dim Area As Range
Dim r As Long, lr As Long, sr As Long, er As Long, i

' turn off screen updating
Application.ScreenUpdating = False

' create an array to fill the 6 inserted rows
i = Array("", "OBS", "VIOL", "VIOL RATE", "STATEMENT", "")

' activate/select the first worksheet
Worksheets(1).Activate

' lr is for last row.  find the last row in column 1 = column A
lr = Cells(Rows.Count, 1).End(xlUp).Row

' when we are insertine/deleting rows we usually start from the bottom up
For r = lr To 3 Step -1

  ' Range("A" & r) is not equal to Range("A" & r - 1)
  ' If A1535 is not equal to A1534 Then
  If Cells(r, 1) <> Cells(r - 1, 1) Then
  
    ' insert 6 rows
    Rows(r).Resize(6).Insert
  End If
Next r

' now that we have inserted six empty rows for each change in STATION
' find the new last row in column 1 = column A
lr = Cells(Rows.Count, 1).End(xlUp).Row

' for each Area in range A1:A new last row
' Area will find each group of rows between the inserted 6 rows
For Each Area In Range("A2", Range("A" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeConstants).Areas

  ' with each Area
  With Area
    
    ' sr a variable for startrow
    ' the .Row of the Area is the first row of the Area
    sr = .Row
    
    ' er a variable for endrow
    ' is equal to sr + count of rows in the Area - 1
    ' er = sr + .Rows.Count - 1
    er = sr + .Rows.Count - 1
    
    ' beginning in the blank inserted 6 rows
    ' transpose the i array vertically
    Cells(er + 1, 1).Resize(6).Value = Application.Transpose(i)
    
    ' in the first blank row change the interior color to Gray
    '   from column 1 = column A to column 46 = column AT
    Cells(er + 1, 1).Resize(, 46).Interior.ColorIndex = 15
    
    ' bold the text inserted from the i array
    Cells(er + 2, 1).Resize(4).Font.Bold = True
    
    ' in the last blank row change the interior color to Gray
    '   from column 1 = column A to column 46 = column AT
    Cells(er + 6, 1).Resize(, 46).Interior.ColorIndex = 15
    
    ' put the formulae in the appropriate cells to do the calculations
    Range("Z" & er + 2).Formula = "=COUNT(Z" & sr & ":Z" & er & ")"
    Range("Z" & er + 3).Formula = "=SUM(COUNTIF(Z" & sr & ":Z" & er & ", ""<6""),COUNTIF(Z" & sr & ":Z" & er & ","">9""))"
    Range("Z" & er + 4).Formula = "=(Z" & er + 3 & "/Z" & er + 2 & ")*100"
    Range("AB" & er + 2).Formula = "=COUNT(AB" & sr & ":AB" & er & ")"
    Range("AB" & er + 3).Formula = "=COUNTIF(AB" & sr & ":AB" & er + 2 & ",""<4"")"
    Range("AB" & er + 4).Formula = "=(AB" & er + 3 & "/AB" & er + 2 & ")*100"
    Range("AL" & er + 2).Formula = "=COUNT(AL" & sr & ":AL" & er & ")"
    Range("AL" & er + 3).Formula = "=COUNTIF(AL" & sr & ":AL" & er + 2 & ","">104"")"
    Range("AL" & er + 4).Formula = "=(AL" & er + 3 & "/AL" & er + 2 & ")*100"
  End With
Next Area

' find the last row in column 1 = column A
lr = Cells(Rows.Count, 1).End(xlUp).Row

' in the following ranges change the number format
Range("Z2:Z" & lr).NumberFormat = "0.000"
Range("AB2:AB" & lr).NumberFormat = "0.000"
Range("AL2:AL" & lr).NumberFormat = "0.000"

' turn back on screen updating
Application.ScreenUpdating = True
End Sub
 
Upvote 0
hiker95,

If you can, will you help with a simple addition to the code? I'm trying to now do a count with the same numbers in my three columns (e.g. =SUM(COUNTIF(Z387:Z424,"<6"),COUNTIF(Z387:Z424,">9"). The only problem is that it is counting my zero values which I don't need. I tried coming up with something like this:
=SUM(COUNTIF(Z387:Z424,"<6"),COUNTIF(Z387:Z424,">9")-COUNT(Z387:Z424,"0"))
It is not working. I'm trying to use <> as 'not equal to zero' but that also just counts this in my total sum and i get a very large number. Any thoughts? Here is the code to this point...I put a note in on the line of code that needs a zero value not included in the sum and count. I can add the other columns if i know how to do one column. Any ideas??

Option Explicit</SPAN>
Sub InsertBetweenV3()</SPAN>
Dim Area As Range</SPAN>
Dim r As Long, lr As Long, sr As Long, er As Long, i</SPAN>

‘ turn off screen updating</SPAN></SPAN>
Application.ScreenUpdating = False</SPAN>

‘ create an array to fill the 6 inserted rows</SPAN></SPAN>
i = Array("", "OBS", "VIOL", "VIOL RATE", "STATEMENT", "")</SPAN>

‘ activate/select the first worksheet</SPAN></SPAN>
Worksheets(1).Activate</SPAN>

‘ lr is for last row. Find the last row in column 1 = column A</SPAN></SPAN>
lr = Cells(Rows.Count, 1).End(xlUp).Row</SPAN>

‘ when we are inserting/deleting rows we usually start from the bottom up </SPAN></SPAN>
For r = lr To 3 Step -1</SPAN>

‘ Range(“A” & r) is not equal to Range(“A” & r - 1)</SPAN></SPAN>
' If A1535 is not equal to A1534 Then</SPAN></SPAN>
If Cells(r, 1) <> Cells(r - 1, 1) Then</SPAN>

' insert 6 rows</SPAN></SPAN>
Rows(r).Resize(6).Insert</SPAN>
End If</SPAN>
Next r</SPAN>

' now that we have inserted six empty rows for each change in STATION</SPAN></SPAN>
' find the new last row in column 1 = column A</SPAN></SPAN>
lr = Cells(Rows.Count, 1).End(xlUp).Row</SPAN>

' for each Area in range A1:A new last row</SPAN></SPAN>
' Area will find each group of rows between the inserted 6 rows</SPAN></SPAN>
For Each Area In Range("A2", Range("A" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeConstants).Areas</SPAN>
‘ with each Area </SPAN></SPAN>
With Area</SPAN>

‘ sr a variable for start row</SPAN></SPAN>
‘ the .Row of the Area is the first row of the Area sr = .Row</SPAN></SPAN>
sr = .Row</SPAN>

' er a variable for end row</SPAN></SPAN>
' is equal to sr + count of rows in the Area - 1</SPAN></SPAN>
' er = sr + .Rows.Count - 1</SPAN></SPAN>

er = sr + .Rows.Count – 1</SPAN>

' beginning in the blank inserted 6 rows</SPAN></SPAN>
' transpose the i array vertically</SPAN></SPAN>
Cells(er + 1, 1).Resize(6).Value = Application.Transpose(i)</SPAN>

' in the first blank row change the interior color to Gray</SPAN></SPAN>
' from column 1 = column A to column 46 = column AT</SPAN></SPAN>
Cells(er + 1, 1).Resize(, 46).Interior.ColorIndex = 15</SPAN>

' bold the text inserted from the i array</SPAN></SPAN>
Cells(er + 2, 1).Resize(4).Font.Bold = True</SPAN>

' in the last blank row change the interior color to Gray</SPAN></SPAN>
' from column 1 = column A to column 46 = column AT</SPAN></SPAN>
Cells(er + 6, 1).Resize(, 46).Interior.ColorIndex = 15</SPAN>

' put the formulae in the appropriate cells to do the calculations</SPAN></SPAN>
Range("Z" & er + 2).Formula = "=COUNTIF(Z" & sr & ":Z" & er &","">0"")"</SPAN>
(THIS IS WHERE zero values should not be included) Range("Z" & er + 3).Formula = "=SUM(COUNTIF(Z" & sr & ":Z" & er & ", ""<6""),COUNTIF(Z" & sr & ":Z" & er & ","">9""))"</SPAN> Range("Z" & er + 4).Formula = "=(Z" & er + 3 & "/Z" & er + 2 & ")*100"</SPAN>
Range("AB" & er + 2).Formula = "=COUNTIF(AB" & sr & ":AB" & er &","">0"")"</SPAN>
Range("AB" & er + 3).Formula = "=COUNTIF(AB" & sr & ":AB" & er & ",""<4"")"</SPAN>
Range("AB" & er + 4).Formula = "=(AB" & er + 3 & "/AB" & er + 2 & ")*100"</SPAN>
Range("AI" & er + 2).Formula = "=COUNTIF(AI" & sr & ": AI" & er & ","">0"")"</SPAN>
Range("AI" & er + 3).Formula = "=COUNTIF(AI" & sr & ": AI" & er & ",""<4"")"</SPAN>
Range("AI" & er + 4).Formula = "=( AI" & er + 3 & "/ AI" & er + 2 & ")*100"</SPAN>
Range("AL" & er + 2).Formula = "=COUNTIF(AL" & sr & ":AL" & er & ","">0"")"</SPAN>
Range("AL" & er + 3).Formula = "=COUNTIF(AL" & sr & ":AL" & er & ","">104"")"</SPAN>
Range("AL" & er + 4).Formula = "=(AL" & er + 3 & "/AL" & er + 2 & ")*100"</SPAN>
End With</SPAN>
Next Area</SPAN>

' find the last row in column 1 = column A</SPAN></SPAN>
lr = Cells(Rows.Count, 1).End(xlUp).Row</SPAN>

' in the following ranges change the number format</SPAN></SPAN>
Range("Z2:Z" & lr).NumberFormat = "0.000"</SPAN>
Range("AB2:AB" & lr).NumberFormat = "0.000"</SPAN>
Range("AI2:AI" & lr).NumberFormat = "0.000"</SPAN>
Range("AL2:AL" & lr).NumberFormat = "0.000"</SPAN>

' turn back on screen updating</SPAN></SPAN>
Application.ScreenUpdating = True</SPAN>
End Sub</SPAN>
 
Upvote 0
Dinah,

I assume that you are using Excel 2007 or newer, based on your last attached workbook.


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


Code:
Option Explicit
Sub InsertBetweenV4()
' hiker95, 10/22/2012
' http://www.mrexcel.com/forum/excel-questions/659755-help-inserting-multiple-data-rows-same-data-after-change-value-occurs-2.html
Dim Area As Range
Dim r As Long, lr As Long, sr As Long, er As Long, i
Application.ScreenUpdating = False
i = Array("", "OBS", "VIOL", "VIOL RATE", "STATEMENT", "")
Worksheets(1).Activate
lr = Cells(Rows.Count, 1).End(xlUp).Row
For r = lr To 3 Step -1
  If Cells(r, 1) <> Cells(r - 1, 1) Then
    Rows(r).Resize(6).Insert
  End If
Next r
lr = Cells(Rows.Count, 1).End(xlUp).Row
For Each Area In Range("A2", Range("A" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeConstants).Areas
  With Area
    sr = .Row
    er = sr + .Rows.Count - 1
    Cells(er + 1, 1).Resize(6).Value = Application.Transpose(i)
    Cells(er + 1, 1).Resize(, 46).Interior.ColorIndex = 15
    Cells(er + 2, 1).Resize(4).Font.Bold = True
    Cells(er + 6, 1).Resize(, 46).Interior.ColorIndex = 15
    Range("Z" & er + 2).Formula = "=COUNTIF(Z" & sr & ":Z" & er & ","">0"")"
    Range("Z" & er + 3).Formula = "=SUM(COUNTIF(Z" & sr & ":Z" & er & ", ""<6""),COUNTIF(Z" & sr & ":Z" & er & ","">9""),-COUNTIF(Z" & sr & ":Z" & er & ",""=0""))"
    Range("Z" & er + 4).Formula = "=(Z" & er + 3 & "/Z" & er + 2 & ")*100"
    Range("AB" & er + 2).Formula = "=COUNTIF(AB" & sr & ":AB" & er & ","">0"")"
    Range("AB" & er + 3).Formula = "=COUNTIF(AB" & sr & ":AB" & er & ",""<4"")"
    Range("AB" & er + 4).Formula = "=(AB" & er + 3 & "/AB" & er + 2 & ")*100"
    Range("AI" & er + 2).Formula = "=COUNTIF(AI" & sr & ": AI" & er & ","">0"")"
    Range("AI" & er + 3).Formula = "=COUNTIF(AI" & sr & ": AI" & er & ",""<4"")"
    Range("AI" & er + 4).Formula = "=( AI" & er + 3 & "/ AI" & er + 2 & ")*100"
    Range("AL" & er + 2).Formula = "=COUNTIF(AL" & sr & ":AL" & er & ","">0"")"
    Range("AL" & er + 3).Formula = "=COUNTIF(AL" & sr & ":AL" & er & ","">104"")"
    Range("AL" & er + 4).Formula = "=IFERROR((AL" & er + 3 & "/AL" & er + 2 & ")*100,0)"
  End With
Next Area
lr = Cells(Rows.Count, 1).End(xlUp).Row
Range("Z2:Z" & lr).NumberFormat = "0.000"
Range("AB2:AB" & lr).NumberFormat = "0.000"
Range("AI2:AI" & lr).NumberFormat = "0.000"
Range("AL2:AL" & lr).NumberFormat = "0.000"
Application.ScreenUpdating = True
End Sub


Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm


Then run the InsertBetweenV4 macro.
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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