+ Reply to Thread
Results 1 to 22 of 22

Macro to copy data from master sheet if match is found

  1. #1
    Registered User
    Join Date
    01-12-2013
    Location
    london
    MS-Off Ver
    Excel 2007
    Posts
    14

    Macro to copy data from master sheet if match is found

    Hi..
    My problem is as below..
    Match the values in Column B of worksheet 2 with column B of worksheet 1. If match is found, select the matched cell of worksheet 1, copy the value from column W from corresponding row and paste it in Y column of worksheet 2. Below is format of data:

    Worksheet 1:
    Column B Column W
    50 Bulb
    51 Lamp
    52 Tubelight
    53 Bulb
    54 Lamp

    Worksheet 2:
    Column B Column Y
    2
    4
    50 Bulb
    60
    70

    I want to use index and match function. But I am not able to write formula for that.
    Kindly help me in this regard.
    Thanks for time spent in going through this post and for replying..

  2. #2
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Macro to copy data from master sheet if match is found

    Use

    Please Login or Register  to view this content.
    or

    Please Login or Register  to view this content.
    If you like my contribution click the star icon!

  3. #3
    Valued Forum Contributor
    Join Date
    02-05-2013
    Location
    Jakarta, Indonesia
    MS-Off Ver
    Excel 2013
    Posts
    571

    Re: Macro to copy data from master sheet if match is found

    or try this :
    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    01-12-2013
    Location
    london
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Macro to copy data from master sheet if match is found

    Thanks for your reply..but I want to write this in macro..I am new to VBA..
    I tried this

    Sht1Rng = Sheets(1).Range("B65536").End(xlUp).Row
    Sht2Rng = Sheets(2).Range("B65536").End(xlUp).Row
    For i = Sht2Rng To 1 Step -1
    Sheets(2).Cells(i, 25).Select
    ActiveCell.FormulaR1C1 = _
    "=IFERROR(INDEX(Sheet1!$W:$W,MATCH(Sheet2!B2,Sheet1!$B:$B,0)),"")"
    Next i

    but the above code is not working..what modifications I have to do?

  5. #5
    Registered User
    Join Date
    01-12-2013
    Location
    london
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Macro to copy data from master sheet if match is found

    Thanks for your reply..but I want to write this in macro..I am new to VBA..
    I tried this

    Sht1Rng = Sheets(1).Range("B65536").End(xlUp).Row
    Sht2Rng = Sheets(2).Range("B65536").End(xlUp).Row
    For i = Sht2Rng To 1 Step -1
    Sheets(2).Cells(i, 25).Select
    ActiveCell.FormulaR1C1 = _
    "=INDIRECT("Sheet1!R2C23" & MATCH(R2C2,Sheet1!R2C2:R50000C2,0))"
    Next i

    but the above code is not working..what modifications I have to do?

  6. #6
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Macro to copy data from master sheet if match is found

    This code works as long as you do not mind with the speed if you have large data

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    01-12-2013
    Location
    london
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Macro to copy data from master sheet if match is found

    Thanks for your reply, AB33..
    My data is very large hence I need a macro which has less execution time..
    I have tested below code..its working but the issue is of speed..due to 2 for loops its taking time to execute. I want to eliminate 1 for loop. Any help in this regard will be much appreciated.

    Sht1Rng = Sheets(1).Range("B65536").End(xlUp).Row
    Sht2Rng = Sheets(2).Range("B65536").End(xlUp).Row

    For i = Sht2Rng To 1 Step -1
    For J = Sht1Rng To 1 Step -1
    If Sheets(1).Cells(J, 2) = Sheets(2).Cells(i, 2) Then
    Sheets(2).Cells(i, 25) = Sheets(1).Cells(J, 23)
    End If
    Next J
    Next i

  8. #8
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Macro to copy data from master sheet if match is found

    I know my code is slow, but your code is much slower than mine

  9. #9
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Macro to copy data from master sheet if match is found

    If you do not have duplicates, this code is much faster than range looping

    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    01-12-2013
    Location
    london
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Macro to copy data from master sheet if match is found

    Thanks again AB33..
    I am new to vba..just want to know is it possible to use inbuilt functions such as index and match to above code? If yes, how should I modify the macro?

  11. #11
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Macro to copy data from master sheet if match is found

    index and match,no as far as I know. Dictionary script can deal with unique values ONLY. With Index and Match there are ways of returning duplicate values. Why do you need to use a code? I and M can simpley do your task.

  12. #12
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Macro to copy data from master sheet if match is found

    A bit strange this discussion. You asked for INDEX and MATCH solutions in post #1, which were provided to you in posts #2 and #3. Then in post #4 you state you want to use VBA and multiple solutions are provided to you. Now in post #10 you state that you are new to VBA and would prefer to use MATCH and INDEX?

  13. #13
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Macro to copy data from master sheet if match is found

    Oli,
    The OP is looking for ideal solution! IMO, it does not exist. The Find function can cope with duplicates, but very slow with large data and dictionary can not cope with duplicates. I would prefer to use a simple Excel I and M function, with out the complication of codes.

  14. #14
    Registered User
    Join Date
    01-12-2013
    Location
    london
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Macro to copy data from master sheet if match is found

    Hi..sorry for confusion..but I want to use index and match functions in vba code.

  15. #15
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Macro to copy data from master sheet if match is found

    I guess I am not very patient today.

    What are you exactly trying to achieve?
    a) assign the "indirect/match" formula to the cells using VBA, or
    b) the outcome/result of the formula as a value?

  16. #16
    Valued Forum Contributor
    Join Date
    02-05-2013
    Location
    Jakarta, Indonesia
    MS-Off Ver
    Excel 2013
    Posts
    571

    Re: Macro to copy data from master sheet if match is found

    From your posting on #4, i think this what you are looking for :

    Please Login or Register  to view this content.

  17. #17
    Valued Forum Contributor
    Join Date
    02-05-2013
    Location
    Jakarta, Indonesia
    MS-Off Ver
    Excel 2013
    Posts
    571

    Re: Macro to copy data from master sheet if match is found

    From your posting on #4, i think this what you are looking for :

    Please Login or Register  to view this content.

  18. #18
    Valued Forum Contributor
    Join Date
    02-05-2013
    Location
    Jakarta, Indonesia
    MS-Off Ver
    Excel 2013
    Posts
    571

    Re: Macro to copy data from master sheet if match is found

    Sorry, double post, because when "the forum was inaccessible" i'll try to post.
    Last edited by SDCh; 02-07-2013 at 12:03 PM.

  19. #19
    Registered User
    Join Date
    01-12-2013
    Location
    london
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Macro to copy data from master sheet if match is found

    Thank you all for your timely help..
    Below code with some modifications is working fine but somehow this code is also considering headers of the columns and matching the values. I want to start from row 2. I tried but could not modify. Can you please help me in this regard?

    Quote Originally Posted by AB33 View Post
    This code works as long as you do not mind with the speed if you have large data

    Please Login or Register  to view this content.

  20. #20
    Registered User
    Join Date
    01-12-2013
    Location
    london
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Macro to copy data from master sheet if match is found

    Below code is not extracting values from Column W of mastersheet..

    Quote Originally Posted by SDCh View Post
    From your posting on #4, i think this what you are looking for :

    Please Login or Register  to view this content.

  21. #21
    Registered User
    Join Date
    01-12-2013
    Location
    london
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Macro to copy data from master sheet if match is found

    Thanks a lot..made some changes..now its working like a charm..

    Quote Originally Posted by SDCh View Post
    From your posting on #4, i think this what you are looking for :

    Please Login or Register  to view this content.

  22. #22
    Valued Forum Contributor
    Join Date
    02-05-2013
    Location
    Jakarta, Indonesia
    MS-Off Ver
    Excel 2013
    Posts
    571

    Re: Macro to copy data from master sheet if match is found

    Glad to know your problem has been solved

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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