Consulting

Results 1 to 3 of 3

Thread: Troubles with Counting Used Rows

  1. #1

    Troubles with Counting Used Rows

    Okay then!

    In the program I am working on, the user enters data into a UserForm and then the Code, does some stuff with the data and then it needs to enter the data onto the next available Row in WorkSheets(1). So I am trying to assign the indes of the Next Available Row to the variable "NextRow"

    I tried using

    Range("A1", ActiveCell.SpecialCells(xlLastCell)).Select
        NextRow = Selection.Rows.Count + 1
    AND

    NextRow = ActiveSheet.UsedRange.Rows.Count + 1

    The Problem with both of these is that the functions will Count a row as being used, even if there is no data in it, but if there was at some point.

    That is, if I enter data in row 4 and then 'delete' it OR 'clear' it, it will still count that row.

    How can I get around this?

    Also, if you open a brand new worksheet and run the function

    MsgBox ActiveSheet.UsedRange.Rows.Count
    or
    Range("A1", ActiveCell.SpecialCells(xlLastCell)).Select
        NextRow = Selection.Rows.Count
    MsgBox NextRow
    It will always return 1. why? The first Row is empty...

    Thanks.
    Last edited by Aussiebear; 04-21-2023 at 06:55 PM. Reason: Adjusted the code tags

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    If Range("A1").Value = "" Then
    NextRow = 1
        ElseIf Range("A2").Value = "" Then
    NextRow = 2
        Else
    NextRow = Cells(Rows.Count, "A").End(xlUp).Row
        End If
    Last edited by Aussiebear; 04-21-2023 at 06:55 PM. Reason: Adjusted the code tags
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Quote Originally Posted by xld

    If Range("A1").Value = "" Then
    NextRow = 1
        ElseIf Range("A2").Value = "" Then
    NextRow = 2
        Else
    NextRow = Cells(Rows.Count, "A").End(xlUp).Row
        End If
    Here is what I like about it: It will not count a cell that previously had data in it, but was deleted.

    Here is what I don't like: It only checks column "A".....so technically it is not checking 'rows'.

    I might be able to work with this one though. Thanks!
    Last edited by Aussiebear; 04-21-2023 at 06:56 PM. Reason: Adjusted the code tags

Posting Permissions

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