• Compare contents of cells

    Author
    Topic
    #462005

    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.

    Viewing 1 reply thread
    Author
    Replies
    • #1174296

      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

      • #1174332

        If the action you want to perform includes deleting rows, you should loop backwards to avoid problems:
        For r = 100 To 1 Step -1

        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?

        • #1174333

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

          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.

    • #1174302

      Thanks for the prompt response Hans

    Viewing 1 reply thread
    Reply To: Compare contents of cells

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: