Consulting

Results 1 to 6 of 6

Thread: Slight modification needed - probably take most 3 minutes to figure out

  1. #1

    Slight modification needed - probably take most 3 minutes to figure out

    The code works except it compares column A values with column B values and prints out the values in column A and not column B

    [VBA]Sub compareColumns()
    Const TEST_COLUMN As String = "A" '<=== change to suit
    Dim i As Long
    Dim LastRow As Long
    Dim NextRow As Long

    With ActiveSheet

    LastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row
    For i = 1 To LastRow

    If IsError(Application.Match(.Cells(i, TEST_COLUMN).Value, .Columns(2), 0)) Then

    NextRow = NextRow + 1
    .Cells(i, TEST_COLUMN).Copy .Cells(NextRow, "C")
    End If
    Next i

    End With

    End Sub
    [/VBA]

    How do I change this such that the code prints values in both column A and B in column C? So, column C has the values that are in both A and B.

    thx

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Sub compareColumns()
    Const TEST_COLUMN As String = "A" '<=== change to suit
    Dim i As Long
    Dim LastRow As Long
    Dim NextRow As Long

    With ActiveSheet

    LastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row
    For i = 1 To LastRow

    If IsError(Application.Match(.Cells(i, TEST_COLUMN).Value, .Columns(2), 0)) Then

    NextRow = NextRow + 1
    .Cells(NextRow, "C") = .Cells(i, TEST_COLUMN).Value & _
    .Cells(i, TEST_COLUMN).Offset(0, 1).Value
    End If
    Next i

    End With

    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Thanks so much. Need some more help though.

    The result is that it adds the value of column A and column B together in column C.

    For example column A has value "hello" and B has "bye", then "hellobye" is printed in C.

    If cell A2 has value "hello" and B38 also has "hello", then "hello" should be printed in column C.

    Much appreciated!!

  4. #4
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    Then just add that test?

    [vba]

    Sub compareColumns()
    Const TEST_COLUMN As String = "A" '<=== change to suit
    Dim i As Long
    Dim LastRow As Long
    Dim NextRow As Long

    With ActiveSheet

    LastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row
    For i = 1 To LastRow

    If IsError(Application.Match(.Cells(i, TEST_COLUMN).Value, .Columns(2), 0)) Then

    NextRow = NextRow + 1
    If .Cells(i TEST_COLUMN).Value = .Cells(i, TEST_COLUMN).Offset(0,1).Value then
    .Cells(NextRow, "C") = .Cells(i, TEST_COLUMN).Value
    Else
    .Cells(NextRow, "C") = .Cells(i, TEST_COLUMN).Value & _
    .Cells(i, TEST_COLUMN).Offset(0, 1).Value
    End If
    End If
    Next i

    End With

    End Sub
    [/vba]

  5. #5
    Thanks, I'm lost!!

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Does lost mean sorted or not?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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