Lookup and Replace From Another Sheet

Caldrumr

New Member
Joined
Aug 5, 2010
Messages
12
Last week, Akuini gave me an awesome script that I was able to use for one of my worksheets. :)
I tried to expand on this to account for another scenario I needed to cover for a different sheet in the same workbook, but unfortunately, my lack of Excel skills is knocking me down again.

When I run this, I get an error saying my subscript is out of range.
I'm not sure if I'm completely off the rails with trying to adapt that code to meet this scenario, or it just needs some adjustment.
I think I'm counting the rows on one sheet, then comparing to another sheet, and it doesn't work that way?

Basically, I want to say that if no inventory value exists on the first sheet, check the second sheet for the same item number, and replace the inventory value with the one from that sheet.


Here is the code I tried and failed to work with:
Code:
Sub Calculate2()Dim xa, xb, xc, xd, xe, xf
Dim i As Long, n As Long, m As Long


n = Range("C" & Rows.Count).End(xlUp).Row
m = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row


xa = Range("C14:C" & n)
xb = Range("AE14:AE" & n)
xc = Range("AF14:AF" & n)
xd = Range("Y14:Y" & n)
xe = Sheets("Sheet1").Range("A11:A" & m)
xf = Sheets("Sheet1").Range("K11:K" & m)


    For i = UBound(xe, 1) - 1 To 1 Step -1
        If xa(i, 1) = xe(i, 1) And xd(i, 1) < 1 Then xb(i, 1) = xf(i + 1, 1)
    Next
    


Range("C14:C" & n) = xa
Range("AE14:AE" & n) = xb
Range("AF14:AF" & n) = xc
Range("Y14:Y" & n) = xd
Range("Sheet1.A11:A" & m) = xe
Range("Sheet1.K11:K" & m) = xf


End Sub

ax = item number from first sheet
xb = column for new value
xd = existing value on first sheet
xe = item number of second sheet
xf = value on second sheet

I hope I'm being clear.
Basically:
IF xd is blank or zero THEN match xe with ax and populate xb with xf.

Let me know if there are any questions, and thank you guys for all the help!
 
Last edited:

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

Forum statistics

Threads
1,214,861
Messages
6,121,969
Members
449,059
Latest member
oculus

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