Mike Rudolph
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.
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.
A little stuck here... Please see attached screenshot:
When item is selected (red arrow) vba code (Worksheet_SelectionChange
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.