Data Validation and VBA

zico8

Board Regular
Joined
Jul 13, 2015
Messages
225
Hi,
I have one cell with Data Validation - list that allow choose only values "AAA", "BBB", "CCC".
The value can by typed or scanned from barcodes. Scanning works rather like pasting so excel does not display alert if value is "DDD".

This is OK for me but I do not want to input data if scanned barcode will be different then 3 char length.

Below code does the job, but the problem is during typing instead scanning. Due to data validation checking, excel remove typed value so my msgbox is executed twice.

How to handle this?

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, cells(1,1)) Is Nothing Then        
If Len(Target.Value) <> 3 Then
            MsgBox "Incorrect value!"
            Application.EnableEvents = False
                Target.Value = ""
                Target.Select
            Application.EnableEvents = True
        Exit Sub
        End If
    End If
End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
skip If the cell is not 3...

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
if len(activecell.value)=3 then
   If Not Intersect(Target, cells(1,1)) Is Nothing Then        
     ' ...
   endif
endif
end sub
 
Last edited:
Upvote 0
Thanks ranman256,

I do not think this is correct solution.
If lenght of scanned value will be different than 3 neither macro nor data validation react.
 
Upvote 0

Forum statistics

Threads
1,215,003
Messages
6,122,655
Members
449,091
Latest member
peppernaut

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