title: Apply borders in a used cell Range VBA
body: <p>I am trying to apply <strong>a border around a group of used cells dynamically</strong>. The <strong>column Range is (B7:E7)</strong> The number of <strong>rows</strong> will always <strong>vary</strong>, so the code needs to be dynamic. My code below is not achieving this:</p>
  
  <pre><code>Sub Borders()
  
  Application.ScreenUpdating = False
  Dim lngLstCol As Long, lngLstRow As Long
  
  lngLstRow = ActiveSheet.UsedRange.Rows.Count
  lngLstCol = ActiveSheet.UsedRange.Columns.Count
  
  For Each rngCell In Range("B7:B" &amp; lngLstRow)
      If rngCell.Value &gt; "" Then
          r = rngCell.row
          c = rngCell.Column
          Range(Cells(r, c), Cells(r, lngLstCol)).Select
              With Selection.Borders
                  .LineStyle = xlContinuous
                  .Weight = xlThin
                  .ColorIndex = xlAutomatic
              End With
      End If
  Next
  
  Application.ScreenUpdating = True
  
  End Sub
  </code></pre>