+ Reply to Thread
Results 1 to 18 of 18

Matching Values and Copying to A new sheet.

Hybrid View

  1. #1
    Registered User
    Join Date
    03-24-2008
    Posts
    17
    Correct on both, conformation Number = Order and Line Number = Line Note.

    Hopefully I will anwser the 3rd question well enough to help out.
    You can take the details from sheet 2 but the reason for sheet one is to match the part number with the detail column on sheet 2. I assumed you would have to match the two together to get the part and detail to match on sheet three, unless I am way overthinking it here.

  2. #2
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862
    OK - let's make our terms consistent; you have:
    Sheet1 - Order, Line Note & Part Number
    Sheet2 - Order, Line Note & Detail.

    You want to find all records where Order & Line Note are the same across Sheets 1 & 2 and you want Order, Line Note & Part Number (which can all be taken from Sheet1) copied to Sheet3 ... Is this right? Or do you want the Detail from Sheet2 as well?
    MatrixMan.
    --------------------------------------
    If this - or any - reply helps you, remember to say thanks by clicking on *Add Reputation.
    If your issue is now resolved, remember to mark as solved - click Thread Tools at top right of thread.

  3. #3
    Registered User
    Join Date
    03-24-2008
    Posts
    17
    Correct on terms for sheets 1 and 2, and I appoligize for making this confusing.

    The actual line not doesn't need to be copied over the the 3rd sheet. The only things needed for sheet three is Order, Part Number, and Detail.

  4. #4
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862
    OK - here you go ...

    Option Explicit
    Option Base 1
    
    Sub MatchAndPaste()
    Dim strPartsSheet As String, strDetailSheet As String, strSummarySheet As String
    Dim intFirstDataRow As Integer, intLastDataRow1 As Integer, intLastDataRow2 As Integer
    Dim sht As Worksheet
    Dim strPart As String, strDetail As String, strOrder As String
    Dim intCount As Integer
    
    Dim i As Integer, j As Integer
    
        'setup/intialise:
        strPartsSheet = "Sheet1"
        strDetailSheet = "Sheet2"
        strSummarySheet = "Sheet3"
        intFirstDataRow = 2
        
        intLastDataRow1 = Worksheets(strPartsSheet).Cells(intFirstDataRow, 1).CurrentRegion.Rows.Count
        intLastDataRow2 = Worksheets(strDetailSheet).Cells(intFirstDataRow, 1).CurrentRegion.Rows.Count
        
        For Each sht In Worksheets
            If sht.Name = strPartsSheet Or sht.Name = strDetailSheet Then
                Worksheets(sht.Name).Select
                Worksheets(sht.Name).Range("A1").Sort _
                    key1:=Worksheets(sht.Name).Columns("A"), order1:=xlAscending, _
                    key2:=Worksheets(sht.Name).Columns("B"), order2:=xlAscending, _
                    header:=xlYes
            End If
        Next sht
        
        intCount = 0
        Worksheets(strSummarySheet).Cells.ClearContents
        
        For i = intFirstDataRow To intLastDataRow1
            For j = i To intLastDataRow2
                If Worksheets(1).Range("A" & i) = Worksheets(2).Range("A" & j) Then
                    If Worksheets(1).Range("B" & i) = Worksheets(2).Range("B" & j) Then
                        strOrder = Worksheets(1).Cells(i, 1).Text
                        strPart = Worksheets(1).Cells(i, 3).Text
                        strDetail = Worksheets(2).Cells(j, 3).Text
                        intCount = intCount + 1
                        Worksheets(strSummarySheet).Range("A" & intFirstDataRow + intCount - 1).Value = strOrder
                        Worksheets(strSummarySheet).Range("B" & intFirstDataRow + intCount - 1).Value = strPart
                        Worksheets(strSummarySheet).Range("C" & intFirstDataRow + intCount - 1).Value = strDetail
                        Exit For   'assuming only 1 match is possible.
                    End If
                End If
            Next j
        Next i
        With Worksheets(strSummarySheet)
            .Columns("A:C").NumberFormat = "@"
            .Columns("A:C").HorizontalAlignment = xlRight
            .Cells(1, 1).Value = "Order Number"
            .Cells(1, 2).Value = "Part Number"
            .Cells(1, 3).Value = "Detail"
            .Range("A1").Sort _
                key1:=Worksheets(3).Columns("A"), order1:=xlAscending, _
                key2:=Worksheets(3).Columns("B"), order2:=xlAscending, _
                header:=xlYes
        End With
    
    End Sub

  5. #5
    Registered User
    Join Date
    03-24-2008
    Posts
    17
    Thank you so very much that is exactly what I was needing! You are a hero in my books. I have been struggling with this for a week now on this issue and you made it look so easy, my hat is off to you.

  6. #6
    Registered User
    Join Date
    03-24-2008
    Posts
    17
    I ran into a small problem, lol there is a possiblility that there might not be a match. Basicly I have an order which says it has a line note(page 1), if you go to the Ordernotes(page2) tab there is no order. On the attachment I threw a order with note on page one and left it out on the second tab for an example if anyone could help.

    Second I know it is possible I just can't think of it, if there is no match after going through all the orders, then I need it to copy the order and part over and then go to the next order.

    I haven't change the code to much as of yet. Only thing I have done is change the rows, sheets locations. I have attached the original code, and my minor modification as of yet. I commented out all the code that I have worked on.
    Attached Files Attached Files

  7. #7
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862

    amended code ...

    Hi Aidenspade ... sure - no problem: here's an amended code that will trap this for you; I've annotated what's going on in the comments so you can follow it ... I've also added a boolean variable called booMatchFound.
    Option Explicit
    Option Base 1
    
    Sub MatchAndPaste()
    Dim strPartsSheet As String, strDetailSheet As String, strSummarySheet As String
    Dim intFirstDataRow As Integer, intLastDataRow1 As Integer, intLastDataRow2 As Integer
    Dim sht As Worksheet
    Dim strPart As String, strDetail As String, strOrder As String
    Dim intCount As Integer
    Dim booMatchFound As Boolean
    Dim i As Integer, j As Integer
    
        'setup/intialise:
        strPartsSheet = "Sheet1"
        strDetailSheet = "Sheet2"
        strSummarySheet = "Sheet3"
        intFirstDataRow = 2
        
        intLastDataRow1 = Worksheets(strPartsSheet).Cells(intFirstDataRow, 1).CurrentRegion.Rows.Count
        intLastDataRow2 = Worksheets(strDetailSheet).Cells(intFirstDataRow, 1).CurrentRegion.Rows.Count
        
        For Each sht In Worksheets
            If sht.Name = strPartsSheet Or sht.Name = strDetailSheet Then
                Worksheets(sht.Name).Select 'selects sheet1
                Worksheets(sht.Name).Range("A1").Sort _
                    key1:=Worksheets(sht.Name).Columns("A"), order1:=xlAscending, _
                    key2:=Worksheets(sht.Name).Columns("B"), order2:=xlAscending, _
                    header:=xlYes
            End If 'sorts pages to assending based on order.
        Next sht
        
        intCount = 0
        Worksheets(strSummarySheet).Cells.ClearContents 'Clears Page three
            
        For i = intFirstDataRow To intLastDataRow1 'Loop till end of sheet
            strOrder = Worksheets(1).Cells(i, 1).Text
            strPart = Worksheets(1).Cells(i, 3).Text
            strDetail = "NO MATCH"
            For j = i To intLastDataRow2
                If Worksheets(1).Range("A" & i) = Worksheets(2).Range("A" & j) Then
                    If Worksheets(1).Range("B" & i) = Worksheets(2).Range("B" & j) Then
                        strDetail = Worksheets(2).Cells(j, 3).Text
                        intCount = intCount + 1
                        Worksheets(strSummarySheet).Range("A" & intFirstDataRow + intCount - 1).Value = strOrder
                        Worksheets(strSummarySheet).Range("B" & intFirstDataRow + intCount - 1).Value = strPart
                        Worksheets(strSummarySheet).Range("C" & intFirstDataRow + intCount - 1).Value = strDetail
                        booMatchFound = True
                        Exit For   'assuming only 1 match is possible.
                    End If
                End If
            Next j
            'if you get to here, you've cycled through the current record in sheet1 and attempted to
            'match it against all the records in sheet2 and found no match; in this case, need to
            'copy just the Order and Part Number to the Summary sheet (Sheet3):
            If booMatchFound = False Then
                intCount = intCount + 1
                Worksheets(strSummarySheet).Range("A" & intFirstDataRow + intCount - 1).Value = strOrder
                Worksheets(strSummarySheet).Range("B" & intFirstDataRow + intCount - 1).Value = strPart
                Worksheets(strSummarySheet).Range("C" & intFirstDataRow + intCount - 1).Value = strDetail
                Exit For
            Else
                booMatchFound = False
            End If
        Next i
        
        With Worksheets(strSummarySheet)
            .Columns("A:C").NumberFormat = "@"
            .Columns("A:C").HorizontalAlignment = xlRight
            .Cells(1, 1).Value = "Order Number"
            .Cells(1, 2).Value = "Part Number"
            .Cells(1, 3).Value = "Detail"
            .Range("A1").Sort _
                key1:=Worksheets(3).Columns("A"), order1:=xlAscending, _
                key2:=Worksheets(3).Columns("B"), order2:=xlAscending, _
                header:=xlYes
        End With
    
    End Sub
    Attached Files Attached Files
    Last edited by MatrixMan; 10-03-2008 at 12:27 PM. Reason: attached file.

+ 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. macro - copying values between two sheets
    By Wedge120 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-15-2008, 10:45 AM
  2. Sealecting rows by value and copying to new sheet
    By cleaco in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-15-2008, 05:50 AM
  3. Copy values from multiple worksheets onto one master sheet
    By CGBatch in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 12-18-2007, 04:46 AM
  4. Copying values on update
    By grant606 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-18-2007, 02:44 PM
  5. copying cell values to specific area in sheet
    By Reinder in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-12-2007, 09:55 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