Scan multiple barcodes into a single Excel cell, each on a separate line

被刻印的时光 ゝ 提交于 2019-12-11 07:43:13

问题


A library has an Excel sheet that's used as a packing list for books to be loaned to other libraries. Each row represents a single box being packed. The cells of that row have info such as Recipient, Shipment number, Box number, etc.

In one of the columns they'd like to scan the barcoded "Acquisition Number" of each of the several books that will go in that box, such that each scanned number will appear on a new line in that cell.

But when they scan a barcode, Excel moves to the next row, since the scanner is programmed to terminate the scanned data with CR/LF.

They cannot reprogram the scanners, as they are used for other apps.

How can we get Excel to:

If we're in column C
    When Excel detects CR/LF
        If the cell was not empty
            Append a new line to the contents of the cell
        Append the scanned data to the contents of the cell
        Stay in that cell ready for the next scan

Then they can press an arrow key, if required, to get out of the cell.

-- adTHANKSvance


回答1:


Most people are aware that macro code (including event macros like Worksheet_Change) will kill the .Undo buffer if allowed to run through but are unaware that the .Undo buffer can be caught and used before cell values are changed. In other words, you can grab the new value, force an .Undo and then append the new value to the old with a line feed e.g. Chr(10) as a delimiter.

Right-click your worksheet's name tab and choose View Code. When the VBE opens up, paste the following into the pane titled something like Book1 - Sheet1 (Code).

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count > 1 Then Exit Sub
    If Not Intersect(Target, Range("C2:C10")) Is Nothing Then
        On Error GoTo Fìn
        Application.EnableEvents = False
        Dim vNew As Variant
        vNew = Target.Value
        Application.Undo
        If Not IsEmpty(Target) Then
            Target = Target.Value & Chr(10) & vNew
        Else
            Target = vNew
        End If
        'Target.Offset(1,0).Activate
    End If
Fìn:
    Application.EnableEvents = True
End Sub

You didn't supply any specifics so I imagined the barcode input range as C2:C10. You should be able to modify that portion to a closer approximation of the range that the barcodes are entered into.

When you think you have the input range right, tap Alt+Q to return to your worksheet.

Caveat: I don't have a barcode scanner to work with but I have used them in the past and they have always closely imitated keyboard input (depending upon the bar code scanner software). This works for keyboard input so make sure your scanner software appends a hard return to the scan output. There is a commented line to move one cell down if you ever want to embrace that method.



来源:https://stackoverflow.com/questions/27085262/scan-multiple-barcodes-into-a-single-excel-cell-each-on-a-separate-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!