VBA_change array to Range

SP321

New Member
Joined
Jan 27, 2023
Messages
12
Office Version
  1. 2016
Platform
  1. Windows
Hi,

can anyone please update the code below? It is for dropdown menu, which cannot be overwritten with copy+paste.
This works perfectly, but sometimes the list is too long (over 255 characters) and once I close and open the file it is showing errors. I found the solution and it needs to be done through a range.

No matter how I try to change the code I get Syntax errors. Since I am complete beginner I can't figure out how to refer to a list in name manager.
Here is the code:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim isect As Range

Dim cell As Range

Dim dd As Variant

Dim i As Long

Dim mtch As Boolean

Dim msg As String

Dim myEntries As String

Set isect = Intersect(Range("B7:B600"), Target)

If Not isect Is Nothing Then

Application.EnableEvents = False

dd = Array("A", "B", "C", "D", "")

For Each cell In isect

mtch = False

For i = LBound(dd) To UBound(dd)

If cell.Value = dd(i) Then

mtch = True

Exit For

End If

Next i

If mtch = False Then

cell.ClearContents

msg = msg & cell.Address(0, 0) & ","

End If

Next cell

For i = LBound(dd) To UBound(dd)

myEntries = myEntries & dd(i) & ","

Next i

myEntries = Left(myEntries, Len(myEntries) - 1)

With Range("B7:B600").Validation

.Delete

.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=myEntries

' .IgnoreBlank = True

' .InCellDropdown = True

' .InputTitle = ""

' .ErrorTitle = ""

' .InputMessage = ""

' .ErrorMessage = ""

' .ShowInput = True

' .ShowError = True

End With

If Len(msg) > 0 Then

MsgBox "Invalid data in cells: " & vbCrLf & Left(msg, Len(msg) - 1), vbOKOnly, "Error"

End If

End If

Application.EnableEvents = True

End Sub



Can anyone please help? Thank you very much.
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Just tested the macro below with no problem
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim isect As Range
Dim c As Range
Dim dd As Variant
Dim i As Long
Dim mtch As Boolean
Dim msg As String
Dim myEntries As String

Set isect = Intersect(Range("B7:B600"), Target)
If isect Is Nothing Then Exit Sub

        Application.EnableEvents = False
        dd = Array("A", "B", "C", "D", "")
            For Each c In isect
              mtch = False
                For i = LBound(dd) To UBound(dd)
                    If c.Value = dd(i) Then
                        mtch = True
                        Exit For
                    End If
                Next i
            
                If mtch = False Then
                    c.ClearContents
                    msg = msg & c.Address(0, 0) & ","
                End If
            Next c
        For i = LBound(dd) To UBound(dd)
            myEntries = myEntries & dd(i) & ","
        Next i
        
        myEntries = Left(myEntries, Len(myEntries) - 1)
        
            With Range("B7:B600").Validation
                .Delete
                .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
                xlBetween, Formula1:=myEntries
            End With
        
        If Len(msg) > 0 Then
            MsgBox "Invalid data in cells: " & vbCrLf & Left(msg, Len(msg) - 1), vbOKOnly, "Error"
        End If

    Application.EnableEvents = True

End Sub
 
Upvote 0
Just tested the macro below with no problem
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim isect As Range
Dim c As Range
Dim dd As Variant
Dim i As Long
Dim mtch As Boolean
Dim msg As String
Dim myEntries As String

Set isect = Intersect(Range("B7:B600"), Target)
If isect Is Nothing Then Exit Sub

        Application.EnableEvents = False
        dd = Array("A", "B", "C", "D", "")
            For Each c In isect
              mtch = False
                For i = LBound(dd) To UBound(dd)
                    If c.Value = dd(i) Then
                        mtch = True
                        Exit For
                    End If
                Next i
           
                If mtch = False Then
                    c.ClearContents
                    msg = msg & c.Address(0, 0) & ","
                End If
            Next c
        For i = LBound(dd) To UBound(dd)
            myEntries = myEntries & dd(i) & ","
        Next i
       
        myEntries = Left(myEntries, Len(myEntries) - 1)
       
            With Range("B7:B600").Validation
                .Delete
                .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
                xlBetween, Formula1:=myEntries
            End With
       
        If Len(msg) > 0 Then
            MsgBox "Invalid data in cells: " & vbCrLf & Left(msg, Len(msg) - 1), vbOKOnly, "Error"
        End If

    Application.EnableEvents = True

End Sub
Hi, yes this code works fine, but I have e.g. countries for dropdown list which exceeds 255 characters, then it doesn't work. I would like to replace dd=array with range, but no matter what I do I get syntax errors
 
Upvote 0
OK ...
Most probably your actual validation list of countries is readily available and has a Name visible in the Name Manager (Ctrl+F3) ....
So you could use in your code Range("MyList") ....
 
Upvote 0
OK ...
Most probably your actual validation list of countries is readily available and has a Name visible in the Name Manager (Ctrl+F3) ....
So you could use in your code Range("MyList") ....
yes it is in name manager, if it is not too much bother could you please show me how the code would look? maybe I find the mistake I make
 
Upvote 0
Since you are using MyEntries for your Validation, you could test:
Dim MyEntries As Range
Set MyEntries = Range("MyList")
 
Upvote 0
Since you are using MyEntries for your Validation, you could test:
Dim MyEntries As Range
Set MyEntries = Range("MyList")
it doesn't work for me :( but as I said Im really bad, so I think Im making mistake somewhere
 
Upvote 0
OK ...
Below is your test Event macro for a starting point
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
' Adds in the Range B7:B600 a Data Validation built with
' your List defined with the Named Range myEntries

If Intersect(Range("B7:B600"), Target) Is Nothing Then Exit Sub
    Application.EnableEvents = False
            With Range("B7:B600").Validation
                .Delete
                .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
                xlBetween, Formula1:="=myEntries"
            End With
    Application.EnableEvents = True
End Sub

Hope this will help
 
Upvote 0
Hello,

Sorry, but I forgot to mention that, for your Data Validation to be working effectively, you do need, beforehand, to add a Named Range within your worksheet, for your list of countries, and you do need to give it the same Name as the one you are using in your macro .... in this case myEntries

Hope this clarifies :)
 
Upvote 0
Hello,

Sorry, but I forgot to mention that, for your Data Validation to be working effectively, you do need, beforehand, to add a Named Range within your worksheet, for your list of countries, and you do need to give it the same Name as the one you are using in your macro .... in this case myEntries

Hope this clarifies :)
Hi yes, I figured that one out, thank you :D
This works good, now I'm trying to mix it with the full code to also forbid pasting wrong data
 
Upvote 0

Forum statistics

Threads
1,214,945
Messages
6,122,395
Members
449,081
Latest member
JAMES KECULAH

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