Using VBA to search for a matching value on another sheet when a row is added

%3CLINGO-SUB%20id%3D%22lingo-sub-1496711%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20VBA%20to%20search%20for%20a%20matching%20value%20on%20another%20sheet%20when%20a%20row%20is%20added%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1496711%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F713575%22%20target%3D%22_blank%22%3E%40crystalhowat%3C%2FA%3E%26nbsp%3BYour%20sheet%20is%20working%20.%26nbsp%3B%20Can%20you%20please%20text%20me%20in%20Private%20to%20explain%20your%20actual%20problem.%20I%20will%20share%20my%20mail%20id%20there%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1496890%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20VBA%20to%20search%20for%20a%20matching%20value%20on%20another%20sheet%20when%20a%20row%20is%20added%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1496890%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F699938%22%20target%3D%22_blank%22%3E%40DevendraJain%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENo%2C%20it%20is%20not%20updating%20column%20L%20on%20the%20MASTER%20worksheet%20when%20we%20add%20a%20row%20to%20the%20GR%20worksheet.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1496691%22%20slang%3D%22en-US%22%3EUsing%20VBA%20to%20search%20for%20a%20matching%20value%20on%20another%20sheet%20when%20a%20row%20is%20added%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1496691%22%20slang%3D%22en-US%22%3E%3CP%3EIn%20a%20single%20workbook%2C%20I%20have%20one%20sheet%20that%20is%20our%20MASTER%20list%20of%20inventory%20items.%20On%20another%20sheet%20(called%20GR)%20where%20we%20log%20when%20new%20inventory%20is%20received.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhen%20a%20new%20item%20is%20added%20to%20the%20GR%20table%2C%20I%20want%20a%20VBA%20to%20search%20for%20the%20matching%20item%20by%20SKU%20on%20the%20MASTER%20worksheet%20and%20update%20the%20number%20of%20inventory%20in%20stock%20based%20on%20the%20value%20we%20received%20(on%20the%20GR%20table)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20pretty%20new%20to%20VBA%20so%20I%20am%20just%20stumped.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EA%20copy%20of%20my%20workbook%20is%20attached.%20Module%201%20is%20the%20one%20that%20I%20have%20some%20code%20I%20found%20on%20another%20forum%20and%20I%20was%20trying%20to%20update%20it%2C%20but%20it's%20above%20my%20head.%20Please%20help%20me!%20(Edited%20Module%20Number)%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1496691%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1497147%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20VBA%20to%20search%20for%20a%20matching%20value%20on%20another%20sheet%20when%20a%20row%20is%20added%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1497147%22%20slang%3D%22en-US%22%3E%3CP%3EHere%20is%20where%20I%20am%20currently%20at.%20I%20don't%20get%20any%20errors%2C%20but%20I'm%20not%20getting%20any%20action%20either%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3ESub%20FindValues()%0A%0ADim%20lookUpSheet%20As%20Worksheet%2C%20updateSheet%20As%20Worksheet%0ADim%20valueToSearch%20As%20String%0ADim%20i%20As%20Integer%2C%20t%20As%20Integer%0ADim%20newstock%20As%20Integer%0ADim%20instock%20As%20Integer%0A%0ASet%20lookUpSheet%20%3D%20Worksheets(%22GR%22)%0ASet%20updateSheet%20%3D%20Worksheets(%22MASTER%22)%0A%0A'get%20the%20number%20of%20the%20last%20row%20with%20data%20in%20sheet1%20and%20in%20sheet2%0AlastRowLookup%20%3D%20lookUpSheet.Cells(Rows.Count%2C%20%22A%22).End(xlUp).Row%0AlastRowUpdate%20%3D%20updateSheet.Cells(Rows.Count%2C%20%22A%22).End(xlUp).Row%0A%0A'for%20every%20value%20in%20column%20A%20of%20MASTER%0AFor%20i%20%3D%201%20To%20lastRowUpdate%0A%20%20%20%20%20valueToSearch%20%3D%20updateSheet.Cells(i%2C%201)%0A%20%20%20%20%20'look%20the%20value%20in%20column%20B%20of%20GR%0A%20%20%20%20%20For%20t%20%3D%202%20To%20lastRowLookup%0A%20%20%20%20%20%20%20%20'if%20found%20a%20match%2C%20copy%20column%20B%20value%20to%20sheet1%20and%20proceed%20to%20the%20next%20value%0A%20%20%20%20%20%20%20%20If%20lookUpSheet.Cells(t%2C%202)%20%3D%20valueToSearch%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20Set%20newstock%20%3D%20lookUpSheet.Cells(t%2C%208)%0A%20%20%20%20%20%20%20%20%20%20%20%20Set%20instock%20%3D%20updateSheet.Cells(i%2C%2012)%0A%20%20%20%20%20%20%20%20%20%20%20%20updateSheet.Cells(i%2C%2012).Value%20%3D%20Excel.WorksheetFunction.Sum(newstock%20%2B%20instock)%0A%20%20%20%20%20%20%20%20%20%20%20%20Exit%20For%0A%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20%20Next%20t%0ANext%20i%0A%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1951283%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20VBA%20to%20search%20for%20a%20matching%20value%20on%20another%20sheet%20when%20a%20row%20is%20added%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1951283%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F713575%22%20target%3D%22_blank%22%3Ecrystalhowat%3C%2FA%3E%2C%20this%20is%20probably%20too%20late%20to%20help%20you%20but%20I've%20posted%20this%20in%20case%20it%20is%20of%20use%20to%20others%20following.%26nbsp%3B%20I%20made%20some%20changes%20so%20that%20it%20works%20for%20me.%26nbsp%3B%20tt%20is%20the%20subscript%20for%20the%20transaction%20file%20and%20mm%26nbsp%3Bis%20the%20subscript%20for%20the%20master%20file.%26nbsp%3B%20I%20read%20each%20row%20in%20the%20transaction%20file%20and%20search%20for%20a%20match%20on%20the%20master%20then%20update%20the%20master.%26nbsp%3B%20The%20code%20below%20is%20only%20sample%20code%20not%20my%20full%20processing.%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-visual%22%3E%3CCODE%3ESub%20FindValues()%0A%0ADim%20lookUpSheet%20As%20Worksheet%2C%20updateSheet%20As%20Worksheet%0ADim%20valueToSearch%20As%20String%0ADim%20tt%20As%20Integer%2C%20mm%20As%20Integer%0ADim%20newstock%20As%20Integer%0ADim%20instock%20As%20Integer%0A%0ASet%20lookUpSheet%20%3D%20Worksheets(%22From%20LinkedIn%22)%0ASet%20updateSheet%20%3D%20Worksheets(%22Contacts_Reformatted%22)%0A%0A'get%20the%20number%20of%20the%20last%20row%20with%20data%20in%20Contacts_Reformatted%20and%20in%20From%20LinkedIn%0A%20%20%20%20lastRowLookup%20%3D%20lookUpSheet.Cells(Rows.Count%2C%20%22A%22).End(xlUp).Row%0A%20%20%20%20lastRowUpdate%20%3D%20updateSheet.Cells(Rows.Count%2C%20%22A%22).End(xlUp).Row%0A%20%20%20%20%0A'for%20every%20value%20in%20column%20A%20of%20From%20LinkedIn%0A%20%20%20%20For%20tt%20%3D%201%20To%20lastRowLookup%0A%20%20%20%20%20%20%20%20%20valueToSearch%20%3D%20lookUpSheet.Cells(tt%2C%203)%0A%20%20%20%20%20%20%20%20%20'look%20for%20the%20value%20in%20column%20D%20of%20Contacts_Reformatted%0A%20%20%20%20%20%20%20%20%20For%20mm%20%3D%202%20To%20lastRowUpdate%0A%20%20%20%20%20%20%20%20%20%20%20%20'if%20found%20a%20match%2C%20copy%20column%20B%20value%20to%20sheet1%20and%20proceed%20to%20the%20next%20value%0A%20%20%20%20%20%20%20%20%20%20%20%20If%20updateSheet.Cells(mm%2C%204)%20%3D%20valueToSearch%20Then%0A%20%20%20%20'%20%20%20%20%20%20%20%20%20%20%20%20Set%20newstock%20%3D%20lookUpSheet.Cells(mm%2C%208)%0A%20%20%20%20'%20%20%20%20%20%20%20%20%20%20%20%20Set%20instock%20%3D%20updateSheet.Cells(tt%2C%2012)%0A%20%20%20%20'%20%20%20%20%20%20%20%20%20%20%20%20updateSheet.Cells(tt%2C%2012).Value%20%3D%20newstock%20%2B%20instock%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20updateSheet.Cells(mm%2C%2029).Value%20%3D%20%22IRATA%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Exit%20For%0A%20%20%20%20%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20%20%20%20%20%20Next%20mm%0A%20%20%20%20Next%20tt%0A%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

In a single workbook, I have one sheet that is our MASTER list of inventory items. On another sheet (called GR) where we log when new inventory is received. 

 

When a new item is added to the GR table, I want a VBA to search for the matching item by SKU on the MASTER worksheet and update the number of inventory in stock based on the value we received (on the GR table)

 

I'm pretty new to VBA so I am just stumped. 

 

A copy of my workbook is attached. Module 1 is the one that I have some code I found on another forum and I was trying to update it, but it's above my head. Please help me! (Edited Module Number)

4 Replies

@crystalhowat Your sheet is working .  Can you please text me in Private to explain your actual problem. I will share my mail id there

 

@DevendraJain 

No, it is not updating column L on the MASTER worksheet when we add a row to the GR worksheet. 

Here is where I am currently at. I don't get any errors, but I'm not getting any action either:

 

Sub FindValues()

Dim lookUpSheet As Worksheet, updateSheet As Worksheet
Dim valueToSearch As String
Dim i As Integer, t As Integer
Dim newstock As Integer
Dim instock As Integer

Set lookUpSheet = Worksheets("GR")
Set updateSheet = Worksheets("MASTER")

'get the number of the last row with data in sheet1 and in sheet2
lastRowLookup = lookUpSheet.Cells(Rows.Count, "A").End(xlUp).Row
lastRowUpdate = updateSheet.Cells(Rows.Count, "A").End(xlUp).Row

'for every value in column A of MASTER
For i = 1 To lastRowUpdate
     valueToSearch = updateSheet.Cells(i, 1)
     'look the value in column B of GR
     For t = 2 To lastRowLookup
        'if found a match, copy column B value to sheet1 and proceed to the next value
        If lookUpSheet.Cells(t, 2) = valueToSearch Then
            Set newstock = lookUpSheet.Cells(t, 8)
            Set instock = updateSheet.Cells(i, 12)
            updateSheet.Cells(i, 12).Value = Excel.WorksheetFunction.Sum(newstock + instock)
            Exit For
        End If
     Next t
Next i

End Sub

 

Hi crystalhowat, this is probably too late to help you but I've posted this in case it is of use to others following.  I made some changes so that it works for me.  tt is the subscript for the transaction file and mm is the subscript for the master file.  I read each row in the transaction file and search for a match on the master then update the master.  The code below is only sample code not my full processing.

Sub FindValues()

Dim lookUpSheet As Worksheet, updateSheet As Worksheet
Dim valueToSearch As String
Dim tt As Integer, mm As Integer
Dim newstock As Integer
Dim instock As Integer

Set lookUpSheet = Worksheets("From LinkedIn")
Set updateSheet = Worksheets("Contacts_Reformatted")

'get the number of the last row with data in Contacts_Reformatted and in From LinkedIn
    lastRowLookup = lookUpSheet.Cells(Rows.Count, "A").End(xlUp).Row
    lastRowUpdate = updateSheet.Cells(Rows.Count, "A").End(xlUp).Row
    
'for every value in column A of From LinkedIn
    For tt = 1 To lastRowLookup
         valueToSearch = lookUpSheet.Cells(tt, 3)
         'look for the value in column D of Contacts_Reformatted
         For mm = 2 To lastRowUpdate
            'if found a match, copy column B value to sheet1 and proceed to the next value
            If updateSheet.Cells(mm, 4) = valueToSearch Then
    '            Set newstock = lookUpSheet.Cells(mm, 8)
    '            Set instock = updateSheet.Cells(tt, 12)
    '            updateSheet.Cells(tt, 12).Value = newstock + instock
                updateSheet.Cells(mm, 29).Value = "IRATA"
                Exit For
            End If
         Next mm
    Next tt

End Sub