添加 Vlookup + Vlookup 与活动单元格

问题描述 投票:0回答:1

我有表 1 和表 2。它们代表两张表,为了解释目的,我在同一张表中制作了这两张表。
enter image description here

我需要当我按下红色按钮时,VBA 代码从单元格 C11 中取出 100,添加到单元格 I11(请注意,I11 位于不同的工作表中),然后删除单元格 C11 和 E11 中的所有内容。

由于它只是一个用于多行的按钮,我读到您可以使用“活动单元格”来避免为每一行创建一个按钮。

这是我想到的将这两个相加的公式。我不知道如何将其放入 VBA 中以与“activesheet”一起运行。

=IF(NOT(ISBLANK(G11)),VLOOKUP(A11,G11:I20,3,0))+ VLOOKUP(A11,A11:E20,3,0)

结果应该是:
enter image description here

如何将 activesheet 与 vlookup 公式合并在一起?

删除我之前使用过的单元格,以便我可以查找它。

excel vba vlookup
1个回答
2
投票

请尝试使用下一个代码。您可以从已有的按钮调用它,或者将其复制As相应的代码:

Sub updateQuantity()
  Dim rng As Range, ws2 As Worksheet, rngItem As Range
  
  Set rng = ActiveCell
  If rng.cells.count > 1 Then MsgBox "You need to select only ONE CELL...": Exit Sub
  If rng.column <> 1 Then MsgBox "The active cell must be in A:A column...": Exit Sub
  
  Set ws2 = rng.Parent.Next ' Set the second worksheet
                            ' in this code it is the next sheet after the one keeping active cell
                            
  Set rngItem = ws2.Range("A:A").Find(What:=rng.value, LookAt:=xlWhole) 'find the Item in the second sheet
  If Not rngItem Is Nothing Then 'if the item has been found:
     'add the existing value in I:I (found range row) with the one from C:C column (activecell row)
     ws2.cells(rngItem.Row, "I").value = ws2.cells(rngItem.Row, "I").value + rng.Offset(, 2).value
     rng.Offset(, 2).Resize(, 3).ClearContents 'clear content of C:E columns of the active cell row
  Else
     MsgBox "No any match for item """ & rng.value & """ has been found in """ & _
           ws2.name & """ sheet..."
  End If
End Sub

如果您的按钮是

ActiveX
类型,请在其
updateQuantity
事件中写入
Click
。如果它是一个
Form
按钮,请将上面的宏分配给它。

如果有些事情不够清楚,请随时要求澄清......

© www.soinside.com 2019 - 2024. All rights reserved.