2012-04-23 22 views
1

私はインターネットを検索していましたが、私の問題を把握していないようです。どんな助けでも大歓迎です。次のように私のコードは次のとおりです。VBA同じ文字列を複数回に分割する

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桁の整数になりますが、簡単に作成するためには、データベースのルックアップ機能。

ご協力いただきありがとうございます。

答えて

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 
+0

両方ともSOOOありがとうございます!あなたは私を救った!私は本当に物事を把握しようとしている人を助けるあなたの意欲に本当に感謝しています! (ケータリングの責任者は私もこれに@しようとしましたが、許可されていませんでした) –

+0

助けてくれることを嬉しく思います:)それをコピーしないでください:)それがどう働くかを試してみてください:) –

+0

私は^ _です。 ^、もう一度ありがとう。何らかの理由で次の空白行の検索が途中で中断されましたが。それは私に "Run Time Error '91':Object VariableまたはWith block not set"というアイデアを教えてくれますか? –

関連する問題