title: Macro (VBA) in Excel to add borders and merge cells if the cells are not empty
body: <p>I recorded the following Macro:</p>
  
  <pre><code>Sub Macro1()
  Range("E66:F68").Select
  Selection.Borders(xlDiagonalDown).LineStyle = xlNone
  Selection.Borders(xlDiagonalUp).LineStyle = xlNone
  With Selection.Borders(xlEdgeLeft)
      .LineStyle = xlContinuous
      .ColorIndex = 0
      .TintAndShade = 0
      .Weight = xlThin
  End With
  With Selection.Borders(xlEdgeTop)
      .LineStyle = xlContinuous
      .ColorIndex = 0
      .TintAndShade = 0
      .Weight = xlThin
  End With
  With Selection.Borders(xlEdgeBottom)
      .LineStyle = xlContinuous
      .ColorIndex = 0
      .TintAndShade = 0
      .Weight = xlThin
  End With
  With Selection.Borders(xlEdgeRight)
      .LineStyle = xlContinuous
      .ColorIndex = 0
      .TintAndShade = 0
      .Weight = xlThin
  End With
  Selection.Borders(xlInsideVertical).LineStyle = xlNone
  Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
  Range("D66:D68,C66:C68,B66:B68,A66:A68").Select
  Range("A66").Activate
  With Selection
      .HorizontalAlignment = xlCenter
      .VerticalAlignment = xlBottom
      .WrapText = False
      .Orientation = 0
      .AddIndent = False
      .IndentLevel = 0
      .ShrinkToFit = False
      .ReadingOrder = xlContext
      .MergeCells = False
  End With
  Selection.Merge
  With Selection
      .HorizontalAlignment = xlCenter
      .VerticalAlignment = xlCenter
      .WrapText = False
      .Orientation = 0
      .AddIndent = False
      .IndentLevel = 0
      .ShrinkToFit = False
      .ReadingOrder = xlContext
  End With
  Selection.Borders(xlDiagonalDown).LineStyle = xlNone
  Selection.Borders(xlDiagonalUp).LineStyle = xlNone
  With Selection.Borders(xlEdgeLeft)
      .LineStyle = xlContinuous
      .ColorIndex = 0
      .TintAndShade = 0
      .Weight = xlThin
  End With
  With Selection.Borders(xlEdgeTop)
      .LineStyle = xlContinuous
      .ColorIndex = 0
      .TintAndShade = 0
      .Weight = xlThin
  End With
  With Selection.Borders(xlEdgeBottom)
      .LineStyle = xlContinuous
      .ColorIndex = 0
      .TintAndShade = 0
      .Weight = xlThin
  End With
  With Selection.Borders(xlEdgeRight)
      .LineStyle = xlContinuous
      .ColorIndex = 0
      .TintAndShade = 0
      .Weight = xlThin
  End With
  Selection.Borders(xlInsideVertical).LineStyle = xlNone
  Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
  Range("G73").Select
  End Sub
  </code></pre>
  
  <p>Now, this was recorded for the range starting at E66, and it basically adds borders for the cells selected and merges the cell rows in the adjacent columns. What I'd like to do is add a condition that looks at column E and starts the macro on the first non-empty cell that has no borders and ends it on the last non-empty cell. In the macro I recorded, the first unbordered non-empty cell was E66 (meaning that the cells in the range E1:E65 had all borders on at least one side), and the last non-empty cell was E68 (the range on the second line is E66:F68 because I used outside borders for the rectangle of cells from E66 to F68, but the condition only needs to be verified for column E).</p>
  
  <p>In other words, I need some sort of loop that goes from E1 to E<em>x</em>, and when it finds a cell that is both non-empty <strong>and</strong> unbordered, it stores that cell number as the starting cell (say E<em>y</em>). Then, when it finds an empty cell (say E<em>z</em>), the loop stops and the cell before E<em>z</em> (so E<em>z-1</em>) is stored as the last one. Then the macro that I recorded should run in the range E<em>y</em>:F<em>z-1</em>.</p>
  
  <p>How can I do this? Thanks.</p>