VBA If Functions, blank cells, and message box

Chris1025

New Member
Joined
Feb 12, 2024
Messages
16
Office Version
  1. 2016
Platform
  1. Windows
I’m looking to add a message box to an existing if function. If i search something in a range, i have it highlight that result, so long as it returns it. If it doesn’t i would like to add a message box that pops up, like an error. Is this possible? The table range could possibly have some blank cells that i do not want to remove also, so i would like to ignore those if possible.
 
Hi,
try this update to your code & see if does what you want

VBA Code:
Sub Scan_Barcode()
  
    Dim BarCode     As Variant
    Dim rngScan     As Range, FoundBar   As Range
    Dim BarCount    As Long
  
    Set rngScan = Range("B1:I4000")
  
    Do
        BarCode = InputBox("Scan Barcode", "Scan")
      
        If Len(BarCode) > 0 Then
          
            If IsNumeric(BarCode) Then BarCode = Val(BarCode)
          
            BarCount = Application.CountIf(rngScan, BarCode)
          
            If BarCount = 1 Then
              
                Set FoundBar = rngScan.Find(BarCode, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
                If Not FoundBar Is Nothing Then FoundBar.Interior.Color = rgbYellow
              
            ElseIf BarCount > 1 Then
          
                MsgBox "Barcode: " & BarCode & Chr(10) & "There Are " & BarCount & " Duplicates Of This Barcode", 48, "Duplicates"
              
            Else
          
                MsgBox "Barcode: " & BarCode & Chr(10) & "No matching barcode found!", 64, "No Matches"
              
            End If
        End If
      
    ''cancel pressed
    Loop Until StrPtr(BarCode) = 0
  
End Sub

Solution is untested but do Note that I have replaced your For Next loop with the Range.Find method which should be much faster searching the specified range HOWEVER - this method can sometimes prove a little troublesome when searching some data types.

Dave
Dave-this worked exactly as needed.

Huge thanks to you and everyone else who helped!
 
Upvote 0

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Hi,
try this update to your code & see if does what you want

VBA Code:
Sub Scan_Barcode()
  
    Dim BarCode     As Variant
    Dim rngScan     As Range, FoundBar   As Range
    Dim BarCount    As Long
  
    Set rngScan = Range("B1:I4000")
  
    Do
        BarCode = InputBox("Scan Barcode", "Scan")
      
        If Len(BarCode) > 0 Then
          
            If IsNumeric(BarCode) Then BarCode = Val(BarCode)
          
            BarCount = Application.CountIf(rngScan, BarCode)
          
            If BarCount = 1 Then
              
                Set FoundBar = rngScan.Find(BarCode, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
                If Not FoundBar Is Nothing Then FoundBar.Interior.Color = rgbYellow
              
            ElseIf BarCount > 1 Then
          
                MsgBox "Barcode: " & BarCode & Chr(10) & "There Are " & BarCount & " Duplicates Of This Barcode", 48, "Duplicates"
              
            Else
          
                MsgBox "Barcode: " & BarCode & Chr(10) & "No matching barcode found!", 64, "No Matches"
              
            End If
        End If
      
    ''cancel pressed
    Loop Until StrPtr(BarCode) = 0
  
End Sub

Solution is untested but do Note that I have replaced your For Next loop with the Range.Find method which should be much faster searching the specified range HOWEVER - this method can sometimes prove a little troublesome when searching some data types.

Dave
Hey Dave,

Would it be possible to add a command to jump to the found cell using the above? Only for the instance when it’s not a duplicate?
 
Upvote 0
Would it be possible to add a command to jump to the found cell using the above? Only for the instance when it’s not a duplicate?

Try adding additional code shown in BOLD & see if does what you want

Rich (BB code):
Set FoundBar = rngScan.Find(BarCode, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
If Not FoundBar Is Nothing Then FoundBar.Interior.Color = rgbYellow: FoundBar.Select

Dave
 
Upvote 0
My data source periodically will have preceding zeros, to which i do NOT want ignored. However the code drops the zeros, even when i have zeros in the input box. Any way to avoid that?
 
Upvote 0
See the image. This is an example of the above. I changed the code to…

If IsNumeric(BarCode) Then BarCode = (BarCode)

From…

If IsNumeric(BarCode) Then BarCode = Val(BarCode)

And it seems to return results as needed. However it’s now recognizing the two below as duplicates. They have the same first 16 digits, but last four are different so they’re unique.
 

Attachments

  • IMG_0419.jpeg
    IMG_0419.jpeg
    26.6 KB · Views: 2
Upvote 0
Try adding additional code shown in BOLD & see if does what you want

Rich (BB code):
Set FoundBar = rngScan.Find(BarCode, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
If Not FoundBar Is Nothing Then FoundBar.Interior.Color = rgbYellow: FoundBar.Select

Dave
hey bud, this worked. would you have any idea on the other minor issue i'm having? Also should i be posting these in different threads? since this post is marked solved?
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,262
Members
452,627
Latest member
KitkatToby

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