Results 1 to 2 of 2

Thread: Loop on Single cell

  1. #1
    VBAX Newbie
    Joined
    Jun 2022
    Posts
    1
    Location

    Loop on Single cell

    Hi

    I’m relatively new to VBA and I’m currently stuck on creating a loop function. I have an IF statement (below) written. Where I need to increase a number (which is an option number) in the active cell until the criteria of another cell (4 to the right of active cell). I’ve had to use double rather than integer due to my value of the dependant cell (score) have decimals involved.

    Sub Name()
    Dim score As Double 
    score = ActiveCell.Offset(0,4).value
    If score >=2 Then
    ActiveCell = ActiveCell +1
    End If
    End Sub
    I have tried Do/Loop While score >= 2 and when the dependant cell starts on <2 it doesn’t add anything (which is correct) however when starting on a value >=2 it keeps the loop going even when a number <2 has come in the dependant cell

    Hope this all makes sense and appreciate any help
    Last edited by Aussiebear; 06-02-2022 at 11:52 AM. Reason: Added code tags to supplied code

  2. #2
    Administrator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,302
    Location
    Hi gally,

    Welcome to the forum.

    If your data was in column A then maybe something like the below:
    Sub test()    
        Dim rng As Range, rCell As Range
        
        Set rng = Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
        
        For Each rCell In rng.Cells
            If rCell.Offset(, 4).Value >= 2 Then rCell.Value = rCell.Value + 1
        Next rCell
    End Sub
    Hope this helps
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2408, Build 17928.20080

Posting Permissions

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