Results 1 to 20 of 20

Thread: Howto Find LAST empty CELL in RANGE

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2006
    Posts
    7

    Howto Find LAST empty CELL in RANGE

    Hello,

    I want to find the LAST empty cell in a range. Example:

    Let say that no values are in A1 to A7, I have some values in A8 up to A20. If I run the function. It should search within RANGE A and will tell me that A21 is available.

    How ?

  2. #2
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: Howto Find LAST empty CELL in RANGE

    Welcome to the Forums.

    Do you want to do this through VBA or as an Excel function? The two will be quite different. Also, can you be more specific about what you mean by "Range". In your example, the last empty cell in range A is A65000. If, however, you specify the range to be A1:A21, then indeed it will be A21.

    Through VBA code, you would just loop:

    VB Code:
    1. For i = 21 to 1 step -1
    2.   if range("A" & i) = "" then
    3.    rownumber = i
    4.    exit for
    5.   end if
    6.  next i
    7.  
    8.  if i = 1 and range ("A1") <> "" then
    9.  Msgbox "No empty cells"
    10.  else
    11.  MsgBox "Last empty cell is A" & rownumber
    12.  end if


    or something of the sort.

    zaza

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

    Re: Howto Find LAST empty CELL in RANGE

    Welcome to the Forums.

    You can use the .SpecialCells function to do that.

    VB Code:
    1. 'Row:
    2. MsgBox Workbooks(1).Sheets(1).Range("A:A").Cells.SpecialCells(xlCellTypeLastCell).Row
    3.  
    4. 'Column:
    5. MsgBox Workbooks(1).Sheets(1).Range("A:A").Cells.SpecialCells(xlCellTypeLastCell).Column
    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

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2006
    Posts
    7

    Re: Howto Find LAST empty CELL in RANGE

    Thanks All.

    It works fine...

  5. #5
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: Howto Find LAST empty CELL in RANGE

    Actually, you want a "+1" on the end of that Row RD...






    Forgot about SpecialCells. Nice one.

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

    Re: Howto Find LAST empty CELL in RANGE

    It gives the last "used" row so its implied.

    Thanks
    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

  7. #7
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Howto Find LAST empty CELL in RANGE

    Just done a search on this topic.
    I have tried using this but it gives me '357' when the last used row at the moment is '16
    VB Code:
    1. 'Column:
    2. MsgBox Workbooks(1).Sheets(1).Range("A:A").Cells.SpecialCells(xlCellTypeLastCell).Column

    I have also tried this piece of code but I cannot get it to start from the point I want to, row 8 rather than row 1
    VB Code:
    1. Do
    2.         Range)"A8").Select
    3.         If IsEmpty(ActiveCell) = False Then
    4.             ActiveCell.Offset(1, 0).Select
    5.             ctr = ctr + 1
    6.         End If
    7.     Loop Until IsEmpty(ActiveCell) = True
    I just want to find the first empty row after row 8, these rows are used for various titles and some are merged.

    Any ideas?
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  8. #8
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Howto Find LAST empty CELL in RANGE

    Hey RobDog:

    In the following:

    MsgBox Workbooks(1).Sheets(1).Range("A:A").Cells.SpecialCells(xlCellTypeLastCell).Column

    I believe that "Range("A:A")." is meaningless and non-functional. "Cells.SpecialCells(xlCellTypeLastCell).Column" will always return the index of the last column on the sheet that has data in it.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  9. #9
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Howto Find LAST empty CELL in RANGE

    Likewise:

    MsgBox Workbooks(1).Sheets(1).Range("A:A").Cells.SpecialCells(xlCellTypeLastCell).Row

    Range("A:A"). is ignored and the actual Row index returned is the row number of the bottom-most, right-most populated cell on the sheet. This has always been an aggravation to me, that you can't sepcify a range.

    I think you have do do a reverse iterative test to find the "first empty cell after the last used cell in a range".

    To find the last cell in a range:

    aRange.Cells(aRange.Cells.Count)

    Returns the last cell in range "aRange". Add one to the row or column, and iteratively test backwards from the end looking for empty cells.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  10. #10
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Howto Find LAST empty CELL in RANGE

    Thanks All
    WebTest:
    I think I understand this for the most part, but what exactly is 'aRange'

    I just tried this and it didn't work:
    VB Code:
    1. Range("A7").Cells (Range("A7").Cells.Count)

    I looked up aRange is the help files and found nothing
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  11. #11
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Howto Find LAST empty CELL in RANGE

    Just tried this as well

    VB Code:
    1. Range("A7").Select
    2. aRange.Cells (aRange.Cells.Count)

    Sorry if this is obvious!!
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  12. #12
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Howto Find LAST empty CELL in RANGE

    This is the basic idea. If you have multiple Workbooks open or multiple sheets, you'll need to handle those specifically. This works for one sheet in one book.
    Code:
    Option Explicit
    
    Sub Macro1()
        Dim aRow As Long
        Dim aRange As Range
        
        'This is an attempt to find a cell in column A that is guaranteed to be empty
        'Find the row number below the bottom-most occupied cell
        aRow = Sheets(1).Cells.SpecialCells(xlCellTypeLastCell).Row + 1
    
        While Cells(aRow, "A").Value = ""
            aRow = aRow - 1
        Wend
        
        'aRange is the first available empty cell below the last used cell in Column A
        Set aRange = Cells(aRow + 1, "A")
        
        MsgBox aRange.Address
    
    End Sub
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  13. #13
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Howto Find LAST empty CELL in RANGE

    Thanks again Webtest

    I have come up with the following, which counts how many rows are used after the rows used in titles, merged rows etc.
    It also puts the names in the column into an array so i can check for duplicates.

    Apart from the loop going to 1000, I can't see a major problem with this as firstly it will never get to that many names, probably no more that 1 or 2 hundred, and secondly, as soon as an empty cell is found it exits the loop.

    Can you see anything wrong with this code

    VB Code:
    1. 'loop through to find how many rows are used already for array
    2.     Range("A8:A1000").Select
    3.     For ctr = 1 To 1000
    4.         Do While ActiveCell(ctr).Value <> ""
    5.             ReDim Preserve strNames(ctr - 1)
    6.             strNames(UBound(strNames)) = ActiveCell(ctr).Value
    7.             Debug.Print ActiveCell(ctr).Value
    8.             Debug.Print strNames(UBound(strNames))
    9.             ctr = ctr + 1
    10.         Loop
    11.         Exit For
    12.     Next
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  14. #14
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Howto Find LAST empty CELL in RANGE

    ActiveCell returns a single cell as a Range. I would be surprised if using an index "(ctr)" would work at all. Doesn't it generate an error?

    ActiveCell(ctr).Value
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  15. #15
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Howto Find LAST empty CELL in RANGE

    No, no error at all.
    When using the debug.print it shows exactly what I expect it to show, both in the activecell.value and in the array element.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  16. #16
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Howto Find LAST empty CELL in RANGE

    Hmmmmm ... interesting. It sort of works as a 'cheap' row offset function ... stays in the same column, but walks down the rows. Index of "1" is the currently active cell. Indexing a range object seems to work the same way.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  17. #17
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Howto Find LAST empty CELL in RANGE

    So you don't see a problem with it as it is then?
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  18. #18
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Howto Find LAST empty CELL in RANGE

    It seems that adding an index to a RangeObject assumes a ".Offset" prefix to the index parentheses. Surprisingly, even the following works ... resulting in "D17".

    Range("C9")(9, 2).Address
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  19. #19
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Howto Find LAST empty CELL in RANGE

    For the sake of clarity and compatibility I would explicitly add in the ".Offset(ctr)".
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  20. #20
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Howto Find LAST empty CELL in RANGE

    This is really tricky! Without the explicit "Offset" the offsets are 1 based. With the Offset added explicitly, the offsets are ZERO based!
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

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