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

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