View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
spyd3r spyd3r is offline
external usenet poster
 
Posts: 7
Default Modify range variable passed through Function

Good questions Trevor, ones I wasn't sure I knew the answer to ;)

Your intersect idea works great sebastienm. It now takes 10 seconds to
update the formulas in my spreasheet versus the 10 minutes it was taking
before. It isn't perfect, as the .usedrange property isn't perfect, but the
worst that should happen is it ends up cycling through a few extra blank
rows. It shouldn't accidentally cut out any rows, at least from what I can
tell?

Thanks for the help!

"sebastienm" wrote:

Hi,
try the UsedRange property of the worksheet object

Dim r as range, cell as range
set r= range("A1:C56")
'limit r to the usedRange and not beyond
set r= application.intersect(r, r.parent.usedRange)
debug.print r.address

Now loop through each cell in r
for each cell in r.cells
...
next
--
Regards,
Sébastien
<http://www.ondemandanalysis.com


"spyd3r" wrote:

I wrote a function where, amongst other things, a range is passed through. I
want to allow a range to be passed so that the multiple columns can be
passed, if necessary, and on different sheets. Otherwise, I could just make
the required pass through a column number...but, I want to keep the
flexibility of a passed range.

Once I have this range passed, I loop through each cell in the range,
performing various calculations. Now, if someone passes the range "D:D", my
code loops through all 65000+ cells, skipping most of the For... loop if the
cell.value = "". What I would like to do is figure out the last valid cell
in the passed range, before going through the For... loop, so that I can cut
down the number of looped cells significantly.

I could do something like this, but it seems very messy and only handles one
column. I guess I'm looking for a way to speed up looping through a passed
range, without looping through the entire range. Thanks for any help!

Public Sub test()
Dim rng As Range
Dim rgn2 As Range
Dim str1 As String
Dim str2 As String

Set rng = Range("Sheet1!D:D")

str1 = rng.Worksheet.name & "!"
str2 = rng.Address
str1 = str1 & Left(str2, InStr(1, str2, ":") - 1) & "1:"
str1 = str1 & Range(rng.Worksheet.name & "!" & Mid(str2, InStr(1, str2, ":")
+ 1) & "65536").End(xlUp).Address

Debug.Print str1

End Sub