微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何根据 Excel 中另一个工作表上的列表使用 vba 更改单元格的值?

如何解决如何根据 Excel 中另一个工作表上的列表使用 vba 更改单元格的值?

请多多包涵。我想在 if 中编写一个 VBA 语句,该语句查看一张纸上的值列表,如果这些值在第二张纸上,则使用 {{1 }} 和公式。

我将尝试展示我所拥有的样本。

Sheet1 表格:

VBA

Part # | supplier

Y67 | supplierY

X23 | supplierX

Sheet2 表格:

Z11 | supplierZ

Part # | supplier | Quantity 1 | Quantity 2

Y799 | supplierY | 644541 | 332154

X23 | supplierX | 97845 | 399987

使用这两个表格示例,我想知道我可以编写哪些 Z555 | supplierZ | 4454512 | 2237419 代码来查看 Sheet1 表格并查看是否在Sheet2 表。如果找到它们,那么我需要使用公式 VBA 更改数量。在此示例中,=(Cell Value/ 1000 / 500) 零件号在 Sheet2 中找到,因此新数量应为 X230.196

我希望这解释得足够好。提前致谢!

解决方法

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。