Consulting

Results 1 to 17 of 17

Thread: Solved: the last cell in each column

  1. #1

    Solved: the last cell in each column

    Hi guys,does anyone know how to find the last row in a certain column with no content? what I mean "with no content" here is: even there was something but later deleted. for example the last cell without content in column A?

  2. #2
    VBAX Tutor
    Joined
    Jun 2012
    Posts
    269
    Location
    sheet1.range("A65536").end(xlup).row

    This will find the last row WITH data in column a

  3. #3
    VBAX Expert CatDaddy's Avatar
    Joined
    Jun 2011
    Posts
    581
    Location
    sheets(1).Range("A" & Rows.Count).End(xlUp).Row

    for sheets longer than 65536 rows
    ------------------------------------------------
    Happy Coding my friends

  4. #4
    Hi Ninja, first thanks a lot for your reply. but I tried it, I donīt think it really gives the row with an empty cell, becaus on a sheet in column a, the first empty cell is in row 67, but somehow this code returns a far more later rows,like over 100...I may misused it. so what if I want to write something like find the first empty row from column "a" to "z"?

    I have the following: [VBA] For j=1 to 27
    Set wrdTable = wrdDoc.Tables.Add(Range:=wrdRange, NumRows:=Sheets("excelReady").Range("A65536").Offset(0, j - 1).End(xlUp).Row, NumColumns:=1)[/VBA]

    so how to express it so that it will find the first empty cell in column A and return the number of that row and do it for column B and so on?

  5. #5
    thanks! it is very thoughtful, but my level is not yet there, I didnīt even know that an excel sheet could be so long...

  6. #6
    VBAX Expert CatDaddy's Avatar
    Joined
    Jun 2011
    Posts
    581
    Location
    never mind...
    ------------------------------------------------
    Happy Coding my friends

  7. #7
    VBAX Tutor
    Joined
    Jun 2012
    Posts
    269
    Location
    Hi Ninja, first thanks a lot for your reply. but I tried it, I donīt think it really gives the row with an empty cell, becaus on a sheet in column a, the first empty cell is in row 67, but somehow this code returns a far more later rows,like over 100...I may misused it. so what if I want to write something like find the first empty row from column "a" to "z"?
    first EMPTY cell would be sheet1.range("A1").end(xldown).row + 1
    so... try putting data in column 1 of a blank spreadsheet... then run the following test scripts to see exactly what they do.

    [vba]
    Sub test()
    'find first empty cell in column A
    MsgBox (Sheet1.Range("A1").End(xlDown).Row) + 1

    End Sub

    Sub test2()
    'find first empty row AFTER ALL DATA in column A
    MsgBox (Sheet1.Range("A65536").End(xlUp).Row + 1)

    End Sub

    [/vba]

  8. #8
    Hi, thanks for your reply. but I couldnīt change j-1 to j, b/c I want to find the empty cell in column a as well, and I also changed it to j as you suggested, it still doensīt work as expected.

  9. #9
    VBAX Expert CatDaddy's Avatar
    Joined
    Jun 2011
    Posts
    581
    Location
    using xlDown is not as reliable for finding the last row as xlUp
    ------------------------------------------------
    Happy Coding my friends

  10. #10
    VBAX Tutor
    Joined
    Jun 2012
    Posts
    269
    Location
    using xlDown is not as reliable for finding the last row as xlUp
    XlDown will find the first empty cell... not sure what sakura.saki really wants... both have uses imho.

  11. #11
    you are a hero...I just found out everything you provide is right, just I made a very stupid mistake... thanks a lot for your patience! I would have gone mad if I have to talk to myself to figure out what i want...

  12. #12
    VBAX Tutor
    Joined
    Jun 2012
    Posts
    269
    Location
    NumRows:=Sheets("excelReady").Range("A65536").Offset(0, j - 1).End(xlUp).Row
    You need something more like this:

    [VBA]
    ' add this function outside of the subroutine
    Private Function getColLtr2(ByVal i As Integer) As String
    getColLtr2 = Split(Cells(1, i).Address, "$")(1)
    End Function

    'change the following:
    NumRows:=Sheets("excelReady").Range("A65536").Offset(0, j - 1).End(xlUp).Row
    To:
    NumRows:= sheets("ExcelRead").range(getColLtr(j) & "65536").end(xlup).row


    [/VBA]

    Good luck...

  13. #13
    VBAX Expert CatDaddy's Avatar
    Joined
    Jun 2011
    Posts
    581
    Location
    what was the problem sakura.saki?
    ------------------------------------------------
    Happy Coding my friends

  14. #14
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    If your column has formulas that could evaluate to "" (like =IF(B1=1,"","cat")), this will find the last row in that column that shows a value.

    [VBA]With Range("A:A")
    MsgBox .Find("?", after:=.Cells(1, 1), LookIn:=xlValues, lookat:=xlWhole, searchdirection:=xlPrevious).Row
    End With[/VBA]

    The End(xlup) approach will find the last cell with a formula in it.

  15. #15
    now I donīt have the problem anymore...thanks to you guys!it is solved

  16. #16
    Hi mikerickson, thanks. but I donīt really know what will appear at the last row, so it doenīt really apply. but it doensīt matter, it is already solved.
    :-)

  17. #17
    Hi Ninja, I donīt know how to thank you. it is really very nice of you. you are really an expert, that is why it might be hard for you to understand what kind of stupid mistake I made. the one I used to say that it doensīt work works actually fine. many many thanks!

Posting Permissions

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