[英]How to change a cell's value using vba based off a list on another sheet in Excel?
Please bear with me.请多多包涵。 I want to write an
if
Statement in VBA
that looks at a list of values on one sheet and if those values are on the second sheet, then change the cell's value on the second sheet using VBA
and formula.我想在
VBA
中编写一个if
语句,它查看一张纸上的值列表,如果这些值在第二张纸上,然后使用VBA
和公式更改第二张纸上的单元格值。
I'll try to show a sample of what I have.我将尝试展示我所拥有的样本。
Sheet1 Table: Sheet1表:
Part # | Supplier
Y67 | SupplierY
X23 | SupplierX
Z11 | SupplierZ
Sheet2 Table: Sheet2表:
Part # | Supplier | Quantity 1 | Quantity 2
Y799 | SupplierY | 644541 | 332154
X23 | SupplierX | 97845 | 399987
Z555 | SupplierZ | 4454512 | 2237419
Using these two table examples, I would like to know what VBA
code I could write to be able to look at Sheet1 Table and see if any of those Part Numbers are found in Sheet2 Table.使用这两个表示例,我想知道我可以编写什么
VBA
代码,以便能够查看Sheet1表并查看是否在Sheet2表中找到了这些部件号。 If they are found, then I would need the quantities changed using the formula =(Cell Value/ 1000 / 500)
.如果找到它们,那么我需要使用公式
=(Cell Value/ 1000 / 500)
更改数量。 In this example X23
part number is found in Sheet2 so the new quantities should be 0.196
and 0.800
.在此示例中,
X23
零件编号位于Sheet2中,因此新数量应为0.196
和0.800
。
I hope this is explained well enough.我希望这解释得足够好。 Thanks in advance!
提前致谢!
sRow1 = 1 'Starting row of data on sheet 1
iRow1 = 0
sCol1 = 1 'The column your part numbers are in on sheet 1
sht1 = "Sheet1" 'Whatever the sheet name is
sRow2 = 1 'Starting row of data on sheet 2
sCol2 = 1 'The column your part numbers are in on sheet 2
sht2 = "Sheet2" 'Again, whatever the sheet name is
Do Until IsEmpty(Sheets(sht1).cells(sRow1+iRow1), sCol1))
'Loop through all parts on first sheet
iRow2 = 0
Do Until IsEmpty(sheets(sht2).cells(sRow2+iRow2, sCol2))
if sheets(sht2).cells(sRow2+iRow2, sCol2) = Sheets(sht1).cells(sRow1+iRow1, sCol1) and _
sheets(sht2).cells(sRow2+iRow2, sCol2+1) = Sheets(sht1).cells(sRow1+iRow1, sCol1+1) then
'We have a matching part number & supplier
partQty1 = sheets(sht2).cells(sRow2+iRow2, sCol2+2)
partQty2 = sheets(sht2).cells(sRow2+iRow2, sCol2+3)
Sheets(sht1).cells(sRow1+iRow1, sCol1+2) = partQty1/1000/500
Sheets(sht1).cells(sRow1+iRow1, sCol1+3) = partQty2/1000/500
exit do 'Exits the do loop in second sheet
end if
iRow2 = iRow2 + 1
Loop
iRow1 = iRow1 + 1
Loop
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.