+ Reply to Thread
Results 1 to 21 of 21

VBA to use VLOOKUP for finding specific data between 2sheets

  1. #1
    Registered User
    Join Date
    07-22-2019
    Location
    Japan
    MS-Off Ver
    2016
    Posts
    48

    VBA to use VLOOKUP for finding specific data between 2sheets

    Hi excel experts,

    Good day.

    I am working on a some macro but I couldn't make the code works.
    I hope you could have some time to analyze it.

    Background:
    1) I have one workbook with 3worksheets. 1 is main and the 2 others are imported everytime (datax and datay)
    2) I need to locate the matches (which was already solved) --> https://www.excelforum.com/excel-pro...ml#post5187039
    3) Upon matching I neede to look for the remarks of the matched data and copy it from datax to datay

    Here's the code that I looked upto but I can't make it work:
    Please Login or Register  to view this content.
    I hope to hear from you guys.

    Thank you so much.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA to use VLOOKUP for finding specific data between 2sheets

    Jpngineerへようこそ!

    ワークブックを添付してください。

    I lived in Japan 38 years ago. So, I apologize in advance if my Japanese is not very good.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    07-22-2019
    Location
    Japan
    MS-Off Ver
    2016
    Posts
    48

    Re: VBA to use VLOOKUP for finding specific data between 2sheets

    Good day Leith Ross,

    Thanks for responding.

    Here's the sample of what I am doing.

    Thanks in advance.
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Question

    Hi !

    Can you elaborate 2) & 3) ? …

  5. #5
    Registered User
    Join Date
    07-22-2019
    Location
    Japan
    MS-Off Ver
    2016
    Posts
    48

    Re: VBA to use VLOOKUP for finding specific data between 2sheets

    Hi Marc L,

    2) two columns from datax needs to be compared to datay.And will be automatically filtered after highlighting the matches.
    (It was solved from the link provided)
    3) since the matched data on datax has an another column (the "Remarks" column) which is not present on datay,thus I wanted to copy that "Remarks" of the matched data from datax to datay. That's when the vlookup was raised into consideration.
    Kindly see the attached excel file for sample.

    Thank you so much for your response.
    Last edited by Jpngineer; 09-03-2019 at 03:13 AM. Reason: wrong spelling

  6. #6
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    2) Which columns ?! As filtering is maybe not the way …

    3) Copy from Remarks to where in datay ? Should we guess ? No !
    Maybe if only your Excel sample has an expected result worksheet …

  7. #7
    Registered User
    Join Date
    07-22-2019
    Location
    Japan
    MS-Off Ver
    2016
    Posts
    48

    Re: VBA to use VLOOKUP for finding specific data between 2sheets

    Hi Marc L,

    2) For the background (https://www.excelforum.com/excel-pro...matches-3.html)
    a. compare Columns ("DRW No." & "Ref") of datax, to columns of datay ("DWG. NO", "SYM.")
    b. then highlight
    c. filter the results
    -It works perfectly as I wanted.. (column positions of datax and datay are not fixed all the time so the columns are called by their column header names)

    3) Is an addition to what i needed..
    "Remarks" column is also a header name from datax (with columns position is also not fixed) and will be copied to the first empty columns after the columns with data on datay

    Sorry sir if my explanations seems to be unclear.

    As always, thank you for your response.

  8. #8
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    Now it's clear enough but for your actual need filtering is just a waste of time as if I'm not wrong
    the logic needs if in datax Remarks cells not blank must be copied to datay if datax DRW No. or Ref. match with datay …

  9. #9
    Registered User
    Join Date
    07-22-2019
    Location
    Japan
    MS-Off Ver
    2016
    Posts
    48

    Re: VBA to use VLOOKUP for finding specific data between 2sheets

    Hi Marc L,

    Yes that's it.

    Thank you so much.

  10. #10
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool

    According to your attachment having few remarks and without any duplicate in sheets matching columns
    starting from a not filtered datay sheet a beginner starter demonstration :

    PHP Code: 
    Sub Demo1()
            
    Dim HRg As RangeVC%, Rc As RangeRf As Range
                H 
    = [{"DRW No.","Ref.","Remarks";"DWG. NO","SYM.",0}]
                
    Set Rg Sheet2.UsedRange.Rows
                V 
    Application.Match(H(13), Rg(1), 0)
                If 
    IsNumeric(VThen H(13) = Else Beep: Exit Sub
        With Sheet3
    .UsedRange
            
    For 1 To 2
                V 
    Application.Match(H(1C), Rg(1), 0)
                If 
    IsNumeric(VThen H(1C) = H(13) + Else Beep: Exit Sub
                V 
    Application.Match(H(2C), .Rows(1), 0)
                If 
    IsNumeric(VThen H(2C) = Else Beep: Exit Sub
            Next
                 H
    (23) = .Columns(.Columns.Count).Column 1
                 Application
    .ScreenUpdating False
            
    For Each Rc In Rg("3:" Rg.Count).Columns(H(13)).SpecialCells(xlCellTypeConstants)
                 
    Set Rf = .Columns(H(21)).Find(Rc(1H(11)).Text, , xlValuesxlWhole)
                  If 
    Rf Is Nothing Then Set Rf = .Columns(H(22)).Find(Rc(1H(12)).Text)
              If 
    Not Rf Is Nothing Then Rc.Copy .Parent.Cells(Rf.RowH(23))
            
    Next
        End With
                 Set Rf 
    Nothing:   Set Rg Nothing
                 Application
    .ScreenUpdating True
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !

  11. #11
    Registered User
    Join Date
    07-22-2019
    Location
    Japan
    MS-Off Ver
    2016
    Posts
    48

    Re: VBA to use VLOOKUP for finding specific data between 2sheets

    Hi Marc L,

    Good evening (here).

    Thanks for your help. It works well with the sample. I'll try it again tomorrow.
    And inform you with the results.

    Thank you so much.

  12. #12
    Registered User
    Join Date
    07-22-2019
    Location
    Japan
    MS-Off Ver
    2016
    Posts
    48

    Re: VBA to use VLOOKUP for finding specific data between 2sheets

    Hi Marc L,

    I would like to know if in the code column positions of headers are indicated.
    (Sorry I don't understand PHP syntax.)
    Coz in the sample sheet column header starts at merged rows 2,3.
    How can I adjust the code to fit my needs? what are the adjustable and fixed variables in the code?

    Thanks.

  13. #13
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    Thanks for the rep' added !

    A smart worksheet does not need any merged cells …

    No fixed column position 'cause you wrote
    « column positions of datax and datay are not fixed all the time so the columns are called by their column header names »
    like you can see within my VBA demonstration !
    This VBA code works with first row used in each worksheet for headers so normally a mod is not necessary
    or it's weird to not attach a matching layout sample according to the real workbook !
    See the words Columns & Rows in the code …

  14. #14
    Registered User
    Join Date
    07-22-2019
    Location
    Japan
    MS-Off Ver
    2016
    Posts
    48

    Re: VBA to use VLOOKUP for finding specific data between 2sheets

    Yeah that's right.. no fixed columns/rows
    The code perfectly works in the sample data.

    However, I am a little bit confused why when copied to the actual workbook its not working wherein the only difference are the headers position.
    It started on row 3,4 instead of row2,3
    Could you explain even part of it
    for example: H(2,3) means?
    I really wanted to understand the code..
    Thank you.

  15. #15
    Registered User
    Join Date
    07-22-2019
    Location
    Japan
    MS-Off Ver
    2016
    Posts
    48

    Re: VBA to use VLOOKUP for finding specific data between 2sheets

    Hi Marc L,

    Sorry if I have so many follow up questions..

    I also changed the sheets number depending on where the datax and datay is.
    aside from Sheet2 and Sheet3.. what other codes indicate sheet number?

    Thanks

  16. #16
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    No row issue with a smart worksheet but in your case …
    Posting a different layout workbook sample than the real one means
    you are very confident with your VBA skills in order to amend the code ‼

    As I yet wrote, the code does not start from the absolute rows position #2 & 3 but on the first used row
    as you can read within the code, just see .UsedRange.Rows …

    You can add a Find codeline in order to get the header row # …

    For each statement once the text cursor on it you can hit F1 key then read its VBA relative help.
    Except for Match which is the MATCH worksheet function so the help is on Excel side …

    Sheet2 is not the sheet # but the CodeName of a sheet, can be replaced by any valid worksheet reference …
    See \1

    Could be easier with a named range or if you convert the range as a true Excel table :
    no need anymore to find out each header column position like in my demonstration with the H variable array
    - to understand see in step by step mode hitting F8 key the variables contents in the VBE Locals window -
    so the code should directly start at the For Each codeline and almost all previous codelines would be useless,
    no row issue whatever the first row (headers) position #, no …

  17. #17
    Registered User
    Join Date
    07-22-2019
    Location
    Japan
    MS-Off Ver
    2016
    Posts
    48

    Re: VBA to use VLOOKUP for finding specific data between 2sheets

    This has been solved thru this link
    https://www.excelforum.com/excel-pro...ml#post5188143

    Thanks to all those who sincerely helped.
    Such a blessing to everyone.

  18. #18
    Registered User
    Join Date
    07-22-2019
    Location
    Japan
    MS-Off Ver
    2016
    Posts
    48

    Re: VBA to use VLOOKUP for finding specific data between 2sheets

    Hi Marc L,

    I just read your reply.. sorry

    I am not confident with my VBA skills.
    Data are too confidential to be shared in a platform like this.
    I hope you understand that.
    I can guarantee that the workbooks are closely similar to each other except for the contents and header starting position.

    Thank you so much for your help.
    I really appreciate your effort for answering and catering all my queries.

    I just wanted to make it clear that I am not just here to copy and paste.
    I wanted to learn from experts like you so that I too would be able to create something on my own in the future so as not to disturb experts like you more often.

    Thanks for your help.
    (by the way this --> "!!" offends me)

  19. #19
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    Thanks for this revert !

    '‼" was not to offend, as it is very not difficult to attach a sample workbook respecting the real workbook layout …

  20. #20
    Registered User
    Join Date
    07-22-2019
    Location
    Japan
    MS-Off Ver
    2016
    Posts
    48

    Re: VBA to use VLOOKUP for finding specific data between 2sheets

    Marc L,

    Sorry for the misunderstanding.
    I'll remember your advise.

    I found the difference why it is working in the sample and not in real workbook.
    The first two rows in the datax(in violet font).

    If you're still interested in helping me solve this using your code.
    Coz I still do.

    Thanks
    Attached Files Attached Files

  21. #21
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb

    As the headers row # is not anymore the first used range row but the third so just replace both Rg(1) to Rg(3)

    In the For Each Rc in Rg("3 replace by 5
    Last edited by Marc L; 09-11-2019 at 06:14 AM.

+ 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. [SOLVED] Finding specific data
    By yiyi2 in forum Excel Formulas & Functions
    Replies: 13
    Last Post: 06-11-2018, 09:31 PM
  2. How to Copy 2sheets Matched Rows only to New Sheet
    By maddyrafi1987 in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 03-01-2018, 08:53 AM
  3. vlookup - based on finding specific text within a cell
    By gromitnz in forum Excel Formulas & Functions
    Replies: 60
    Last Post: 05-16-2016, 06:14 AM
  4. [SOLVED] Finding a specific set of data, and then the min value within this set
    By timothy040888 in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 04-16-2015, 06:20 AM
  5. [SOLVED] Finding specific data in cell comments and finding the line# in comments it appears on
    By tv69 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-11-2014, 04:26 PM
  6. [SOLVED] Formula for VLOOKUP when finding closest value above & below a specific value??
    By bryanr72 in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 10-06-2014, 03:17 PM
  7. finding specific data
    By steveori in forum Excel General
    Replies: 2
    Last Post: 08-12-2008, 05:40 AM

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