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

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
I had posted the code earlier in this chat and was asking you if it worked, and you said NO.
 
Upvote 0
oh sorry I just realized my mistake!


thanks a lot for the code, what happen is I ran your code inside mine and I screwed up sorry again.
 
Last edited:
Upvote 0
This is the code, if you go to the first page of this thread, you would see that I had initially posted this code but maybe you skipped it
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
h sorry I just realized my mistake!


thanks a lot for the code, what happen is I ran your code inside mine and I screwed up sorry again.
 
Upvote 0
No problems at all, so long as it does what you want, thats fine :)
 
Upvote 0

Forum statistics

Threads
1,214,973
Messages
6,122,534
Members
449,088
Latest member
RandomExceller01

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