简体   繁体   English

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

[英]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.1960.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.

相关问题 如何使用 VBA 代码根据单元格值隐藏 Excel 表 - How to hide Excel Sheet based on cell value using VBA Code 如何使用另一个工作表中单元格内的单元格地址更改具有 Excel vba 的单元格的值? - How to change the value of a cell with Excel vba using a cell address that's inside a cell in another worksheet? Excel:基于另一个工作表中的单元格值动态更改工作表标签 - Excel: Dynamically change sheet tag based on cell value in another sheet 遍历填充的行,根据条件匹配,使用 VBA 将值添加到 Excel 中另一个工作表中的特定单元格 - Iterate through populated rows, Match based on criteria, add value to specific cell in another sheet in Excel using VBA 如何在不使用宏或VBA的情况下基于另一个工作表中的单元格值隐藏/取消隐藏工作表 - How to hide/un hide a sheet based on a cell value in another sheet without using macro or vba 使用 VBA 根据另一个单元格值设置 Excel 单元格值 - Setting Excel cell value based on another cell value using VBA excel VBA根据条件从列表(在另一个工作表中)选择一个值 - excel VBA select a value from a list (in another sheet) based on a condition 使用VBA将单元格值复制到Excel中的另一个工作表 - Copying Cell Value to another sheet in Excel with VBA AppleScript & Excel:根据另一个单元格的值更改单元格 - AppleScript & Excel: change cell based on another cell's value 根据另一个工作表单元格值的值更改工作表中的单元格值 - Change a cell value in a sheet based on the value of another sheet cell value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM