Consulting

Results 1 to 4 of 4

Thread: "For Each" an endless loop, any better ideas?

  1. #1
    VBAX Regular
    Joined
    Feb 2009
    Posts
    8
    Location

    "For Each" an endless loop, any better ideas?



    Hello,

    When my userform enters data into my spreadsheet, it will need more than one row in some columns. The problem is, the next time I enter data, it will copy over the data in the extra rows. I've tried to remedy this with an index column that will have an "x" in Column A for every corresponding cell in Column K, but the "For Every" function is an endless loop.
    There has to be a better way?


    Dim Cell As Range
    For Each Cell In Range("K:K")
    If ActiveCell.Value > 0 Then ActiveCell.Offset(0, -10).Value = "x"
    Next Cell

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    You need to look for the last row of data and put your userform input in the next row?

    Fairly easy, could you post your workbook, it would be much easier to help you. hit post reply at the bottom left of the last post, scroll down till you see "manage attachments"

    best if it is a 2003 version for me, I don't have 07
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Regular
    Joined
    Feb 2009
    Posts
    8
    Location

    Thanks

    Actually Lucas,

    In preparing an answer, I figured out the issue.
    I've attached a word document with the userform code.

    If I just change the "NextRow" function to calculate on column K instead of A, then the issue is resolved.

  4. #4
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    You might want to try something like this. The 1 denotes column A so if you want to change it to 11 to check column K for the last row use:

    [vba]NextRow = Cells(Rows.Count, 11).End(xlUp).Row + 1[/vba]

    [vba]Sub y()
    Dim NextRow As Long
    NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
    MsgBox NextRow
    End Sub
    [/vba]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

Posting Permissions

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