title: Range error setting range in VBA using Range(Cells,Cells)
body: <p>I am reworking the code in an older worksheet to update it and make it more efficient.</p>
  
  <p>I have a subroutine that formats a data table but I am getting an error where I dont see why I am getting one. My VBA is a bit rusty but the following code should be working</p>
  
  <pre><code>Sub FormatPnLDataTable(tableRange As Range)
      Dim tempRange As Range, ws As Worksheet
      Dim lStartRow As Long, lEndRow As Long, lLastCol As Long
  
      Set ws = Sheets("PandLDataTable")
      Application.CutCopyMode = False
      lStartRow = tableRange.Row
      lEndRow = lStartRow + tableRange.Rows.Count
      lLastCol = tableRange.Columns.Count
  
      'format the whole table with border
      With tableRange
          'format borders
          'code removed for brevity
      End With
      'set range for the top row of table
      Set tempRange = ws.Range(Cells(lStartRow, 1), Cells(lStartRow, lLastCol))
  </code></pre>
  
  <p>The code is erring on this line</p>
  
  <pre><code> Set tempRange = ws.Range(Cells(lStartRow, 1), Cells(lStartRow, lLastCol))
  </code></pre>
  
  <p>but the worksheet (ws) is valid, and the variables all have their proper values
  for this particular instance, lStartRow is 1, lEndRow is 15 and lLastCol is 35</p>