Macro to Find a match and update corresponding value

laxminarayana

Board Regular
Joined
Nov 16, 2013
Messages
56
Hello All,

I have something similar situation on my workbook. Sheet1 has large amount of data with Product name (column A) and current value (column D). There are some blank records in between too. Sheet2 has Product name (column A) and sold amount (column B). There are no blank records in between for this sheet. I'm trying to search column A of Sheet2 in column A of Sheet1. If match is found in Sheet1, then amount in Column D has to be deducted by the amount of sheet2 (column B). In short deducting the sold amount in sheet2 from the total amount in sheet1. I have attached a simplified version of what my worksheets look like. Can you please help with some code.

Thank you!
 

Attachments

  • Inventory Sheet Screenshot.jpg
    Inventory Sheet Screenshot.jpg
    188.1 KB · Views: 32

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
VBA Code:
Sub Deduct()
     Set c1 = Sheets("sheet1").UsedRange.Resize(, 4)            'has empty row in between
     Set c2 = Sheets("sheet2").Range("A1").CurrentRegion.Resize(, 5)     '1 contigious block
     c1.Columns(1).Name = "Prod_Store"
     a = [transpose(prod_store&"|"&offset(prod_store,,2))]      'c1, Columns 1&3 together in an array in memory

     For i = 2 To c2.Rows.Count                                 'loop through all data in sheet2
          r = Application.Match(c2(i, 1).Value & "|" & c2(i, 4), a, 0)     'find match product and store in A-column sheet1
          If IsNumeric(r) Then                                  'match found
               c1(r, 4) = c1(r, 4) - c2(i, 2)                   'decrease value for product&store in sheet1 with the value found in sheet2
          End If
     Next
End Sub
 
Upvote 0
VBA Code:
Sub Deduct()
     Set c1 = Sheets("sheet1").UsedRange.Resize(, 4)            'has empty row in between
     Set c2 = Sheets("sheet2").Range("A1").CurrentRegion.Resize(, 5)     '1 contigious block
     c1.Columns(1).Name = "Prod_Store"
     a = [transpose(prod_store&"|"&offset(prod_store,,2))]      'c1, Columns 1&3 together in an array in memory

     For i = 2 To c2.Rows.Count                                 'loop through all data in sheet2
          r = Application.Match(c2(i, 1).Value & "|" & c2(i, 4), a, 0)     'find match product and store in A-column sheet1
          If IsNumeric(r) Then                                  'match found
               c1(r, 4) = c1(r, 4) - c2(i, 2)                   'decrease value for product&store in sheet1 with the value found in sheet2
          End If
     Next
End Sub

I have pasted the exact code in module and run the code. It did nothing. I'm I missing something.
 
Upvote 0
laxminarayana.xlsm
columns 1&2 instead of 1&3:oops:
VBA Code:
Sub Deduct()
     Set c1 = Sheets("sheet1").UsedRange.Resize(, 4)            'has empty row in between
     Set c2 = Sheets("sheet2").Range("A1").CurrentRegion.Resize(, 5)     '1 contigious block
     c1.Columns(1).Name = "Prod_Store"
     a = [transpose(prod_store&"|"&offset(prod_store,,1))]      'c1, Columns 1&2 together in an array in memory

     For i = 2 To c2.Rows.Count                                 'loop through all data in sheet2
          r = Application.Match(c2(i, 1).Value & "|" & c2(i, 4), a, 0)     'find match product and store in A-column sheet1
          If IsNumeric(r) Then                                  'match found
               c1(r, 4) = c1(r, 4) - c2(i, 2)                   'decrease value for product&store in sheet1 with the value found in sheet2
          End If
     Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,737
Messages
6,126,559
Members
449,318
Latest member
Son Raphon

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top