title: How to add data validation to a cell using VBA
body: <p>I want to  add "data validation" in a cell(which is variable) using VBA and the range which is to come in the  data validation list is also variable. Till now I have been using this</p>
  
  <p>Here "range1" is the range which is to come in the data validation list and "rng" is the cell where I want the data validation</p>
  
  <pre><code>Dim range1, rng As range
  Set range1 = range("a1:a5")
  Set rng = range("b1")
  With rng
  With .Validation
      .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
      xlBetween, Formula1:="range1"
  End With
  End With
  </code></pre>
  
  <p>I am getting "application defined and object defined error"</p>
  
  <p>Also can someone explain me the meaning of different arguments in</p>
  
  <pre><code>With .Validation
  .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
      xlBetween, Formula1:="range1"
  </code></pre>