+ Reply to Thread
Results 1 to 7 of 7

Copy the row to another sheet when the value of a cell matches

  1. #1
    Registered User
    Join Date
    08-07-2019
    Location
    Chile
    MS-Off Ver
    365
    Posts
    8

    Copy the row to another sheet when the value of a cell matches

    Hi,

    I have a problem that I could not solve.

    I have two tables, where in column "A" of table 1 has values ​​that appear in column "A" of table 2 (these tables are on separate sheets).

    The data in column "A" in table 1 is never repeated.
    The data in column "A" in table 2 are the same as in column "A" in table 1 but they are repeated.

    I need a macro that looks for each of the cells in column A of table 1 in column A of table 2, and when they match, copy the row in table 1 and paste it over the row in table 2.

    The data in table 1 should only be copied once in table 2, on the first cell that matches

    I leave a sample file in case they need it, and this is the code that I have been occupying but it doesn't work for me.

    Please Login or Register  to view this content.
    Thank you!!!
    Attached Files Attached Files
    Last edited by Ghuphuneho; 08-28-2019 at 04:04 PM.

  2. #2
    Valued Forum Contributor
    Join Date
    07-14-2017
    Location
    Poland
    MS-Off Ver
    Office 2010
    Posts
    528

    Re: Copy the row to another sheet when the value of a cell matches

    Does my code give you the expected results?
    Working time approx. 90 seconds.
    Please Login or Register  to view this content.
    Best Regards,
    Maras.

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

    Cool Hola ! Try this !


    A starter demonstration lasting around a second :

    PHP Code: 
    Sub Demo1()
        
    Dim HVR&, WRw As RangeC%
            
    Application.Index(Hoja1.UsedRange.Rows(1).Value20)
            
    H
        
    For 1 To UBound(H)
            
    Application.Match(H(R) & "*"Hoja2.UsedRange.Rows(1), 0)
            
    H(R) = IIf(IsNumeric(W), WFalse)
            
    V(R) = IIf(IsNumeric(W), RFalse)
        
    Next
            H 
    Filter(HFalseFalse):  If UBound(H) < 0 Then Beep: Exit Sub
            H 
    Evaluate("{" Join(H",") & "}")
            
    Filter(VFalseFalse)
            
    Application.ScreenUpdating False
        With CreateObject
    ("Scripting.Dictionary")
            For 
    Each Rw In Hoja1.UsedRange.Rows("2:" Hoja1.UsedRange.Rows.Count)
                .
    Add Rw.Cells(1).Value2Application.Index(Rw.Value2, , V)
            
    Next
                V 
    Hoja2.UsedRange.Columns(1).Value2
            
    For 2 To UBound(V)
                If .
    Exists(V(R1)) Then
                    W 
    = .Item(V(R1))
                    For 
    1 To UBound(H):  Hoja2.Cells(RH(C)).Value2 W(C):  Next
                   
    .Remove V(R1)
                    If .
    Count 0 Then Exit For
                
    End If
            
    Next
               
    .RemoveAll
        End With
            Application
    .ScreenUpdating True
    End Sub 
    ► Do you like it ? ► ► So thanks to click on bottom left star icon « Add Reputation » !
    Last edited by Marc L; 08-28-2019 at 11:45 PM.

  4. #4
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Copy the row to another sheet when the value of a cell matches

    @MarcL
    Thanks a lot for your masterpieces ..
    As for this line
    Please Login or Register  to view this content.
    What if I would like to hard-code the v variable ..
    I tried this
    Please Login or Register  to view this content.
    But doesn't work ..
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

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

    Arrow

    As the V variable is an array like the one you have hardcoded as you can check in the Locals window

    and the result with Z = Application.Index(Rw.Value2, , V)

    As H is the destination columns and V is the source columns, when you hardcode V do not forget H, did you ?

  6. #6
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Copy the row to another sheet when the value of a cell matches

    Thanks a lot. I got it now
    Thank you very much

  7. #7
    Registered User
    Join Date
    08-07-2019
    Location
    Chile
    MS-Off Ver
    365
    Posts
    8

    Re: Copy the row to another sheet when the value of a cell matches

    Thank you very much everyone for the help!!!
    Everything works perfectly and I was exploding my head trying to achieve this.

    The problem is solved.

+ 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. If name on sheet 1 matches in sheet 2, copy data over
    By marcoandretti25 in forum Excel General
    Replies: 1
    Last Post: 10-30-2018, 11:57 AM
  2. Replies: 6
    Last Post: 05-29-2017, 04:16 PM
  3. [SOLVED] Copy Column A data to Sheet 2 where rows matches Sheet 2 headers
    By SCDE in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-19-2015, 12:14 AM
  4. [SOLVED] Copy datas in sheet if value matches
    By heiderayos in forum Excel Formulas & Functions
    Replies: 17
    Last Post: 09-15-2014, 08:55 PM
  5. [SOLVED] Copy row if value matches 'Sheet'!B1
    By ngocpdn in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-21-2013, 11:54 PM
  6. Replies: 4
    Last Post: 08-23-2012, 05:26 AM
  7. Replies: 1
    Last Post: 10-30-2009, 10:58 AM

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