I am new to VB .. I have a great vb code that will validate barcode check digit when I post the code in the worksheet, but I need it to work inside of an userform. The code is ....
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range
Dim cell As Range
Dim s As String
Dim i As Long
Dim iSum As Long
Set r = Intersect(Target, Columns("L:N"))
If r Is Nothing Then Exit Sub
On Error GoTo Oops
Application.EnableEvents = False
For Each cell In r
With cell
s = Replace(.Text, " ", "")
If Not IsNumeric(s) Then
.Interior.ColorIndex = 0
Else
Select Case Len(s)
Case 8
.Value = Format(Val(s), "0000 0000")
.Interior.ColorIndex = xlColorIndexNone
Case 12
.Value = Format(Val(s), "000000 000000")
.Interior.ColorIndex = xlColorIndexNone
Case 13
.Value = Format(Val(s), "0 000000 000000")
.Interior.ColorIndex = xlColorIndexNone
Case 14
.Value = Format(Val(s), "0 00 00000 000000")
.Interior.ColorIndex = xlColorIndexNone
Case Else
.Interior.ColorIndex = 3
End Select
If .Interior.ColorIndex = xlColorIndexNone Then
iSum = 0
For i = 1 To Len(s) - 1
iSum = iSum + Val(Mid(s, i, 1)) * IIf(i And 1, 3, 1)
Next i 'formatting in the code.
iSum = WorksheetFunction.Ceiling(iSum, 10) - iSum
If Val(Right(s, 1)) <> iSum Then .Interior.ColorIndex = 3
If Val(Right(s, 1)) <> iSum Then MsgBox ("Check Digit Failed. Retry"), vbRetryCancel
End If
End If
End With
Next cell
Oops:
Application.EnableEvents = True
End Sub
How can I rearrange this code to work inside of userform .
Thanks for any help
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range
Dim cell As Range
Dim s As String
Dim i As Long
Dim iSum As Long
Set r = Intersect(Target, Columns("L:N"))
If r Is Nothing Then Exit Sub
On Error GoTo Oops
Application.EnableEvents = False
For Each cell In r
With cell
s = Replace(.Text, " ", "")
If Not IsNumeric(s) Then
.Interior.ColorIndex = 0
Else
Select Case Len(s)
Case 8
.Value = Format(Val(s), "0000 0000")
.Interior.ColorIndex = xlColorIndexNone
Case 12
.Value = Format(Val(s), "000000 000000")
.Interior.ColorIndex = xlColorIndexNone
Case 13
.Value = Format(Val(s), "0 000000 000000")
.Interior.ColorIndex = xlColorIndexNone
Case 14
.Value = Format(Val(s), "0 00 00000 000000")
.Interior.ColorIndex = xlColorIndexNone
Case Else
.Interior.ColorIndex = 3
End Select
If .Interior.ColorIndex = xlColorIndexNone Then
iSum = 0
For i = 1 To Len(s) - 1
iSum = iSum + Val(Mid(s, i, 1)) * IIf(i And 1, 3, 1)
Next i 'formatting in the code.
iSum = WorksheetFunction.Ceiling(iSum, 10) - iSum
If Val(Right(s, 1)) <> iSum Then .Interior.ColorIndex = 3
If Val(Right(s, 1)) <> iSum Then MsgBox ("Check Digit Failed. Retry"), vbRetryCancel
End If
End If
End With
Next cell
Oops:
Application.EnableEvents = True
End Sub
How can I rearrange this code to work inside of userform .
Thanks for any help