Share via

adding data validation using vba

Anonymous
2010-12-31T14:44:30+00:00

hi all

i am trying to add data validation using vba but cant get passed the Formula1 := .

im trying to use a named as the source of the list and it i record the code it seems to work with what looks like a string"=contract"

however i would like the code to be able to change the name. to match the column the code is on. so i thought using a variable

i have tried myform = "=contract" and myform="=" & name.name

 this gives me myform with a value which looks like "=contract" the same as when recorded

leaving me with formula1:=myform

but this errors

Dim myform As String

    myform = "=contract"

    With Selection.Validation

        .Delete

        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _

        xlBetween, Formula1:=myform

        .IgnoreBlank = True

        .InCellDropdown = True

        .InputTitle = ""

        .ErrorTitle = ""

        .InputMessage = ""

        .ErrorMessage = ""

        .ShowInput = True

        .ShowError = True

    End With

End Sub

is there a special way of putting the Equals sign into the variable

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2011-01-01T09:13:15+00:00

Hi Pete

As HansV says, you can not refer to a list in another workbook.

The one way to solve this is to build a list in a variable based on the content of the range in the master workbook. The only thing is that you have to update all validation lists if the list in MasterBook is changed!

Dim Master as Workbook

Dim ListRng As Range

Dim MyList As String

Set Master=Workbooks("MasterBook.xlsm")

Set ListRng = Master.Range("f1:f4")

For Each cell In ListRng

    MyList = MyList & cell.Value & ","

Next

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

 With Selection.Validation

        .Delete

        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _

        xlBetween, Formula1:=MyList

        .IgnoreBlank = True

        .InCellDropdown = True

        .InputTitle = ""

        .ErrorTitle = ""

        .InputMessage = ""

        .ErrorMessage = ""

        .ShowInput = True

        .ShowError = True

    End With

Was this answer helpful?

0 comments No comments

6 additional answers

Sort by: Most helpful
  1. Anonymous
    2010-12-31T21:05:58+00:00

    hi per

    thanks for your response im afraid its more complicated than that. i have responded to Hanv with a bit more detail

    i fear im trying to run befor i can walk

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2010-12-31T21:04:30+00:00

    hi thanks for the response

    i had tried this in isolation so there is a lesson learnt

    maybe i should give you more of the story

    i have a master workbook (new format master.xls) which has a number of lists on a sheet called list

    the code runs from this workbook and as i need to create multiple copies creats a new workbook using

    Set newlog = Workbooks.Add(xlWBATWorksheet)

    it then adds names to the new workbook using

    namescount = Workbooks("new format master.xls").Names.Count

    For namescounter = 1 To namescount

    myname = Workbooks("new format master.xls").Names.Item(namescounter).Name

    newlog.ActiveSheet.Names.Add Name:=myname, RefersTo:="='new format master.xls'!" & myname

    Next namescounter

    it then adds sheets to the new work book renames them and adds headings in row 1

    If ActiveSheet.Previous Is Nothing Then

    Set NewSheet = Workbooks(newlog.Name).Worksheets.Add

    NewSheet.Name = aname

    For Each colhead In columnheadings

    headingcount = headingcount + 1

    NewSheet.Cells(1, headingcount) = columnheadings(headingcount, 1)

    Next colhead

    it is at this point i am trying to add the validation and am wondering if it is because the code is not running in the workbook the names are in anyway ill keep playing but any pointers will be apreciated.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2010-12-31T15:02:51+00:00

    Your code works, if you have a range named 'Contract' in the workbook.

    Look at this:

    Dim myform As String

    Select Case ActiveCell.Column

    Case 1 'Column A'

        myform = "=UserID"

    Case 2

        myform = "=contract"

    Case 3

        myform = "=OtherNamedRange"

    Case Else

        Exit Sub

    End Select

        With Selection.Validation

            .Delete

            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _

            xlBetween, Formula1:=myform

            .IgnoreBlank = True

            .InCellDropdown = True

            .InputTitle = ""

            .ErrorTitle = ""

            .InputMessage = ""

            .ErrorMessage = ""

            .ShowInput = True

            .ShowError = True

        End With

    End Sub

    Was this answer helpful?

    0 comments No comments
  4. HansV 462.6K Reputation points
    2010-12-31T15:00:27+00:00

    I created a range named contract, then ran your code. It correctly added validation to the selected cells, with the contract range used for the dropdown list.

    So check very carefully that you have used the correct name, spelled exactly as defined.

    Was this answer helpful?

    0 comments No comments