+ Reply to Thread
Results 1 to 7 of 7

Data Validation Using Target.Value

Hybrid View

  1. #1
    Registered User
    Join Date
    02-20-2012
    Location
    Austin
    MS-Off Ver
    Excel 2010
    Posts
    28

    Data Validation Using Target.Value

    I am trying to get a data validation using vba. I can not seem to get the below code correct. I have posted part of the code below. I am wanting use the Match function to look at the Target Value. I am having trouble on this line "CHOOSE(MATCH(Target.Value,Categories,0),Column1,Column2)" The code language on Target.Value reads an error.
    An example would be if cell A1 reads the word Red then add the data validation list Column1. Red is the first index in the Categories list.

                        With Target.Offset(0, 1).Validation
                            .Delete
                            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
                            xlBetween, Formula1:="=CHOOSE(MATCH(Target.Value,Categories,0),Column1,Column2)"
                            .IgnoreBlank = True
                            .InCellDropdown = True
                            .InputTitle = ""
                            .ErrorTitle = "Cell Validated"
                            .InputMessage = ""
                            .ErrorMessage = "Cell validated.Select from dropdown list."
                            .ShowInput = True
                            .ShowError = True
                        End With

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,485

    Re: Data Validation Using Target.Value

    You may have to supply a sample workbook so somebody can see what the problem is.

  3. #3
    Registered User
    Join Date
    02-20-2012
    Location
    Austin
    MS-Off Ver
    Excel 2010
    Posts
    28

    Re: Data Validation Using Target.Value

    I have attached an example program. Just a note where it says Formula1:="=CHOOSE(MATCH(A2,Categories,0),Column1,Column2)" the A2 should read the target.value which is A2. When the program runs it should look at the value in Column A within the same row. Example if A15 has a value the formula would read Formula1:="=CHOOSE(MATCH(15,Categories,0),Column1,Column2)"

    Program is also here to.

    Private Sub Worksheet_Change(ByVal Target As Range)
      If Not Intersect(Target, Range("A2:A500")) Is Nothing Then
          Call ColumnG(Intersect(Target, Range("A2:A500")))
       End If
    End Sub
    Sub ColumnG(Target As Range)
    
                        With Target.Offset(0, 1).Validation
                            .Delete
                            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
                            xlBetween, Formula1:="=CHOOSE(MATCH(A2,Categories,0),Column1,Column2)"
                            .IgnoreBlank = True
                            .InCellDropdown = True
                            .InputTitle = ""
                            .ErrorTitle = "Cell Validated"
                            .InputMessage = ""
                            .ErrorMessage = "Cell validated.Select from dropdown list."
                            .ShowInput = True
                            .ShowError = True
                        End With
    
    End Sub
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    02-20-2012
    Location
    Austin
    MS-Off Ver
    Excel 2010
    Posts
    28

    Re: Data Validation Using Target.Value

    Any suggestions?

  5. #5
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Data Validation Using Target.Value

    hi nalgene5622, try this

    Sub ColumnG(Target As Range)
    
        With Target.Offset(0, 1).Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
            xlBetween, Formula1:="=CHOOSE(MATCH(" & Target.Address & ",Categories,0),Column1,Column2)"
            .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = "Cell Validated"
            .InputMessage = ""
            .ErrorMessage = "Cell validated.Select from dropdown list."
            .ShowInput = True
            .ShowError = True
        End With
        
        'clears cell in B column
        Target.Offset(0, 1).ClearContents
    
    End Sub

  6. #6
    Registered User
    Join Date
    02-20-2012
    Location
    Austin
    MS-Off Ver
    Excel 2010
    Posts
    28

    Re: Data Validation Using Target.Value

    Watersev,

    Thanks for the help that is exactly what I needed.

  7. #7
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Data Validation Using Target.Value

    if your issue is Solved: Select Thread Tools (on top of your 1st post) -> Mark this thread as Solved.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1