PDA

View Full Version : loop through cells



junior6202
03-31-2015, 12:55 PM
A
B
C
D
F


1






2


3/31/2015




I have a question I'm pretty new to excell vba and I created a FORM where there's a combobox that is populate by column A values. So if i were to choose in the conmbo box the value 2 I need a for loop that will look for the row with the value of 2 in column A and when it finds it I want to set column D with the date. This is what I have so far and the loops seem to work but it skips THEN
.


Private Sub cmdEnd_Click()
Dim i As Long

Sheet1.Activate

For i = 1 To Cells(Rows.Count, "A").End(xlUp).Row
If CmbID.Value = Cells(i, 1).Value Then
Cells(i, 4).Value = Format(Now(), "hh:mm AM/PM")
End If
Next i

End Sub


Thanks in advance.

NoSparks
03-31-2015, 04:58 PM
Turn your IF statement around, make it

If Cells(i, 1).Value = CmbID.Value Then

Bob Phillips
04-01-2015, 02:05 AM
Private Sub cmdEnd_Click()
Dim i As Long

Sheet1.Activate

For i = 1 To Cells(Rows.Count, "A").End(xlUp).Row

If Val(cmbID.Value) = Cells(i, 1).Value Then

Cells(i, 4).Value = Format(Now(), "hh:mm AM/PM")
End If
Next i
End Sub