+ Reply to Thread
Results 1 to 11 of 11

match values from two columns

  1. #1
    Forum Contributor
    Join Date
    02-23-2015
    Location
    Sweden
    MS-Off Ver
    2013
    Posts
    109

    match values from two columns

    Hello people,


    I been struggling with a function in couple of days now
    I hope someone can help me.


    I need a code that loops through sheet "mysheet1" on each row and checks the values in Columns "A" and "B"
    Example:


    (mySheet1)


    Columns:[ A ][ B ]
    ROW1: SWE ARA
    ROW2: SWE DAN



    in this case take value from Column A Row 1 = SWE and Column B Row 1 = ARA
    (keep them in memory)


    then go to sheet "mysheet2"


    check any columns as long as the values in this case "SWE" and "ARA" are placed next to each other.



    Could someone help me

  2. #2
    Registered User
    Join Date
    08-05-2015
    Location
    Finland
    MS-Off Ver
    Home and Business 2013
    Posts
    50

    Re: match values from two columns

    sub looooooop()
    dim x as long, k as long
    dim y as string

    x=1,
    '(first cell is A1)

    do until 'write when to stop
    mysheet1.activate
    y=cells(x,1).value
    z=cells(x,2).value

    mysheet2.activate
    k = 1
    do until cells(k,1).value=y and cells(k,2).value = z
    k=k+1
    loop

    'Your code here


    loop
    end sub


    try this

    this will work if values in mysheet2 are in columns A and B
    Do you have many columns?
    Last edited by The_Greg; 08-05-2015 at 04:03 AM.

  3. #3
    Forum Contributor
    Join Date
    02-23-2015
    Location
    Sweden
    MS-Off Ver
    2013
    Posts
    109

    Re: match values from two columns

    I have many columns also the code doesn't work

    Compile error Loop without do,

    also "x=1," gives error

    Thank you in advance

  4. #4
    Forum Contributor
    Join Date
    02-23-2015
    Location
    Sweden
    MS-Off Ver
    2013
    Posts
    109

    Re: match values from two columns

    I get loads errors have you even run the code you wrote?.

  5. #5
    Registered User
    Join Date
    08-05-2015
    Location
    Finland
    MS-Off Ver
    Home and Business 2013
    Posts
    50

    Re: match values from two columns

    nope, I will try something else=)

  6. #6
    Registered User
    Join Date
    08-05-2015
    Location
    Finland
    MS-Off Ver
    Home and Business 2013
    Posts
    50

    Re: match values from two columns

    Does it have to be like SWE is in left column and ARA in right or can it also be both ways?

  7. #7
    Forum Contributor
    Join Date
    02-23-2015
    Location
    Sweden
    MS-Off Ver
    2013
    Posts
    109

    Re: match values from two columns

    it can be any value I just gave a example.

  8. #8
    Registered User
    Join Date
    08-05-2015
    Location
    Finland
    MS-Off Ver
    Home and Business 2013
    Posts
    50

    Re: match values from two columns

    Sub FIND_PAIRS()
    'Application.ScreenUpdating = False
    Dim x As Long, y As Long, c As Long, r As Long, k As Long
    Dim Lword As String, Rword As String

    x = 1
    Do Until IsEmpty(Sheets("mysheet1").Cells(x, 1))
    Sheets("mysheet1").Activate
    Lword = Cells(x, 1).Value
    Rword = Cells(x, 2).Value

    Sheets("mysheet2").Activate
    c = 1
    k = 1

    Do Until IsEmpty(Cells(k, c)) And Cells(k, c).Row = 1
    If c = 1 Then
    If Cells(k, c).Value = Lword And Cells(k, c + 1).Value = Rword Then

    'YOUR CODE HERE
    MsgBox Lword & " and " & Rword & " are on " & k & " row," & vbNewLine & _
    " columns are: 1 and " & c + 1
    'YOUR CODE HERE

    k = k + 1
    ElseIf IsEmpty(Cells(k, c)) Then
    c = c + 1
    k = 1
    Else
    k = k + 1
    End If
    ElseIf c > 1 Then
    If Cells(k, c).Value = Lword And Cells(k, c + 1).Value = Rword Or _
    Cells(k, c).Value = Lword And Cells(k, c - 1).Value = Rword Then

    'YOUR CODE HERE
    MsgBox Lword & " and " & Rword & " are on " & k & " row," & vbNewLine & _
    "one of them is in column: " & c
    'YOUR CODE HERE

    k = k + 1
    ElseIf IsEmpty(Cells(k, c)) Then
    c = c + 1
    k = 1
    Else
    k = k + 1
    End If
    End If
    Loop
    x = x + 1
    Loop
    Application.ScreenUpdating = True
    End Sub

  9. #9
    Forum Contributor
    Join Date
    02-23-2015
    Location
    Sweden
    MS-Off Ver
    2013
    Posts
    109

    Re: match values from two columns

    Quote Originally Posted by The_Greg View Post
    Sub FIND_PAIRS()
    'Application.ScreenUpdating = False
    Dim x As Long, y As Long, c As Long, r As Long, k As Long
    Dim Lword As String, Rword As String

    x = 1
    Do Until IsEmpty(Sheets("mysheet1").Cells(x, 1))
    Sheets("mysheet1").Activate
    Lword = Cells(x, 1).Value
    Rword = Cells(x, 2).Value

    Sheets("mysheet2").Activate
    c = 1
    k = 1

    Do Until IsEmpty(Cells(k, c)) And Cells(k, c).Row = 1
    If c = 1 Then
    If Cells(k, c).Value = Lword And Cells(k, c + 1).Value = Rword Then

    'YOUR CODE HERE
    MsgBox Lword & " and " & Rword & " are on " & k & " row," & vbNewLine & _
    " columns are: 1 and " & c + 1
    'YOUR CODE HERE

    k = k + 1
    ElseIf IsEmpty(Cells(k, c)) Then
    c = c + 1
    k = 1
    Else
    k = k + 1
    End If
    ElseIf c > 1 Then
    If Cells(k, c).Value = Lword And Cells(k, c + 1).Value = Rword Or _
    Cells(k, c).Value = Lword And Cells(k, c - 1).Value = Rword Then

    'YOUR CODE HERE
    MsgBox Lword & " and " & Rword & " are on " & k & " row," & vbNewLine & _
    "one of them is in column: " & c
    'YOUR CODE HERE

    k = k + 1
    ElseIf IsEmpty(Cells(k, c)) Then
    c = c + 1
    k = 1
    Else
    k = k + 1
    End If
    End If
    Loop
    x = x + 1
    Loop
    Application.ScreenUpdating = True
    End Sub
    You are a ******* genius! thank you so much sir !

  10. #10
    Registered User
    Join Date
    08-05-2015
    Location
    Finland
    MS-Off Ver
    Home and Business 2013
    Posts
    50
    Quote Originally Posted by elmnas View Post
    You are a ******* genius! thank you so much sir !
    Glad to help.

  11. #11
    Forum Contributor
    Join Date
    02-23-2015
    Location
    Sweden
    MS-Off Ver
    2013
    Posts
    109

    Re: match values from two columns

    Quote Originally Posted by The_Greg View Post
    Glad to help.
    Hello again,

    You have wrote on some places in the code 'YOUR CODE HERE
    could you explain what happens on each place?

    Thank you in advance.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Match values from two columns having similar values in 1st column if not error
    By csunilkumar in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-23-2014, 08:32 AM
  2. [SOLVED] Match values from two different columns and sort according to the matched values
    By kongfookann in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 12-20-2013, 09:44 AM
  3. [SOLVED] Distribute values in columns based on match between two other columns
    By hydrgal in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 03-15-2013, 04:07 AM
  4. [SOLVED] Match values in two columns
    By ispyhope in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 10-06-2012, 12:00 AM
  5. Match Columns in 2 Worksheets, return only changed values of other columns
    By cheynooki in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-17-2011, 01:19 PM
  6. Replies: 5
    Last Post: 10-11-2008, 04:01 PM
  7. Match values and sum columns
    By cgeistman in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-23-2008, 06:45 PM

Tags for this Thread

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