title: Set range and define named range in VBA
body: <p>I am trying to define a named range (with Workbook scope) in VBA based on certain inputs to help figure out the cells that should be included in the range. To make it really simple, I have tried the following two with the errors indicated below. A, B, X, Y are found from some calculations</p>
  
  <pre><code>rName = &lt;some string that decides the name&gt;
  Set TempRng = .Range(.Cells(A,B), .Cells(X,Y))
  ActiveWorkbook.Names.Add Name:=rName, RefersTo:=Worksheets("Sheet1").TempRng
  </code></pre>
  
  <p>This gives me an "object or method not supported" error or something like that, on the Set TempRng line.</p>
  
  <pre><code>rName = &lt;string ...&gt;
  Set TempRng = Worksheets("Sheet1").Range(Cells(A,B), Cells(X,Y))
  ActiveWorkbook.Names.Add Name:=rName, RefersTo:= &lt;blah blah...&gt;
  </code></pre>
  
  <p>This gives me an "application defined or object defined error" on the same line.</p>
  
  <p>Can you please help me find the correct way to define this range?</p>