+ Reply to Thread
Results 1 to 5 of 5

usedrange function

Hybrid View

  1. #1
    Registered User
    Join Date
    04-29-2006
    Posts
    12

    usedrange function

    I'm just starting to work with functions in VBA. Does something like this look ok to use when getting the first and last rows of the used range? I'm wondering if there's a better or more efficient way of doing this.

    -----
    Sub test()
    a = rangeUsed(0)
    b = rangeUsed(1)

    MsgBox "first row = " & a & vbLf & "last row = " & b
    End Sub


    Function rangeUsed()
    Dim firstRow As Long, lastRow As Long

    With ActiveSheet.UsedRange.Rows
    firstRow = .Row
    lastRow = .Count + (firstRow - 1)
    End With

    rangeUsed = Array(firstRow, lastRow)
    End Function
    -----

    Thanks.

    Jay

  2. #2
    Tom Ogilvy
    Guest

    RE: usedrange function

    If you want to know the first an last row of the rectangular area that Excel
    is maintaining information on (irrespective of wether there are values in
    those cells), then yes, I think that should work.

    --
    Regards,
    Tom Ogilvy


    "jayy" wrote:

    >
    > I'm just starting to work with functions in VBA. Does something like
    > this look ok to use when getting the first and last rows of the used
    > range? I'm wondering if there's a better or more efficient way of
    > doing this.
    >
    > -----
    > Sub test()
    > a = rangeUsed(0)
    > b = rangeUsed(1)
    >
    > MsgBox "first row = " & a & vbLf & "last row = " & b
    > End Sub
    >
    >
    > Function rangeUsed()
    > Dim firstRow As Long, lastRow As Long
    >
    > With ActiveSheet.UsedRange.Rows
    > firstRow = .Row
    > lastRow = .Count + (firstRow - 1)
    > End With
    >
    > rangeUsed = Array(firstRow, lastRow)
    > End Function
    > -----
    >
    > Thanks.
    >
    > Jay
    >
    >
    > --
    > jayy
    > ------------------------------------------------------------------------
    > jayy's Profile: http://www.excelforum.com/member.php...o&userid=33975
    > View this thread: http://www.excelforum.com/showthread...hreadid=538563
    >
    >


  3. #3
    Registered User
    Join Date
    04-29-2006
    Posts
    12
    Hi Tom,

    Thanks very much for your reply. Out of curiosity, if I did only want rows that have text is the best way to loop through each row and check for rows that aren't empty?

    Thanks.

    Jay

  4. #4
    Tom Ogilvy
    Guest

    Re: usedrange function

    http://www.beyondtechnology.com/geeks012.shtml

    --
    Regards,
    Tom Ogilvy

    "jayy" <jayy.278xfa_1146684001.9842@excelforum-nospam.com> wrote in message
    news:jayy.278xfa_1146684001.9842@excelforum-nospam.com...
    >
    > Hi Tom,
    >
    > Thanks very much for your reply. Out of curiosity, if I did only want
    > rows that have text is the best way to loop through each row and check
    > for rows that aren't empty?
    >
    > Thanks.
    >
    > Jay
    >
    >
    > --
    > jayy
    > ------------------------------------------------------------------------
    > jayy's Profile:

    http://www.excelforum.com/member.php...o&userid=33975
    > View this thread: http://www.excelforum.com/showthread...hreadid=538563
    >




  5. #5
    Registered User
    Join Date
    04-29-2006
    Posts
    12
    Hi Tom,

    Thanks so much for the info. I really appreciate it!

    Jay

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1