Link to home
Start Free TrialLog in
Avatar of Mike Rudolph
Mike RudolphFlag for United States of America

asked on

Excel: Looping through rows to enter data in empty cell

Dear Experts,

A little stuck here... Please see attached screenshot:
When item is selected (red arrow) vba code (Worksheet_SelectionChange) runs a vlookup to enter data into cell L3. Would like then to run a looping structure to find an empty cell between A10 through A16 to enter that code. Each time I invoke the SelectionChange event I would like to keep populating any empty cell. Should the rows between A10 and A16 fill up I would like to start replacing values beginning with A10.  
User generated image
Thanks in advance for looking at this.

Here is the code I currently have but of course as you can see my current do while loop enters values into Cell A10 through A16 which is not what I want to do. I want to enter the value only once into any empty cell between those rows.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
 If Not Intersect(Target, [M8:U65]) Is Nothing Then

    'MsgBox Target.Address
     
    Dim lookFor As Range
    Dim rng As Range
    Dim col As Integer
    Dim found As Variant
    Dim ws As Worksheet
    Dim row As Integer
    
    Set ws = ActiveWorkbook.Sheets("Report")
    row = 10
    
    Set lookFor = Sheets("Report").Range(Target.Address)
    Set rng = Sheets("Number and Operations").Columns("A:B")
    col = 2
    
    On Error Resume Next
    found = Application.WorksheetFunction.VLookup(lookFor.Value, rng, col, 0)
    If IsError(found) Then
    MsgBox lookFor & " not found"
    Else: Range("L3").Value = found
       'MsgBox "The look-up value of " & lookFor & " is " & found & " in column " & col
    End If
    On Error GoTo 0
    
    
    Do
     ws.Cells(row, 1).Value = Range("L3")
     row = row + 1
    Loop While row < 17
    
    row = 10
End If

 
End Sub
 

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Anthony Berenguel
Anthony Berenguel
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial