Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Call DataValidationManagement(Target)
End Sub
And here’s the DataValidationManagement()
Sub DataValidationManagement(ByVal Target As Range)
'The below called from "change" events associated with Data Validation Lists
'Original Concept for Data Validation/ComboBox drop down from http://www.contextures.com/xldataval10.html
Dim str As String
Dim cboTemp As OLEObject
Dim WS As Worksheet
Dim vType As Variant
Dim chkDVList As Variant
Dim proceedSetup As Boolean
Set WS = ActiveSheet
On Error Resume Next 'connect to temporary ComboBox "TempCombo", testing along the way using Err.Number
Set cboTemp = ActiveSheet.OLEObjects("TempCombo")
If Err.Number <> 0 Then 'the ComboBox object must have been inadvertently deleted, so let's create it
Set cboTemp = ActiveSheet.OLEObjects.Add(classtype:="Forms.ComboBox.1")
cboTemp.Name = "TempCombo"
End If
proceedSetup = False
vType = Target.Validation.Type 'Empty if there is no validation type at Target address
If Left(Target.Validation.Formula1, 1) = "=" Then 'check to see if there's a validation reference to a range address
proceedSetup = True
End If
On Error GoTo 0
On Error GoTo errHandler 'fall to bottom on any errors
If Not IsEmpty(vType) And vType = 3 And proceedSetup Then 'Target.Validation.Type = 3 Then 'if the cell contains a data validation list
Application.EnableEvents = False
'get the data validation formula
str = Target.Validation.Formula1
chkDVList = Evaluate(str)
str = Right(str, Len(str) - 1)
If Not IsError(chkDVList) Then 'must be a range reference in the Data Validation list; else just use regular data validation, then
With cboTemp
'show the combobox with the list
.Visible = True
.Left = Target.Left
.Top = Target.Top
.Width = Target.Width + 5
.Height = Target.Height + 5
.ListFillRange = "'" & Evaluate(str).Parent.Name & "'!" & Evaluate(str).Address ' modded
.LinkedCell = Target.Address
End With
Call loadMyListObjectUnique(cboTemp, True, False)
Else
'do nothing
End If
Else
With cboTemp
'hide the combobox, and get it out of the way from inadvertent deletion
If .Visible = True Then
.Visible = False
.Left = Range("BB5000").Left
.Top = Range("BB5000").Top
End If
End With
End If
errHandler: 'attempt to re-initialize application-level events on error
'Call InitalizeAppEvents 'not needed if this is called from regular events
Application.EnableEvents = True
Exit Sub
End Sub
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (14)
Author
Commented:Well, I'm finally off my DV list / ListObjects "kick". I've used this particular set of tips quite a few times in the past months with posted questions, and now I can finally have a place to point to without needing to restate directions. Thanks to participating on E-E, I've developed a few other tools in other dimensions, I hope to share, in the coming months...
PS - For more on setting up Application-level events, go to Chip Pearson's site (where I learned the tips): http://www.cpearson.com/excel/appevent.aspx
Cheers,
Dave
Commented:
Voted Yes at the top of the article.
Author
Commented:Author
Commented:Enhancements include:
1. More control as you navigate your spreadsheet. I add a comboBox event handler to support TAB, SHIFT-TAB, ENTER, SHIFT-ENTER, SHIFT-UP arrow, and SHIFT-DOWN arrow navigation, just like with normal Excel spreadsheet handling. The app won't STOP on a data validation range when navigating over one, unless YOU stop.
2. AutoFill bug fixed, now you can start typing and see the text autofill.
3. If you're editing a word in DynamicDV! and hit ESC, the prior value reverts to the cell
4. Added Data Validation checking (yes, it needed it;) with a prompt if the value entered is not in the list (careful, if you DV list includes a blank cell, it will never alert, even with normal DV!)
5. Note, when you are backspacing on a word in the combo box, the cells are highlighted which is a difference from an Excel normal cell or DV cell. I haven't modified this "feature", as you can just hit the delete key when you want the word to be truncated.
I also added an auto install function for the add-in. With Excel already started, just download the new add-in version 2.0 and run it, after a couple prompts, it should be installed.
There is a revised Add-in menu for DynamicDV! that is fairly self explanatory. You can turn it on/off for your current Excel session, and you can toggle the startup state (e.g., when you start Excel whether Dynamic DV is automatically on or off.) Finally, I also added a ToggleValidationAlert to turn on/off the validation "feature".
Download Attachment II v2.0 to use the latest.
Cheers,
Dave
Commented:
I tried to Use it in Excel 2019. Got this ERROR message.
View More