Consulting

Results 1 to 7 of 7

Thread: Last Row

  1. #1
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location

    Last Row

    I'm suffering RSI with typing "Cells(Rows.Count,1).End(xlUp)" so I'm going for a function. Any other suggestions?
    [VBA]Sub Test()
    Dim rng As Range
    Set rng = Range(Cells(1, 3), LRw(3,1))
    End Sub

    Function LRw(Col, Optional Oset As Long) As Range
    If IsMissing(Oset) Then Oset = 0
    Set LRw = Cells(Rows.Count, Col).End(xlUp).Offset(Oset)
    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'

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You can't do IsMissing with a typed argument, it has to be type Variant. By declaring it as long, it will be 0 if not present.

  3. #3
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Even better then, I can just delete the IsMissing line.
    Thanks Bob
    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
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Personally, I'd add a worksheet to the parameters. The function is then a litel more robust as you can pass it whatever sheet you want to test without having to activate it first.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Quote Originally Posted by Ken Puls
    Personally, I'd add a worksheet to the parameters. The function is then a litel more robust as you can pass it whatever sheet you want to test without having to activate it first.
    [vba]Option Explicit

    Sub Test()
    Dim WS As Worksheet
    Set WS = Sheets(1)
    Range(WS.Cells(2, 2), LRw(WS, 2, 1)).Interior.ColorIndex = 6
    End Sub

    Function LRw(LrSh, Col, Optional OSet As Long) As Range
    Set LRw = LrSh.Cells(Rows.Count, Col).End(xlUp).Offset(OSet)
    End Function

    [/vba]

    Edit:
    For more on this subject see The Last Row
    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
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You should declare the arguments, and qualify all

    [vba]

    Function LRw(ByVal LrSh As Worksheet, ByVal Col As Long, Optional ByVal OSet As Long) As Range
    With LrSh
    Set LRw = .Cells(.Rows.Count, Col).End(xlUp).Offset(OSet)
    End With
    End Function
    [/vba]

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Thanks Bob,
    Improvements always welcome. I'll make Col a variant though, so I can pass either the column letter or number.

    [vba]Option Explicit

    Sub Test()
    Dim WS As Worksheet
    Set WS = Sheets(1)
    Range(WS.Cells(2, 2), LRw(WS, 2, 1)).Interior.ColorIndex = 6
    Range(WS.Cells(2, "D"), LRw(WS, "D", 1)).Interior.ColorIndex = 8
    End Sub

    Function LRw(ByVal LrSh As Worksheet, ByVal Col As Variant, Optional ByVal OSet As Long) As Range
    With LrSh
    Set LRw = .Cells(.Rows.Count, Col).End(xlUp).Offset(OSet)
    End With
    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'

Posting Permissions

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