2

I want to compare 2 to 1 Columns in Excel using VBA..

I already achieve 2 to 2 Columns using this code

Sub LIST1_LIST2()
    Dim list1 As Range
    Dim LIST2 As Range
    Set list1 = Range("B3:C181")
    Set LIST2 = Range("G3:H729")

    For Each row1 In list1.Rows
        For Each row2 In LIST2.Rows
          If (row1.Cells(1) = row2.Cells(1) And row1.Cells(2) = row2.Cells(2)) Then
              row1.Cells(1).Offset(0, 2) = row1.Cells(1)
              row1.Cells(2).Offset(0, 2) = row1.Cells(2)
              Exit For
          End If
        Next row2

    Next row1

End Sub

But now I need something VBA scripts that works somehow like the image below enter image description here

2
  • Why don't you just put into the output column Name the formula =LastName&", "&FirstName and in the age column you can use =Age (from the column further to the left). Commented Mar 29, 2017 at 9:39
  • @Ralph let us say that I'm not the one who inputs this data, I'm just here to compare this data. Commented Mar 29, 2017 at 9:44

2 Answers 2

1

Work with a a simple Do Until Loop:

Option Explicit
Public Sub Example()
    Dim B As Range, _
        C As Range, _
        D As Range, _
        F As Range, _
        G As Range

    Dim i%, x% ' Dim as long

    Set B = Columns(2)
    Set C = Columns(3)
    Set D = Columns(4)
    Set F = Columns(6)
    Set G = Columns(7)

    i = 2
    x = 2

    Do Until IsEmpty(B.Cells(i))

        Debug.Print B.Cells(i).Value & ", " & _
                    C.Cells(i).Value ' Print on Immed Win

        Do Until IsEmpty(F.Cells(x))
            DoEvents ' For testing

            If F.Cells(x).Value = B.Cells(i).Value & ", " & _
                                  C.Cells(i).Value Then

               Debug.Print F.Cells(i).Value = B.Cells(i).Value & ", " & _
                           C.Cells(i).Value ' Print on Immed Win

                G.Cells(x) = D.Cells(i)
                x = 2 ' Reset Loop
                Exit Do
            End If

            x = x + 1
        Loop

        i = i + 1
    Loop
End Sub

Other info

DoEvents is most useful for simple things like allowing a user to cancel a process after it has started, for example a search for a file. For long-running processes, yielding the processor is better accomplished by using a Timer or delegating the task to an ActiveX EXE component.. In the latter case, the task can continue completely independent of your application, and the operating system takes case of multitasking and time slicing.



Debug.Print Immediate Window is used to debug and evaluate expressions, execute statements, print variable values, and so forth. It allows you to enter expressions to be evaluated or executed by the development language during debugging. To display the Immediate window, open a project for editing, then choose Windows from the Debug menu and select Immediate, or press CTRL+ALT+I.

Sign up to request clarification or add additional context in comments.

1 Comment

this code isn't what I'm looking for but the Idea is here, so yeah I got it. thanks by the way..I consider this the answer :) Thanks a lot
0

You can use this pseudocode to guide you:

If LASTNAME = Left(NAME,LASTNAME.Length) And _
FIRSTNAME = Right(NAME,FIRSTNAME.Length) Then

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.