Help with VBA code

Dievox22

New Member
Joined
Jun 16, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello, I am trying to set up an automatic case counter with a barcode scanner. I found a tutorial online with a basic one but I can't get it to work I keep getting "Compile error: sub or function not defined"

Here's the code currently:

Sheet1 Received:

Sub receive()
Dim barcode As String
Dim rng As Range
Dim rownumber, count As Long
Dim rec As Long

barcode = Sheet3.Cells(2, 2)
count = Sheet3.Cells(2, 1).Value
Sheet1.Activate

If barcode <> "" Then
Set rng = ActiveSheet.Columns("a:a").Find(what:=barcode, _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
searchdirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If rng Is Nothing Then
MsgBox "number not found"
GoTo ende
Else
If count > 1 Then
rng.Offset(0, 3).Value = rng.Offset(0, 3).Value + count
Else
rng.Offset(0, 3).Value = rng.Offset(0, 3).Value + 1

End If
Sheet3.Cells(2, 2) = ""
End If
Sheet3.Cells(2, 1) = ""
End If
ende:

Sheet3.Activate
Sheet3.Cells(2, 2).Select


End Sub

Sheet3 scan-in:

Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Me.Range("B2")) Is Nothing Then

Call receive


Application.EnableEvents = True
End If

End Sub

If anyone could help me understand what I'm doing wrong, I would appreciate it.
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Place this macro in the code module for Sheet3:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Intersect(Target, Range("B2")) Is Nothing Then Exit Sub
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    If Target <> "" Then
        Set rng = ActiveSheet.Range("A:A").Find(Target.Value, LookIn:=xlValues, lookat:=xlWhole)
        If Not rng Is Nothing Then
            If Range("A2") > 1 Then
                rng.Offset(0, 3).Value = rng.Offset(0, 3).Value + Range("A2").Value
            Else
                rng.Offset(0, 3).Value = rng.Offset(0, 3).Value + 1
            End If
            Target.ClearContents
        Else
            MsgBox "Number not found."
        End If
        Range("A2").ClearContents
        Target.Select
    End If
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Place this macro in the code module for Sheet3:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Intersect(Target, Range("B2")) Is Nothing Then Exit Sub
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    If Target <> "" Then
        Set rng = ActiveSheet.Range("A:A").Find(Target.Value, LookIn:=xlValues, lookat:=xlWhole)
        If Not rng Is Nothing Then
            If Range("A2") > 1 Then
                rng.Offset(0, 3).Value = rng.Offset(0, 3).Value + Range("A2").Value
            Else
                rng.Offset(0, 3).Value = rng.Offset(0, 3).Value + 1
            End If
            Target.ClearContents
        Else
            MsgBox "Number not found."
        End If
        Range("A2").ClearContents
        Target.Select
    End If
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
Should I replace the code I already have in Sheet1 or just add this to it? Sorry, I am completely new to this
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top