【问题标题】:VBA Splitting the same string multiple timesVBA多次拆分相同的字符串
【发布时间】:2012-04-23 19:21:40
【问题描述】:

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

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 位整数的形式出现,但我需要单独保存这些部分以创建一个简单的数据库查询功能。

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

【问题讨论】:

    标签: string excel split vba


    【解决方案1】:

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

    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
    

    【讨论】:

    • 非常感谢你们!你刚刚救了我!我非常感谢您愿意帮助仍在努力解决问题的人! (餐饮负责人我也尝试过@你,但我不被允许)
    • 很高兴能帮上忙 :) 现在不要复制它 ;) 尝试了解它的工作原理 :)
    • 我是^_^,再次感谢。尽管由于某种原因,我对下一个空白行的搜索现在以某种方式中断了。它一直告诉我“运行时错误'91':对象变量或未设置块”,有什么想法吗?
    • 实际检查 sheet1 是否为空?您应该至少有第一行填充标题。或者,您可以使用此iRow = ws.Range("A" & rows.count).end(xlup).row + 1" 查找下一行。
    • 很高兴帮助迈克。祝你的应用好运。
    猜你喜欢
    • 1970-01-01
    • 2013-05-02
    • 2022-01-15
    • 2016-04-14
    • 1970-01-01
    • 2019-10-19
    • 1970-01-01
    • 2014-09-19
    • 2013-04-14
    相关资源
    最近更新 更多