Consulting

Results 1 to 7 of 7

Thread: Where's the Bug

  1. #1

    Where's the Bug

    Working on a VERY simple function here, I would like to find the bug instead of a new way to do it (I would assume I'm pretty close b/c it's so easy), but I know people are busy, so I'll take whatever you brilliant people are willing to give

    I'm creating a function to find the last row (to use in another sub). This Code WORKS:

    [vba]Sub Last_Row()

    lastRow = Cells(Rows.Count, 1).End(xlUp).Address

    End Sub[/vba]
    So I would assum my function would work too, but it doesn't:
    [vba]
    Function LROW(rng) As Range

    LROW = Range(rng).Cells(Rows.Count, 1).End(xlUp).Address

    End Function
    [/vba]
    I get the generic error "Object variable or With block variable not set" when I run it through my tester Sub:

    [vba]Sub TestLROW()

    Dim Test As Range

    Test = LROW("A1")

    End Sub[/vba]
    I've tried the function another way too that more closely mirrors my original working Sub:
    [vba]Function LROW(col) As Range

    LROW = Cells(Rows.Count, col).End(xlUp).Address

    End Function[/vba]
    This one gets the same error message..... UGH!

    Thank you in advance, you people are great!

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    I recommend always using Option Explicit.

    Your errors are that your function expects a range object to be returned. Use Set to set a Range object.

    Also, be aware that you are getting the last row for column A, not for the usedrange in the sheet. You can use what I did in the Sub t, to get the first and last cells information like Address, Row, Column, etc.

    Here are some that I use. The last one returns the sent range less the first row.
    [vba]Sub t()
    Dim r As Range
    Set r = Range("Y4", Cells(Rows.Count, "Y").End(xlUp).Address)
    MsgBox r.Address
    MsgBox Cells(r.Row, r.Column).Address
    MsgBox r.Cells(r.Rows.Count, r.Columns.Count).Address

    End Sub

    '=LastNBRow(A3:G10)
    Function LastNBRow(rng As Range) As Long
    Dim LastRow As Long
    If WorksheetFunction.CountA(Cells) > 0 Then
    'Search for any entry, by searching backwards by Rows.
    LastRow = rng.Find(What:="*", After:=rng.Cells(rng.Rows.Count, rng.Columns.Count), _
    SearchOrder:=xlByRows, _
    SearchDirection:=xlPrevious).Row
    End If
    LastNBRow = LastRow
    End Function

    '=LastNBCol(A3:G10)
    Function LastNBCol(rng As Range) As Long
    Dim LastColumn As Integer
    If WorksheetFunction.CountA(Cells) > 0 Then
    'Search for any entry, by searching backwards by Columns.
    LastColumn = rng.Find(What:="*", After:=rng.Cells(rng.Rows.Count, rng.Columns.Count), _
    SearchOrder:=xlByColumns, _
    SearchDirection:=xlPrevious).Column
    End If
    LastNBCol = LastColumn
    End Function

    Function RangeLR1(aRange As Range) As Range
    Set RangeLR1 = Range(Cells(aRange.Row + 1, aRange.Column), Cells(aRange.Rows.Count, aRange.Columns.Count))
    End Function
    [/vba]

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    These will accept letter or numerical values, with an option to get the next row/column by adding an offset

    [vba]Option Explicit

    'Last Row
    Function LR(Col As Variant, Optional OS As Long)
    Application.Volatile
    If Not IsNumeric(Col) Then Col = Columns(Col).Column()
    LR = Cells(Cells.Rows.Count, Col).End(xlUp).Offset(OS)
    End Function

    'Last Column
    Function LC(Rw As Variant, Optional OS As Long)
    Application.Volatile
    If Not IsNumeric(Rw) Then Rw = Rows(Rw).Row()
    LC = Cells(Rw, Cells.Columns.Count).End(xlToLeft).Offset(, OS)
    End Function
    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    Thanks for that incredible input. I'm trying to debug and DO the code so I can half understand it.... Dr. Mack, When i run your functions I get the value for the last row, or last column. How would I alter the code so that it returns the Address of the last row/column? When I change the last part of the function to .Address, it gets angry at me and doesn't work

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]Sub test()
    MsgBox LR(5).Address
    End Sub



    'Last Row
    Function LR(Col As Variant, Optional OS As Long) As Range
    Application.Volatile
    If Not IsNumeric(Col) Then Col = Columns(Col).Column()
    Set LR = Cells(Cells.Rows.Count, Col).End(xlUp).Offset(OS)
    End Function[/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6

    Thank you...

    Thank you so much, I really appreciate it.

  7. #7
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    Quote Originally Posted by Factor3
    ... it gets angry at me and doesn't work
    As soon as you hear the bell ring, race in and grab Excel by the throat, and shake it around a bit....

    It works for me.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

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