We help IT Professionals succeed at work.

Insert rows in Excel when cells contents change

RobertBreg
RobertBreg asked
on
372 Views
Last Modified: 2010-04-17
Does anyone know how to  add rows in excel VBA when the value in a certain cell changes.

I.e..

Column A
Robert
Robert
Robert
Robert
TIM
TIM
TIM
TIM


I want excel to insert a row between the last Robert and the first Tim.  Any help would be much appreciated!
Comment
Watch Question

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Sorry about the delay on closing the question but thanks.  GREAT JOB!

Author

Commented:
The code is working great.  however, i would like to have it create 3 empty rows between the changing of the cell contents.  I have modified the code which is below:

Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown

this adds two blank rows but when i add a third
Selection.Insert Shift:=xlDown

it only does it one time and then drops out of the loop.  it never checks the rest of the spread sheet.  can someone help me make this modification?

Thanks

Author

Commented:
When i put the third Selection.Insert Shift:=xlDown,
it adds 6 empty rows instead of just 3.  When i use 2 Selection.Insert Shift:=xlDown, it only adds 2 rows.  Why?

Commented:
Robert,

please post the entire code on the site.  I don't have it anymore... i'll try to help.

Commented:
oh geez.  DUH.  it's already UP there................. HAHA!

Never mind.  I'll take a look and post an answer.

Commented:
the code to create THREE empty rows...  

Notice the loop will terminate whenever it finds a row with no text in it.  That's why the iterator = iterator + 3 is at the end... to put it on the next row with text after the rows are added.  If you added FOUR rows... it would be iterator = iterator + 4.  Get it?

Sub insert_row()
'
    Dim value As String
    Dim iterator As Integer
    Dim number_of_rows_to_test As Integer
   
    iterator = 1
    number_of_rows_to_test = 500
   
    value = ActiveSheet.Cells(1, 1).value
   
    While (Not (value = ""))
   
        If (Not (value = ActiveSheet.Cells(iterator, 1).value)) Then
        value = ActiveSheet.Cells(iterator, 1).value
        ActiveSheet.Rows(iterator).Select
        Selection.Insert Shift:=xlDown
        Selection.Insert Shift:=xlDown
        Selection.Insert Shift:=xlDown
        iterator = iterator + 3
        End If
        iterator = iterator + 1
    Wend
   
End Sub

Author

Commented:
Thank you very much

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.