库存宏-将信息从一页纸复制到另一页

时间:2019-01-27 21:50:26

标签: excel vba

我正在尝试为库存创建一个宏,实际上,工作表1具有所有信息,包括条形码编号,产品名称和产品尺寸(按行)。在工作表2中,我有一个按钮,单击该按钮后,我可以扫描手中的产品条形码。

我想要的是,当我扫描的条形码与工作表1中的条形码之一匹配时,与该产品有关的所有其他信息都被复制到工作表2或另一工作表中。我不确定哪个会更容易。 现在,我所拥有的代码仅在匹配的条形码旁边添加了一些其他信息。我不确定如何将这些信息以及原始信息从工作表1添加到新工作表中。

Private Sub CommandButton1_Click()

Dim MatchRow As Variant
Dim code As Variant
Dim matchedCell As Range

barcode = InputBox("Please scan a barcode and hit enter if you need to")

' verify that there is a successful match in the searched range

Set Shta = ActiveWorkbook.Worksheets("Sheet1")
Set Shtb = ActiveWorkbook.Worksheets("Sheet2")
Set Sheet1Range = Sheet1.Range("D2:D108")

If Not IsError(Application.Match(barcode, Sheet1Range, 0)) Then
    MatchRow = Application.Match(barcode, Sheet1Range, 0) '<-- get the row number
    MatchCatalog = MatchRow + 1
    Set matchedCell = Range("D" & MatchCatalog) '<-- set the range (add 1 row since you are starting from row 2)
    matchedCell.Offset(0, 2).Value = Now
    matchedCell.Offset(0, 3).Value = MatchCatalog
    matchedCell.Offset(0, 4).Value = barcode


    'option 2: without setting the range
    Range("AE" & MatchRow).Offset(1, 2).Value = Now
End If

End Sub

提前谢谢! 即使是一些有关如何将整行数据复制到工作表中的帮助,也将从一开始就有用。

1 个答案:

答案 0 :(得分:0)

您可以使用VLOOKUPINDEX(MATCH())导入信息,具体取决于数据的设置方式。您可以只使用宏将barcode放到单元格中。这将是您的VLOOKUP

中的查找值(第一个条件)

以下内容会将您的条形码放在A1

Private Sub CommandButton1_Click()

Dim Barcode
barcode = InputBox("Please scan a barcode and hit enter if you need to")

ThisWorkbook.Sheets("Sheet2").Range("A1") = Barcode

End Sub