+ Reply to Thread
Results 1 to 16 of 16

Cell not equal to previous cell?

Hybrid View

  1. #1
    Registered User
    Join Date
    09-29-2011
    Location
    Georgia
    MS-Off Ver
    Excel 2007
    Posts
    29

    Question Cell not equal to previous cell?

    Sub SalesOrderChange()
        Range("B2").Select
        Do Until IsEmpty(ActiveCell)
            Dim rng As Range
            Set rng = ActiveCell
            If ActiveCell <> ActiveCell.Offset(-1,0)
    My current and INCOMPLETE code is posted. I am new to VBA and what I am doing should be simple but I am apparently not using the correct syntax. I want to go through a list and every time the number in the cell I am currently in (my active cell) is not equal to the number in the same column in the row before it I want to add 2 rows afterwards. Otherwise I want to check the next row down and see if it is equal to the row before it.

    In non vba is should look something like this:

    Select Cell B2

    Until empty
    if Active Cell DOES NOT EQUAL Previous Cell (up one row, no column offset)
    insert 2 rows
    then select the cell 2 rows down, no column offset
    else select the next Cell (down one row, no column offset)
    end the loop

    Let me know if this doesn't make sense
    Last edited by sweedey; 09-29-2011 at 08:25 PM. Reason: Added code tags

  2. #2
    Forum Expert ConneXionLost's Avatar
    Join Date
    03-11-2009
    Location
    Victoria, Canada
    MS-Off Ver
    2010
    Posts
    2,952

    Re: Active Cell Not Equal to Previous Cell

    Your post did not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here

    I've added the tags for you in this post; please use them in the future.
    Would you like to say thanks? Please click the: " Add Reputation" button, on the grey bar below the post.

  3. #3
    Registered User
    Join Date
    09-29-2011
    Location
    Georgia
    MS-Off Ver
    Excel 2007
    Posts
    29

    Re: HELP: how to say cell not equal to previous cell?

    Sorry about that, I saw that we needed to do that, but couldn't find how because I was in a hurry! Thanks for the assistance!

  4. #4
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: HELP: how to say cell not equal to previous cell?

    Maybe try this:

    Sub test()
    
    Dim x As Long
    Dim LastRw As Long
    
    LastRw = Range("B" & Rows.Count).End(xlUp).Row
    
    For x = LastRw To 2 Step -1
        If Cells(x, 2) <> Cells(x - 1, 2) Then Rows(x).Resize(2).Insert
    Next x
    
    
    End Sub
    abousetta
    Last edited by abousetta; 09-29-2011 at 08:21 PM.
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  5. #5
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: HELP: how to say cell not equal to previous cell?

    ConneXionLost has your avatar's teeth gotten sharper or is my screen resolution all of a sudden much better

  6. #6
    Registered User
    Join Date
    09-29-2011
    Location
    Georgia
    MS-Off Ver
    Excel 2007
    Posts
    29

    Red face Re: Cell not equal to previous cell?

    Thanks! That is SOO close to what I want it to do, unfortunately I do not exactly understand it or I would fiddle with it to get it to work. I want the insertion of rows to happen between the different numbers rather than the same numbers

    Example:
    
    1
    4
    2
    2
    5
    3
    3
    1
    
    would become
    
    1
    
    
    4
    
    
    2
    2
    
    
    5
    
    
    3
    3
    
    
    1
    Attached Files Attached Files

  7. #7
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Cell not equal to previous cell?

    My bad... I misread the intention. Just change the equal sign to not equal (e.g. <>). I have done this in the code that I posted earlier. It seems to work on my sample.

    abousetta

  8. #8
    Forum Expert ConneXionLost's Avatar
    Join Date
    03-11-2009
    Location
    Victoria, Canada
    MS-Off Ver
    2010
    Posts
    2,952

    Re: HELP: how to say cell not equal to previous cell?

    Quote Originally Posted by abousetta View Post
    ConneXionLost has your avatar's teeth gotten sharper or is my screen resolution all of a sudden much better
    Only if you`ve bought a new monitor. And no, I`m not going to quote the Big Bad Wolf here.

  9. #9
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Cell not equal to previous cell?

    Same monitor(s) but I did switch to Child 3.7 which seems to have lower graphics, but allows me to use color in the code. And then all of a sudden I could have sworn that your avatar was looking at me funny ...

    I quess enough forums for today and more real work.

    abousetta

  10. #10
    Registered User
    Join Date
    09-29-2011
    Location
    Georgia
    MS-Off Ver
    Excel 2007
    Posts
    29

    Re: Cell not equal to previous cell?

    thank you SO much! You are a life saver!

  11. #11
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Cell not equal to previous cell?

    Just spreading the love ... My @ss has been on the line more than once and others have chipped in to save it...

    Good luck.

    abousetta

    P.S. If you are satisfied with the answers, please mark the thread as solved. Also reputation points (scales) are always welcomed. In fact I have over 800 posts... feel free to add rep points to any of them
    Last edited by abousetta; 09-29-2011 at 08:29 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1