1

我一直在搜索互联网,我似乎无法弄清楚我的问题。任何帮助将非常感激。我的代码如下:

Private Sub removebutton_Click()
Dim iRow As Long
Dim ws As Worksheet
Dim removebox As String
Set ws = Worksheets("Sheet2")
removebox = InputBox("Please Scan the Barcode to be added", "Add Coin", "Scan Barcode Here")
'Find the next blank row'
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1

'Promt User To Actually input data if they have not entered anything'
If Trim(Me.removebox.Value) = "" Then
Me.removebox.SetFocus
MsgBox "Please enter barcode"
Exit Sub
End If

'For 20 digit barcodes split into 4 parts and record the data in the next blank row'
If Len(removebox) = 20 Then
Dim s1 As String
s1 = removebox.Substring(1, 6)
Dim s2 As String
s2 = removebox.Substring(7, 8)
Dim s3 As String
s3 = removebox.Substring(9)
Dim s4 As String
s4 = removebox.Substring(10, 20)

ws.Cells(iRow, 1).Value = Me.s1.Value
ws.Cells(iRow, 2).Value = Me.s2.Value
ws.Cells(iRow, 3).Value = Me.s3.Value
ws.Cells(iRow, 4).Value = Me.s4.Value

'For 18 Digit barcodes spilt into 3 parts and record the data in the next blank row'
ElseIf (removebox) = 18 Then
Dim s5 As String
s5 = removebox.Substring(1, 6)
Dim s6 As String
s6 = removebox.Substring(7, 8)
Dim s7 As String
s7 = removebox.Substring(9, 18)

ws.Cells(iRow, 1).Value = Me.s5.Value
ws.Cells(iRow, 2).Value = Me.s6.Value
ws.Cells(iRow, 3).Value = Me.s7.Value

'If not 20 or 18 digit then it is 16, split into 3 parts and record the data in the next blank row'
Else
Dim s8 As String
s8 = removebox.Substring(1, 6)
Dim s9 As String
s9 = removebox.Substring(7, 8)
Dim s10 As String
s10 = removebox.Substring(9, 16)

ws.Cells(iRow, 1).Value = Me.s8.Value
ws.Cells(iRow, 2).Value = Me.s9.Value
ws.Cells(iRow, 3).Value = Me.s10.Value
End If

End Sub

所以我搞砸了,整个代码应该是删除一个项目,而不是添加一个我意识到的项目。我的问题是s1 = removebox.Substring(1, 6)突出显示removebox变量并告诉我Compiler Error: Invalid Qualifier。如果有人能给我一个可能的解决方案,我知道我做错了,但是当您扫描条形码时,它以 20、18 或 16 位整数的形式出现,但我需要单独保存这些部分以便创建一个简单的数据库查询功能。

提前感谢您的任何帮助或建议。

4

1 回答 1

0

餐饮部主管打败了我。但是我会粘贴代码,因为我已经在处理它了。

以 20、18 或 16 位整数形式出现的条形码

注意:基于上述,我没有将它分成 4 个相等的部分。我只是转换了您现有的代码。

Option Explicit

Private Sub removebutton_Click()
    Dim iRow As Long
    Dim ws As Worksheet
    Dim removebox As String

    Set ws = Worksheets("Sheet2")

    removebox = InputBox("Please Scan the Barcode to be added", _
    "Add Coin", "Scan Barcode Here")

    '~~> Prompt User To Actually input data if they have not entered anything
    If Len(Trim(removebox)) = 0 Then
        MsgBox "Please enter barcode", vbCritical, "Please Try again"
        Exit Sub
    End If

    '~~> Find the next blank row
    iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1

    ws.Cells(iRow, 1).Value = Left(removebox, 6)
    ws.Cells(iRow, 2).Value = Mid(removebox, 7, 8)

    Select Case Len(removebox)
        '~~> For 20 digit barcodes split into 4 parts and record
        '~~> the data in the next blank row'
        Case 20
            ws.Cells(iRow, 3).Value = Mid(removebox, 9)
            ws.Cells(iRow, 4).Value = Mid(removebox, 10, 20)
        '~~> For 18 Digit barcodes spilt into 3 parts and record
        '~~> the data in the next blank row'
        Case 18
            ws.Cells(iRow, 3).Value = Mid(removebox, 9, 18)
        '~~> If not 20 or 18 digit then it is 16, split into 3
        '~~> parts and record the data in the next blank row'
        Case Else
            ws.Cells(iRow, 3).Value = Mid(removebox, 9, 18)
    End Select
End Sub
于 2012-04-23T20:02:04.213 回答