Results 1 to 5 of 5
  1. #1
    Star Lounger
    Join Date
    Mar 2002
    Location
    San Jose, California, USA
    Posts
    82
    Thanks
    4
    Thanked 0 Times in 0 Posts
    What's the correct code to compare contents of cells? I have a sheet sorted by "worker name" that groups all the tasks for a certain worker into subsequent rows. What I need to do is:

    IF (column,row) = (column,row+1) then do something, else do something else.

    I can't find the right expression to compare the two cell contents.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Do you want to do this in VBA? If so, you can use code like this:
    Code:
    Dim r As Long ' row number
    Dim c As Long ' column number
    
    c = 4 ' work with column D
    
    For r = 1 To 100
      If Cells(r, c) = Cells(r + 1, c) Then
    	...
      Else
    	...
    End If
    If the action you want to perform includes deleting rows, you should loop backwards to avoid problems:

    For r = 100 To 1 Step -1

  3. #3
    Star Lounger
    Join Date
    Mar 2002
    Location
    San Jose, California, USA
    Posts
    82
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks for the prompt response Hans

  4. #4
    Bronze Lounger
    Join Date
    Sep 2007
    Posts
    1,203
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [quote name='HansV' post='789991' date='20-Aug-2009 18:10']If the action you want to perform includes deleting rows, you should loop backwards to avoid problems:
    For r = 100 To 1 Step -1[/quote]
    Oh yeah, like the ones that had me yanking my hair out at 01:00 this morning!!

    Other than deleting rows, are there any other reasons for looping backwards?

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    [quote name='VegasNath' post='790027' date='20-Aug-2009 21:54']Other than deleting rows, are there any other reasons for looping backwards?[/quote]
    The same holds for inserting rows. If you're looping downwards, deleting a row means there are fewer rows left than the loop expects, and inserting a row means that there are more rows to go than the loop expects. It would be possible to take that into account, but it's a lot easier to loop backwards.

Posting Permissions

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