Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Further to my comment above which includes a <a href="https://stackoverflow.com/questions/10714251/excel-macro-avoiding-using-select/10718179#10718179">link</a> which talks in detail on avoiding <code>.Select</code>.</p> <p>Here are my <code>three</code> cents.</p> <p><strong>A. Declare your objects/Variables</strong></p> <p>It becomes easier to work when you have declared your Variables/objects. This ensures that you do not have to type repetitive code. For example</p> <pre><code>Range(Cells(2, 2).Address, Cells(5, 5).Address).THIS Range(Cells(2, 2).Address, Cells(5, 5).Address).THAT Range(Cells(2, 2).Address, Cells(5, 5).Address).THIS etc... </code></pre> <p><strong>B. Ensure that you fully qualify your objects and work with them</strong></p> <p>This is the most common cause of errors. Consider this line.</p> <pre><code>Range(Cells(2, 2).Address, Cells(5, 5).Address) </code></pre> <p>Here Excel is assuming that you are working with the current sheet. What if you are not. See this example</p> <pre><code>Sheets(2).Range(Cells(2, 2).Address, Cells(5, 5).Address) </code></pre> <p>Here the <code>Cells()</code> object are not fully qualified and can cause an error. See this <a href="https://stackoverflow.com/questions/14388270/setting-a-chart-source-data-based-on-a-variable-cell-range-in-vba">post</a></p> <p><strong>C. Cut out the extra/repetitive code</strong></p> <p>The Excel constants <code>xlEdgeLeft</code>, <code>xlEdgeTop</code>, <code>xlEdgeBottom</code>, <code>xlEdgeRight</code> etc each is equal to a number and that too in increasing order. If you type it in Immediate window then you can check it's value</p> <pre><code>'~~&gt; This will give you 7 ?xlEdgeLeft </code></pre> <p>So we can actually take advantage of this and shorten out code.</p> <p>See the code below</p> <pre><code>Option Explicit Sub Sample() Dim ws As Worksheet Dim rng As Range Dim i As Long '~~&gt; Change this to the relevant worksheet Set ws = ThisWorkbook.Sheets("Sheet1") With ws Set rng = .Range(.Cells(2, 2).Address, .Cells(5, 5).Address) With rng .Borders(xlDiagonalDown).LineStyle = xlNone .Borders(xlDiagonalUp).LineStyle = xlNone For i = 7 To 10 'xlEdgeLeft = 7 : xlEdgeTop = 8 : xlEdgeBottom = 9 'xlEdgeRight = 10 With .Borders(i) .LineStyle = xlDouble: .Color = -16777216: .Weight = xlThick End With Next For i = 11 To 12 'xlInsideVertical = 11 : xlInsideHorizontal = 12 With .Borders(i) .LineStyle = xlContinuous: _ .ColorIndex = xlAutomatic: .Weight = xlThick End With Next End With End With End Sub </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload