VBA insert row when value Changes

kourbeh

New Member
Joined
Nov 5, 2013
Messages
26
Hi guys
I hope someone can help me with this one!
I need to check the value of the Cell in B column, once it is changed I need to insert a new row, copy the contains of the first row to the new row, my code is not working properly :(
this is the sheet:
https://app.box.com/s/bntxmsw7wzbtonxrab57

Code:
Sub Insertrow()    SheetName = "Sheet1"
    RngStr = "B2:B" + CStr(Worksheets(SheetName).UsedRange.Rows.Count)
    Set Rng = Worksheets(SheetName).Range(RngStr)
    
    For Each Cell In Rng
        If Application.CountIf(Rng, Cell.Value) = 1 Then
            Cell.EntireRow.Insert
            Rows("1:1").Select
             Selection.Copy
             Range(Rng).Select
             ActiveSheet.Paste
                      
    
        End If
        Next Cell


End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Is what you posted in the screenshot the final output or what?
 
Upvote 0
No it is not, it is the input, the macro I wrote isnot working, I don't know what is the reason :(
 
Upvote 0
You want to copy the header row to the newly inserted rows?? right? If thats what you want to do, then
Code:
Sub runmacro()
      For i = Range("A" & Rows.Count).End(xlUp).Row - 1 To 2 Step -1
         If Range("B" & i).Value <> Range("B" & i + 1).Value Then
            Rows(i + 1).Select
            Selection.Resize(1).Insert
            Rows(1).EntireRow.Copy Range("A" & i + 1)
          End If
    Next i
End Sub
 
Upvote 0
So what does the macro do or not do? cos it works as i think it should
 
Upvote 0
it is suppose to insert a row when he finds that the value of B2:B is changed!
but it is not working
 
Upvote 0
insert a new row, copy the contains of the first row to the new row, my code is not working properly :(
This is the output from my code, when i ran it on your data


Excel 2010
ABCDEFGH
1QueryDateCity NameAttSuccBlkrbchsbTdom
223/06/2013Montreal1282400.00110.39
324/06/2013Montreal4385700.00160.72
425/06/2013Montreal7374710.03210.17
526/06/2013Montreal10374600.00170.45
627/06/2013Montreal13377000.00150.40
728/06/2013Montreal16388600.00160.41
8QueryDateCity NameAttSuccBlkrbchsbTdom
923/06/2013Toronto227400.0020.41
1024/06/2013Toronto564800.0020.31
1125/06/2013Toronto864300.0020.34
1226/06/2013Toronto1153800.0061.10
1327/06/2013Toronto1461900.0091.43
1428/06/2013Toronto1757900.0040.69
15QueryDateCity NameAttSuccBlkrbchsbTdom
1623/06/2013Vancouver3178000.0030.56
1724/06/2013Vancouver6208200.0070.31
1825/06/2013Vancouver9162600.0030.18
1926/06/2013Vancouver12173500.0080.46
2027/06/2013Vancouver15167500.0040.24
2128/06/2013Vancouver18183100.0070.38
Sheet1


The inserted rows are thee ones shown in Bold, the data from row 1 is copied in there. Isnt that what you want?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,693
Members
448,979
Latest member
DET4492

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