Results 1 to 10 of 10

Thread: [2005] Excel- Finding last used cell in a column.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    Minneapolis, MN
    Posts
    189

    [2005] Excel- Finding last used cell in a column.

    I want to find the last used cell in ANY given column in an Excel spreadsheet. The following code, and variations of it, seem to only report the last used row for the ENTIRE sheet, I don't want that.

    VB Code:
    1. lRow = oExcel.Range("A").SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Row

    I've tried messing with the range value and many other things, I always get the same. I know I can simulate a Ctrl-Up key sequence but that is not reliable (blank in between values). Column A may or may not be longer than B.

    To summarize, I want the last used cell (row) in column A, do some things, then get the last used cell (row) in column B and do some things.

  2. #2
    Frenzied Member
    Join Date
    Aug 2005
    Posts
    1,042

    Re: [2005] Excel- Finding last used cell in a column.

    Kriag K:
    Take a look at si's tutorial: http://www.vbforums.com/showthread.php?t=391665

    I think it will help you.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    Minneapolis, MN
    Posts
    189

    Re: [2005] Excel- Finding last used cell in a column.

    Quote Originally Posted by AIS4U
    Kriag K:
    Take a look at si's tutorial: http://www.vbforums.com/showthread.php?t=391665

    I think it will help you.
    Unfortunately, I did read the "special cells" section and tried what was there with no luck. The UsedRange applies to the entire sheet, not just a column. The xlUp or xlDown is not reliable due to the possibility of blank cells in between data. And the third example I haven't got to work.

    Remember, I want the last used cell in a particular column, not for the entire sheet. Maybe I could set the UsedRange to A1:A65536 but that seems really clunky. I was hoping the third method could be tweaked, it seems elegant, now if it would only check by column.

  4. #4
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] Excel- Finding last used cell in a column.

    Obviously you need to change the variables to suit your particular needs but this should work (or very similar). Put it in a loop to go through the cells. It may be worth starting at the end cell of the column you want to check and work backwards to find the last used cell.

    Not as efficient as the other methods I'm sure but it might do for now until someone posts the proper solution.

    VB Code:
    1. 'Use r and j to determine where to search:
    2. If CType(oSht.Cells(r, j), Range).Value.ToString = Nothing Then
    3.  
    4. Then
    Last edited by stimbo; Feb 7th, 2007 at 11:07 AM.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    Minneapolis, MN
    Posts
    189

    Re: [2005] Excel- Finding last used cell in a column.

    Quote Originally Posted by stimbo
    Obviously you need to change the variables to suit your particular needs but this should work (or very similar). Put it in a loop to go through the cells. It may be worth starting at the end cell of the column you want to check and work backwards to find the last used cell.

    Not as efficient as the other methods I'm sure but it might do for now until someone posts the proper solution.

    VB Code:
    1. 'Use r and j to determine where to search:
    2. If CType(oSht.Cells(r, j), Range).Value.ToString = Nothing Then
    3.  
    4. Then
    This would work which would basically be the same as what I did in another app, select the last cell in a column, then do xlUp (simulate Ctrl-Up) then get the address of the active cell. I was hoping for a more "clean" way and when I started messing around with the SpecialCells I thought that was going to be it.

    Looks like that may not work but thing is, I've seen other posts where people say to use that command, was hoping I was just missing it.

  6. #6
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] Excel- Finding last used cell in a column.

    I found a post by Rob

    Excel last row post

    I think this bit might be of interest. I could be completely wrong but the 1 here may refer to column A. So just adapt it from that. Does it work?
    VB Code:
    1. MessageBox.Show(wksht.Cells(1,wksht.Cells.SpecialCells(xlCellTypeLastCell).Row).Value.ToString)

    EDIT::::

    Should be this:
    VB Code:
    1. MessageBox.Show(wksht.Cells(1,wksht.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Row).Value.ToString)
    Last edited by stimbo; Feb 7th, 2007 at 11:32 AM.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    Minneapolis, MN
    Posts
    189

    Re: [2005] Excel- Finding last used cell in a column.

    Quote Originally Posted by stimbo
    I found a post by Rob

    Excel last row post

    I think this bit might be of interest. I could be completely wrong but the 1 here may refer to column A. So just adapt it from that. Does it work?
    VB Code:
    1. MessageBox.Show(wksht.Cells(1,wksht.Cells.SpecialCells(xlCellTypeLastCell).Row).Value.ToString)

    EDIT::::

    Should be this:
    VB Code:
    1. MessageBox.Show(wksht.Cells(1,wksht.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Row).Value.ToString)
    I moved the 1 behind the special cells, since I'm looking for row not column and it throws a System.NullReferenceException. Then I tried it just as you had it, same thing. I'm toying with it now, it looks like it should work.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    Minneapolis, MN
    Posts
    189

    Resolved Re: [2005] Excel- Finding last used cell in a column.

    Did a Google search and actually led me to a post on these forums. Original Post

    I made it VB friendly. All you'll have to do then is parse out the row number from the returned cell address.

    VB Code:
    1. Private Sub FindLastUsedCell()
    2.         Dim aRow As Integer
    3.         Dim aRange As Excel.Range
    4.  
    5.         'This finds the last used cell in column A that is guaranteed to be empty
    6.         'Find the row number below the bottom-most occupied cell for the entire sheet
    7.         aRow = oExcel.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Row
    8.  
    9.         'Loop from lowest cell up until first cell with a value
    10.         While oExcel.Cells(aRow, 1).Value = ""
    11.             aRow = aRow - 1
    12.         End While
    13.  
    14.         'aRange is the last used cell in Column A
    15.         aRange = oExcel.Cells(aRow, 1)
    16.  
    17.         'pick which style
    18.         Debug.WriteLine(aRange.Address)
    19.         Debug.WriteLine(aRange.Address(, , Excel.XlReferenceStyle.xlR1C1))
    20.  
    21.     End Sub

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    Minneapolis, MN
    Posts
    189

    Re: [2005] Excel- Finding last used cell in a column.

    Replace the appropriate section in the above code to parse out the cell number.

    VB Code:
    1. Dim Position, Cell As Integer
    2.  
    3. 'aRange is the last used cell below in Column A
    4.         aRange = oExcel.Cells(aRow, 1)
    5.         Position = InStr(2, aRange.Address(, , Excel.XlReferenceStyle.xlR1C1), "C")
    6.         Cell = Mid(aRange.Address(, , Excel.XlReferenceStyle.xlR1C1), 2, (Position - 2))
    7.         Debug.WriteLine(Cell)

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2005] Excel- Finding last used cell in a column.

    Yes, SpecialCells does seem to have an issue returning the last used cell for the entire sheet instead of the specified range when specified.

    I have found that UsedRange works best for situations like yours.

    VB Code:
    1. 'Column A = 1
    2. Dim iRow As Integer = oSht.UsedRange(65535, 1).End(Microsoft.Office.Interop.Excel.XlDirection.xlUp).Row
    3. MessageBox.Show("Last Used Row in Column (A): " & iRow.ToString, "Last Used Row", MessageBoxButtons.OK)
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

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



Click Here to Expand Forum to Full Width