需要更新另一张工作表EXCEL中的数据

我正在写一个代码,它必须从表单3中获取数据并将数据传输到另一个表单。 有了这部分是一切OK。 同样的点击,我也希望该程序在相同的表单sheet3查看数据,从每个填充的行获取Item's ID (它在列A中),然后在sheet1 (列C)中查找匹配项。 然后代码find匹配它确定匹配的行,并在单元格(列I),即从表单3取值。 它在列4中。然后在sheet1 – >匹配的行 – >列我使用sheet3这个值进行相减,我附加了printscreen的清晰度。

在这里input图像说明

  Option Explicit Sub Button4_Click() Dim x As Long Dim erow As Long Dim y as Long Dim roww as Long Dim matchess as Integer Dim IDitem as Integer Dim myrange as Long 'Calculate starting rows x = 15 With Worksheets("sheet2") erow = .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0).Row End With myrange = Worksheets("sheet1").Cells.Range("C:I") With Worksheets("sheet3") Do While .Cells(x, 1) <> "" 'The next line copies values to Sheet2 Worksheets("sheet2").Range("A" & erow & ":Z" & erow).Value = .Range("A" & x & ":Z" & x).Value 'increment row counters x = x + 1 erow = erow + 1 Loop End With y = 15 With Worksheets("sheet1") Do While Worksheets("sheet3").Cells(y, 1) <> "" matchess = Worksheets("sheet3").Cells(y, 1) IDitem = Application.WorksheetFunction.VLookup(matchess, myrange, 4, False) roww = Application.Match(matches, myrange, 0) .cell(roww, 7).Value = .cell(roww, 7).Value - IDitem y = y + 1 Loop End With End Sub 

我不知道为什么,但代码的第二部分不起作用。

附加信息:表3:行从15开始,合并列A和B中的项目ID。合并列“S,T,U”中的金额; sheet1:列C中的项目ID,存储在列I中;

伙计们帮我修改代码

过去我使用.Find方法来处理这样的问题。 我已经在代码中包含了循环的解释,但是我很乐意详细说明您是否需要。

 Sub Find_Method() Dim LastRow_1 As Long Dim LastRow_3 As Long Dim Source_Item_ID_Range As Range Dim Search_Item_ID_Range As Range Dim Item_ID_Found_Cell As Range Dim Cell As Range LastRow_1 = Worksheets("Sheet1").Range("C" & Rows.Count).End(xlUp).Row LastRow_3 = Worksheets("Sheet3").Range("A" & Rows.Count).End(xlUp).Row Set Search_Item_ID_Range = Worksheets("Sheet1").Range("C4:C" & LastRow_1) Set Source_Item_ID_Range = Worksheets("Sheet3").Range("A15:A" & LastRow_3) 'For each Item ID cell in Sheet3, search for its 'string in the Item ID column of Sheet1. If a match 'is found, deduct 1 from the Warehouse Stock column 'and move on to the next cell. For Each Cell In Source_Item_ID_Range Set Item_ID_Found_Cell = Search_Item_ID_Range.Find(Cell.Value, LookIn:=xlValues) If Not Item_ID_Found_Cell Is Nothing Then Item_ID_Found_Cell.Offset(0, 6).Value = Item_ID_Found_Cell.Offset(0, 6).Value - Cell.Offset(0, 3).Value End If Next Cell End Sub