如何在Excel VBA中仅增加序列号字符串的最后一位数字?

时间:2014-02-10 14:02:59

标签: excel vba excel-vba serialization

我是vba的新手并尝试编写条形码扫描程序算法。它现在工作正常,但我想在我的exel表中保留序列号,它们的结构必须像“A151000”。我希望每个条形码输入一个带有输入框的单元格也可以分配给一个序列号。例如,当列C中写入的新条目(条形码)我希望在B列序列号中最后一位数增加1并自动存储在该单元格中。 现在我可以从角落拖动单元格,exel增加最后一位数字。如何自动触发新条目?提前谢谢。

A151000  
A151001  
A151002  
...


Sub DataInput()

Dim SearchTarget As String
Dim myRow As Long
Dim Rng As Range


Static PrevCell As Range
Dim FoundCell As Range
Dim CurCell As Range
Dim a As String
Dim Target As Range
Dim buttonclick As Boolean
V = True

If PrevCell Is Nothing Then
    myRow = Selection.Row
    Set PrevCell = Range("C" & myRow)
End If

Set Rng = Range("C:C,C:C") 'Columns for search defined here

With Rng
    Set FoundCell = .Cells.Find(What:=SearchTarget, _
        After:=PrevCell, LookIn:=xlFormulas, LookAt:= _
        xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
        MatchCase:=True)
End With

Dim Eingabezahl As String

Do While Eingabezahl! = ""
    Eingabezahl = InputBox("Last barcode scanned" & "  " & Eingabezahl)
    Range("C" & Range("C" & Rows.Count).End(xlUp).Row + 1) = Eingabezahl
    Range("D" & Range("D" & Rows.Count).End(xlUp).Row + 1) = Now()
    Range("E" & Range("E" & Rows.Count).End(xlUp).Row + 1) = "VALID"
Loop 

End Sub

1 个答案:

答案 0 :(得分:1)

要使用自动填充功能而不更改原始代码,您只需在子文件中添加自动填充功能:

Dim Eingabezahl As String
Dim rngLastBCell As Range
Do While Eingabezahl = ""
    Eingabezahl = InputBox("Last barcode scanned" & "  " & Eingabezahl)

    Set rngLastBCell = Range("B" & Rows.Count).End(xlUp)
    rngLastBCell.AutoFill Destination:=Range(rngLastBCell, rngLastBCell.Offset(1)), Type:=xlFillDefault
    Range("C" & Range("C" & Rows.Count).End(xlUp).Row + 1) = Eingabezahl
    Range("D" & Range("D" & Rows.Count).End(xlUp).Row + 1) = Now()
    Range("E" & Range("E" & Rows.Count).End(xlUp).Row + 1) = "VALID"
Loop

或者您可以使用以下使用相同概念(自动填充)但将您对工作表的调用压缩为一行:

Dim Eingabezahl As String
Dim rngLastBCell As Range
Do While Eingabezahl = ""
    Eingabezahl = InputBox("Last barcode scanned" & "  " & Eingabezahl)

    Set rngLastBCell = Range("B" & Rows.Count).End(xlUp)
    rngLastBCell.AutoFill Destination:=Range(rngLastBCell, rngLastBCell.Offset(1)), Type:=xlFillDefault
    rngLastBCell.Offset(1, 1).Resize(, 3) = Array(Eingabezahl, Now(), "VALID")
Loop

虽然我建议只使用将当前行附加到序列的末尾,而不是使用数组调用工作表的次数:

Dim rngB As Range
Dim Eingabezahl As String
Dim SerialBase As String
SerialBase = "A15100"

Do While Eingabezahl = ""
    Eingabezahl = InputBox("Last barcode scanned" & "  " & Eingabezahl)

    Set rngB = Range("B" & Rows.Count).End(xlUp).Offset(1)
    rngB.Resize(, 4).Value = Array(SerialBase & rngB.Row, Eingabezahl, Now(), "VALID")
 Loop