简体   繁体   中英

Data validation list run macro from VBA

I'm trying to run a macro (for a test just prompt a message box) from a data validation list which is created every time I run a macro which create a new line. The new row is created in row 6, and the data validation list is always in F6. When I add a new line this will offcourse change and move the old one down.

I want to run a new macro when any of the data validation lists are changed in column "F".

Right now I have this code, but it keeps prompting

"Run-time error '13': Type mismatch"

My module creating the validation list looks like this:

With Range("F6").Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
    Operator:=xlBetween, Formula1:="High,Medium,Low"
    .IgnoreBlank = True
    .InCellDropdown = True
    .InputTitle = ""
    .ErrorTitle = ""
    .InputMessage = ""
    .ErrorMessage = ""
    .ShowInput = True
    .ShowError = True
End With

And in sheet1 I have on change the following code:

Sub Worksheet_Change(ByVal Target As Range)

Dim d As Range
Set d = Application.Intersect(Target.Cells(1), Me.Range("F5:F1000"))
    If Not Range("F5:F1000") Is Nothing Then
        Select Case Columns(2)
            Case "High": MsgBox ("Test")
            Case "Medium": MsgBox ("Test")
            Case "Low": MsgBox ("Test")
        End Select
        Else
                    'do nothing
    End If

Can anyone spot the error?

Screenshot

-CP

maybe you're after this:

Sub Worksheet_Change(ByVal Target As Range)
    Dim d As Range

    Set d = Application.Intersect(Target.Cells(1), Range("F5:F1000")) 
    If Not d Is Nothing Then ' <<---check if set range is nothing
        Select Case Target.Value2 ' <<-- check changed cell (i.e. 'Target') value
            Case "High": MsgBox "High" & " in " & Target.Address
            Case "Medium": MsgBox "Medium" & " in " & Target.Address
            Case "Low": MsgBox "Low" & " in " & Target.Address
        End Select
    End If
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM