Note that there are some explanatory texts on larger screens.

plurals
  1. POEfficiently assign cell properties from an Excel Range to an array in VBA / VB.NET
    primarykey
    data
    text
    <p>In VBA / VB.NET you can assign Excel range values to an array for faster access / manipulation. Is there a way to efficiently assign other cell properties (e.g., top, left, width, height) to an array? I.e., I'd like to do something like:</p> <pre><code> Dim cellTops As Variant : cellTops = Application.ActiveSheet.UsedRange.Top </code></pre> <p>The code is part of a routine to programmatically check whether an image overlaps cells that are used in a workbook. My current method of iterating over the cells in the UsedRange is slow since it requires repeatedly polling for the top / left / width / height of the cells.</p> <p><strong>Update:</strong> I'm going to go ahead an accept Doug's answer as it does indeed work faster than naive iteration. In the end, I found that a non-naive iteration works faster <em>for my purposes of detecting controls that overlap content-filled cells</em>. The steps are basically:</p> <p>(1) Find the interesting set of rows in the used range by looking at the tops and heights of the first cell in each row (my understanding is that all the cells in the row must have the same top and height, but not left and width)</p> <p>(2) Iterate over the cells in the interesting rows and perform overlap detection using only the left and right positions of the cells.</p> <p>The code for finding the interesting set of rows looks something like:</p> <pre><code>Dim feasible As Range = Nothing For r% = 1 To used.Rows.Count Dim rowTop% = used.Rows(r).Top Dim rowBottom% = rowTop + used.Rows(r).Height If rowTop &lt;= objBottom AndAlso rowBottom &gt;= objTop Then If feasible Is Nothing Then feasible = used.Rows(r) Else feasible = Application.Union(used.Rows(r), feasible) End If ElseIf rowTop &gt; objBottom Then Exit For End If Next r </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.
 

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