1

I am trying to create data validation in a cell based on a string as the address. This string is saved from a worksheet.

In this example the strAddress returns $G$3:$G$14.

I get

Object-defined error

when adding the validation as indicated by * in the code.

This is half the routine, as the rest are repetitions of this:

Sub UpdateLists(advertiser As String)

'    On Error Resume Next

    'need to update the other data validation based on the advertiser selected
    Dim strAdvertiser As String, strAddress As String
    Dim adRng As Range
    Dim myRng As Range

    'clear validation first
    Range("I10:I12").Validation.Delete

    strAdvertiser = advertiser

    'now find the relevant supplier in the lists sheet
    Set adRng = Sheets("Lists").Range("A:A").Find(What:=strAdvertiser)

    If Not adRng Is Nothing Then

        'adjust division according to advertiser
        Set myRng = ActiveSheet.Cells.Find(What:="Division")

        If Not myRng Is Nothing Then
            'list range
            strAddress = adRng.Offset(0, 1).Text

            'adjust list
            '**error on below line
            myRng.Offset(0, 1).Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:="='Lists'!" & strAddress

        Else
            MsgBox "Failed to find Division", vbCritical
            Exit Sub
        End If
    End if
End Sub

I have tried:

a) removing the myRng object and using Sheets, but this still returns the error.

b) hard-coding the strAddress.

c) running several Debug.Print on the myRng object to check it's validity.

d) to use .Validation.Modify originally, but then have moved to .Validation.Delete and .Validation.Add.

9
  • @BigBen it is defined in the above post. See: strAddress returns $G$3:$G$14 Commented Jun 11, 2019 at 14:07
  • 1
    What version of Excel are you using? Seems to me that in older versions, you cannot use validation from a range on another worksheet. Commented Jun 11, 2019 at 14:14
  • 3
    @Dean Can you try removing ActiveSheet and fully qualifying that line? Just in case :). Also fully qualify Range("I10:I12").Validation.Delete. Commented Jun 11, 2019 at 14:20
  • 1
    @Dean Either one of those could've been the issue - maybe you were deleting validation on the wrong sheet (so it'd be similar to the problem FunThomas brought up), or you were establishing myRng incorrectly by finding Division on the wrong worksheet. Either way, fully qualifying all your ranges will probably save you many headaches in the future! :) Commented Jun 11, 2019 at 14:30
  • 2
    @Dean I'm going to guess you were deleting validation on the wrong worksheet, so FunThomas was pretty much spot on with his solution - might as well give him the credit :) Commented Jun 11, 2019 at 14:32

1 Answer 1

2

I think you must clear any existing validation before you set the validation to a list.

Try to change the code to

With myRng.Offset(0, 1)
    .Validation.Delete
    .Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:="='Lists'!" & strAddress
End With
Sign up to request clarification or add additional context in comments.

4 Comments

I thought this initially as well, but it looks like he might do that here: Range("I10:I12").Validation.Delete
Yes @dwirony you are correct. My apologies I was not clear about that
@Dean Are you certain that the offset from myRng is always within "I10:I12"? Why not just do myRng.Offset(0, 1).Validation.Delete before you add just in case.
@dwirony yes, Debug.Print myRng.Address returns H10:H12. I added the Validation.Delete in my debugging process.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.