Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 963333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:39:17+00:00 2026-05-16T01:39:17+00:00

In VBA / VB.NET you can assign Excel range values to an array for

  • 0

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:

 Dim cellTops As Variant : cellTops = Application.ActiveSheet.UsedRange.Top

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.

Update: 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 for my purposes of detecting controls that overlap content-filled cells. The steps are basically:

(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)

(2) Iterate over the cells in the interesting rows and perform overlap detection using only the left and right positions of the cells.

The code for finding the interesting set of rows looks something like:

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 <= objBottom AndAlso rowBottom >= objTop Then
        If feasible Is Nothing Then
            feasible = used.Rows(r)
        Else
            feasible = Application.Union(used.Rows(r), feasible)
        End If
    ElseIf rowTop > objBottom Then
        Exit For
    End If
Next r
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T01:39:18+00:00Added an answer on May 16, 2026 at 1:39 am

    Todd,

    The best solution I could think of was to dump the tops into a range and then dump those range values into a variant array. As you said, the For Next (for 10,000 cells in my test) took a few seconds. So I created a function that returns the top of the cell that it’s entered into.
    The code below, is mainly a function that copies the usedrange of a sheet you pass to it and then enters the function described above into each cell of the usedrange of the copied sheet. It then transposes and dumps that range into a variant array.

    It only takes a second or so for 10,000 cells. Don’t know if it’s useful, but it was an interesting question. If it is useful you could create a separate function for each property or pass the property you’re looking for, or return four arrays(?)…

    Option Explicit
    Option Private Module
    
    Sub test()
    Dim tester As Variant
    
    tester = GetCellProperties(ThisWorkbook.Worksheets(1))
    MsgBox tester(LBound(tester), LBound(tester, 2))
    MsgBox tester(UBound(tester), UBound(tester, 2))
    
    End Sub
    
    Function GetCellProperties(wsSourceWorksheet As Excel.Worksheet) As Variant
    Dim wsTemp As Excel.Worksheet
    Dim rngCopyOfUsedRange As Excel.Range
    Dim i As Long
    
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    
    wsSourceWorksheet.Copy after:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
    Set wsTemp = ActiveSheet
    Set rngCopyOfUsedRange = wsTemp.UsedRange
    rngCopyOfUsedRange.Formula = "=CellTop()"
    wsTemp.Calculate
    GetCellProperties = Application.WorksheetFunction.Transpose(rngCopyOfUsedRange)
    Application.DisplayAlerts = False
    wsTemp.Delete
    Application.DisplayAlerts = True
    Set wsTemp = Nothing
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    
    End Function
    
    Function CellTop()
    CellTop = Application.Caller.Top
    End Function
    

    Todd,

    In answer to your request for a non-custom-UDF I can only offer a solution close to what you started with. It takes about 10 times as long for 10,000 cells. The difference is that your back to looping through cells.

    I’m pushing my personal envelope here, so maybe somebody will have a way to to it without a custom UDF.

    Function GetCellProperties2(wsSourceWorksheet As Excel.Worksheet) As Variant
    Dim wsTemp As Excel.Worksheet
    Dim rngCopyOfUsedRange As Excel.Range
    Dim i As Long
    
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    
    wsSourceWorksheet.Copy after:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
    Set wsTemp = ActiveSheet
    Set rngCopyOfUsedRange = wsTemp.UsedRange
    With rngCopyOfUsedRange
    For i = 1 To .Cells.Count
    .Cells(i).Value = wsSourceWorksheet.UsedRange.Cells(i).Top
    Next i
    End With
    GetCellProperties2 = Application.WorksheetFunction.Transpose(rngCopyOfUsedRange)
    Application.DisplayAlerts = False
    wsTemp.Delete
    Application.DisplayAlerts = True
    Set wsTemp = Nothing
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    
    End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can VBA code instantiate and use .NET objects? The specific class I'm interested in
Is there code in VBA I can wrap a function with that will let
Using VBA, how can I: test whether a file exists, and if so, delete
What VBA code is required to perform an HTTP POST from an Excel spreadsheet?
I'm using Excel VBA to a write a UDF. I would like to overload
Is VBA going to go away any time soon, like VB6 has? Should I
Using VBA i have a set of functions that return an ADODB.Recordset where all
Using VBA in MS Office, how do I add text to the Windows clipboard
I work in VBA, and want to parse a string eg <PointN xsi:type='typens:PointN' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
I had a VBA project in outlook with a few email macros - but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.