库存宏-结合步骤和while循环?

时间:2019-02-03 00:25:57

标签: excel vba

我正在研究一个库存控制宏,在其中扫描代码,区域,团队和条形码,以查看是否正在扫描该项目。现在,我为每个功能使用4个不同的按钮(1.产品2.团队3.区域4.输入/输出)。我希望有人可以帮助改善这一点,因此只需要按下一个按钮,您就可以连续将所有4个按钮扫描到弹出框中。我还试图弄清楚如何创建while循环,以便在对一种产品的所有这四个类别进行扫描后,它会自动跳至下一行以开始扫描新产品。我假设这将需要一个do while循环,但是由于我还没有自动完成所有4个步骤,因此我正在努力实现这一点。

我曾尝试运行do while代码,但我正努力使其正常工作,但我首先要努力弄清楚如何使上一步之后的四个步骤自动弹出。

Sub ScanProductBarcode_Click()

Dim Barcode
Dim ProductGroup

Barcode = InputBox("Please scan a PRODUCT barcode and hit enter if you need to")
ThisWorkbook.Sheets("Sheet2").Range("A2") = Barcode
ThisWorkbook.Sheets("Sheet2").Range("A2").Offset(0, 5) = Now 'Cell Formatted for Date
ThisWorkbook.Sheets("Sheet2").Range("A2").Offset(0, 6) = Now 'Cell Formatted for Time

End Sub

Sub ScanTeamBarcode_Click()

Dim Team

Team = InputBox("Please scan the TEAM barcode and hit enter if you need to")
ThisWorkbook.Sheets("Sheet2").Range("H2") = Team

End Sub


Sub ScanRegionBarcode_Click()

Dim Region

Region = InputBox("Please scan the REGION barcode and hit enter if you need to")
ThisWorkbook.Sheets("Sheet2").Range("J2") = Region

End Sub

Sub ScanInOut_Click()

Dim InOut

InOut = InputBox("Please scan the device IN or OUT barcode and hit enter if you need to")
ThisWorkbook.Sheets("Sheet2").Range("L2") = InOut

End Sub

希望有人可以帮助我完成所有这四个扫描步骤,只需单击一下按钮,然后放到excel的下一行后即可开始扫描下一个产品。 预先感谢!

1 个答案:

答案 0 :(得分:2)

要循环扫描所有条形码:

Sub ScanAllBarcodes_Click()

    Dim Barcode, Team, Region, InOut
    Dim ProductGroup, rw As Range

    Do

        Barcode = GetBC("PRODUCT")
        If Len(Barcode) = 0 Then Exit Do '<<<  empty input terminates loop

        Team = GetBC("TEAM")
        Region = GetBC("REGION")
        InOut = GetBC("device IN or OUT")

        Set rw = ThisWorkbook.Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Offset(1).EntireRow
        'make sure the row is really empty (in case of missing "A" value)
        Do While Application.CountA(rw) > 0
            Set rw = rw.Offset(1, 0)
        Loop

        With rw
            .Cells(1) = Barcode
            .Cells(6) = Now
            .Cells(7) = Now
            .Cells(8) = Team
            .Cells(10) = Region
            .Cells(12) = InOut
        End With

    Loop

End Sub

Function GetBC(nm)
    GetBC = InputBox("Please scan a " & nm & " barcode and hit enter if you need to")
End Function