Results 1 to 11 of 11

Thread: [RESOLVED] Find next empty row, when data may/may not be in first column

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    50

    Resolved [RESOLVED] Find next empty row, when data may/may not be in first column

    I am trying to write a code that pastes data entered into a Userform into the next empty row.

    All is good, except for finding the empty row. Currently, I use the code:

    emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1

    But this only searches in column A for data. How do I find the empty row, when data could be entered into columns up to column Y (and beyond if possible)?

    Thanks.

  2. #2
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Find next empty row, when data may/may not be in first column

    Do it as you would do manually, that is: If you need to look in all that columns then just do it.
    Your EmptyRow will be the Maximum rowNumber found in all that columns.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    50

    Re: Find next empty row, when data may/may not be in first column

    Yeah I thought that would be a straight-forward solution (and it probably still is) but to do that, how would I manipulate the above code?

    I have tried unsuccessfully using:

    emptyRow = WorksheetFunction.CountA(Range("A:Z")) + 1

    Would it instead be something like:

    a = WorksheetFunction.CountA(Range("A:A")) + 1
    b = WorksheetFunction.CountA(Range("B:B")) + 1
    ...

    then emptyRow = (some code for max value of a:z)

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Find next empty row, when data may/may not be in first column

    you could try usedrange.rows.count property of the sheet object, but this too can lie sometimes, especially if you have empty rows at the top of the sheet, or some rows have been deleted
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    50

    Re: Find next empty row, when data may/may not be in first column

    Yeah, data gets deleted from rows in this workbook regularly, so other rows will have been used, but then become empty.

    Any other ideas?

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Find next empty row, when data may/may not be in first column

    vb Code:
    1. for col = 1 to 26
    2.   if lastrow < cells(rows.count, col) then lastow = cells(rows.count, col)
    3. next
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    50

    Re: Find next empty row, when data may/may not be in first column

    This code doesn't work for me I'm afraid. I have column headers in Row 1. I had data in Row 2, Column B. I ran the userform to add a new entry, hoping that it would place it in Row 3 when saved, but it actually entered it into Row 1, overwriting the column headers.

    From your code, I added a further line:

    emptyRow = lastrow + 1

    Is this where I am going wrong? (By the way, I corrected the spelling error in the code, so that is not the issue.)

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Find next empty row, when data may/may not be in first column

    By the way, I corrected the spelling error in the code,
    good you read it

    for some reason i only wrote half the code (brain dead yseterday)
    l
    vb Code:
    1. astrow = 1
    2.     For col = 1 To 26
    3.       If lastrow < Cells(Rows.Count, col).End(xlUp).Row Then lastrow = Cells(Rows.Count, col).End(xlUp).Row
    4.     Next
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    50

    Re: Find next empty row, when data may/may not be in first column

    Thanks very much, I'll try that, and I'll correct this spelling mistake too

  10. #10

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    50

    Re: Find next empty row, when data may/may not be in first column

    This works perfectly. Thank you.

  11. #11
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [RESOLVED] Find next empty row, when data may/may not be in first column

    and I'll correct this spelling mistake too
    not spelling /typing this time, selecting to copy missed the first character
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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