Reputation: 67
I have this do loop code in my Userform
.
Private Sub cmdLogin_Click()
Dim user, pass As String
Dim x As Integer
user = Me.user1.Text
pass = Me.pass1.Text
x = 2
Do While ActiveWorkbook.Worksheets("Sheet1").Cells(x, 1).Value <> ""
If ActiveWorkbook.Worksheets("Sheet1").Cells(x, 1).Value = user _
And ActiveWorkbook.Worksheets("Sheet1").Cells(x, 2).Value = pass Then
MsgBox "YES"
Exit Do
Else
MsgBox "NO"
Exit Do
End If
Loop
End Sub
But it seems like the code does not loop throughout the cells and only stops until x=2 or cells(2,1). This results not being able to go through the whole list of usernames and passwords and every username and pass i enter becomes wrong except the ones at row 2. Please help as i am new to VBA
Upvotes: 1
Views: 371
Reputation: 2233
Just to simplify your own answer Angelo, here are some tips :
With ActiveWorkbook.Worksheets("Sheet1")
If (.Cells(x, 1) = user And .Cells(x, 2) = pass) Or _
(.Cells(x + 1, 1) = user And .Cells(x + 1, 2) = pass) Then
MsgBox "YES"
Exit Do
Else
MsgBox "NO"
End If
End With
Upvotes: 0
Reputation: 67
@PortlandRunner i have found the answer. thanks for helpiing. instead of placing x=x+1 right before the loop i did this
Do While ActiveWorkbook.Worksheets("Sheet1").Cells(x, 1).Value <> ""
If ActiveWorkbook.Worksheets("Sheet1").Cells(x, 1).Value = user And ActiveWorkbook.Worksheets("Sheet1").Cells(x, 2).Value = pass Then
MsgBox "YES"
Exit Do
Else
x = x + 1
If ActiveWorkbook.Worksheets("Sheet1").Cells(x, 1).Value = user And ActiveWorkbook.Worksheets("Sheet1").Cells(x, 2).Value = pass Then
MsgBox "YES"
Exit Do
Else
MsgBox "NO"
Exit Do
End If
End If
loop
thank you so much to the people who helped :)
Upvotes: 1
Reputation: 31364
If Cells(2,1)
is not ""
then you have an infinite loop because you never increment x
.
Just before Loop
add this: x = x + 1
to increment your loop.
I suggest you play with some of these examples to get an idea of how VBA loops work.
Upvotes: 4