Home » excel » excel – Inventory Macro – combining steps and while loops?

excel – Inventory Macro – combining steps and while loops?

Posted by: admin May 14, 2020 Leave a comment


I am working on an inventory control macro where i scan in codes, regions, teams, and a barcode for whether the item is being scanned in or out. Right now I am using 4 different buttons for each of these functions (1. product 2. team 3. region 4. in/out). I am hoping someone can help with improving this so only one button needs to be pressed and you just scan all 4 into pop-up boxes consecutively. I also am trying to figure out how to create a while loop so that once one product is scanned for all 4 of these categories it automatically jumps down to the next row to begin scanning a new product. I’m assuming this would require a do while loop but i’m struggling with implementing this since I don’t have all 4 of the steps happening automatically already.

I have tried running a do while code but I am struggling to get it to work but I am first struggling with figuring out how to get the four steps to automatically pop-up after the previous.

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

Hoping someone can help me with running all four of these scanning steps with just one button click, and then after dropping to the next row of the excel to begin scanning the next product.
Thanks in advance!

How to&Answers:

To scan all barcodes in a loop:

Sub ScanAllBarcodes_Click()

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


        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)

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


End Sub

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