Do While on Two Conditions
up vote
1
down vote
favorite
I have this Do While loop:
Sub TEST_LOOP()
Dim i As Integer
i = 2
Do While Cells(i, 3) <> "" And _
Int(Mid(Cells(i, 3), 12, 2)) = 21
Value = Value + Cells(i, 4)
i = i + 1
Loop
End Sub
Which is applied to this basic data set:
I get an error message:
"Run-time error '13': Type mismatch.
The problem happens because when the loop reaches the first empty cell (i = 7
)
then the Int function is applied to an empty value Mid(Cells(i, 3), 12, 2
gives nothing). So I was wondering whether there was an efficient way to exit the loop once the cell in question does not meet the two conditions, that is it is not empty and it involves the hour 21.
excel-vba while-loop
add a comment |
up vote
1
down vote
favorite
I have this Do While loop:
Sub TEST_LOOP()
Dim i As Integer
i = 2
Do While Cells(i, 3) <> "" And _
Int(Mid(Cells(i, 3), 12, 2)) = 21
Value = Value + Cells(i, 4)
i = i + 1
Loop
End Sub
Which is applied to this basic data set:
I get an error message:
"Run-time error '13': Type mismatch.
The problem happens because when the loop reaches the first empty cell (i = 7
)
then the Int function is applied to an empty value Mid(Cells(i, 3), 12, 2
gives nothing). So I was wondering whether there was an efficient way to exit the loop once the cell in question does not meet the two conditions, that is it is not empty and it involves the hour 21.
excel-vba while-loop
Unfortunately VBA doesn't havecontinue
orbreak
statements. See stackoverflow.com/a/12960972 for a solution.
– Robert Harvey♦
Nov 8 at 20:14
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have this Do While loop:
Sub TEST_LOOP()
Dim i As Integer
i = 2
Do While Cells(i, 3) <> "" And _
Int(Mid(Cells(i, 3), 12, 2)) = 21
Value = Value + Cells(i, 4)
i = i + 1
Loop
End Sub
Which is applied to this basic data set:
I get an error message:
"Run-time error '13': Type mismatch.
The problem happens because when the loop reaches the first empty cell (i = 7
)
then the Int function is applied to an empty value Mid(Cells(i, 3), 12, 2
gives nothing). So I was wondering whether there was an efficient way to exit the loop once the cell in question does not meet the two conditions, that is it is not empty and it involves the hour 21.
excel-vba while-loop
I have this Do While loop:
Sub TEST_LOOP()
Dim i As Integer
i = 2
Do While Cells(i, 3) <> "" And _
Int(Mid(Cells(i, 3), 12, 2)) = 21
Value = Value + Cells(i, 4)
i = i + 1
Loop
End Sub
Which is applied to this basic data set:
I get an error message:
"Run-time error '13': Type mismatch.
The problem happens because when the loop reaches the first empty cell (i = 7
)
then the Int function is applied to an empty value Mid(Cells(i, 3), 12, 2
gives nothing). So I was wondering whether there was an efficient way to exit the loop once the cell in question does not meet the two conditions, that is it is not empty and it involves the hour 21.
excel-vba while-loop
excel-vba while-loop
edited Nov 8 at 20:12
K.Dᴀᴠɪs
5,940102140
5,940102140
asked Nov 8 at 20:09
Alan
113
113
Unfortunately VBA doesn't havecontinue
orbreak
statements. See stackoverflow.com/a/12960972 for a solution.
– Robert Harvey♦
Nov 8 at 20:14
add a comment |
Unfortunately VBA doesn't havecontinue
orbreak
statements. See stackoverflow.com/a/12960972 for a solution.
– Robert Harvey♦
Nov 8 at 20:14
Unfortunately VBA doesn't have
continue
or break
statements. See stackoverflow.com/a/12960972 for a solution.– Robert Harvey♦
Nov 8 at 20:14
Unfortunately VBA doesn't have
continue
or break
statements. See stackoverflow.com/a/12960972 for a solution.– Robert Harvey♦
Nov 8 at 20:14
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
You need to check if the cell value is a date first, then proceed. See if this helps:
Sub TEST_LOOP()
Dim i As Long, ws As Worksheet
Set ws = ActiveSheet
For i = 2 To lastRow(ws, "C")
If isDate(ws.Cells(i, 3)) Then
If Int(Mid(ws.Cells(i, 3), 12, 2)) = 21 Then
Value = Value + ws.Cells(i, 4)
End If
End If
Next i
End Sub
Function lastRow(ws As Worksheet, Optional col As Variant = 1) As Long
With ws
lastRow = .Cells(.Rows.Count, col).End(xlUp).Row
End With
End Function
The issue is that in an IF()
or Do While
statement, everything is evaluated - even if a previous statement returns FALSE
. So you will need to first check if your cell is a date before trying to proceed.
Thanks very much K.Davis, it works perfectly! Best
– Alan
Nov 9 at 15:22
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
You need to check if the cell value is a date first, then proceed. See if this helps:
Sub TEST_LOOP()
Dim i As Long, ws As Worksheet
Set ws = ActiveSheet
For i = 2 To lastRow(ws, "C")
If isDate(ws.Cells(i, 3)) Then
If Int(Mid(ws.Cells(i, 3), 12, 2)) = 21 Then
Value = Value + ws.Cells(i, 4)
End If
End If
Next i
End Sub
Function lastRow(ws As Worksheet, Optional col As Variant = 1) As Long
With ws
lastRow = .Cells(.Rows.Count, col).End(xlUp).Row
End With
End Function
The issue is that in an IF()
or Do While
statement, everything is evaluated - even if a previous statement returns FALSE
. So you will need to first check if your cell is a date before trying to proceed.
Thanks very much K.Davis, it works perfectly! Best
– Alan
Nov 9 at 15:22
add a comment |
up vote
1
down vote
accepted
You need to check if the cell value is a date first, then proceed. See if this helps:
Sub TEST_LOOP()
Dim i As Long, ws As Worksheet
Set ws = ActiveSheet
For i = 2 To lastRow(ws, "C")
If isDate(ws.Cells(i, 3)) Then
If Int(Mid(ws.Cells(i, 3), 12, 2)) = 21 Then
Value = Value + ws.Cells(i, 4)
End If
End If
Next i
End Sub
Function lastRow(ws As Worksheet, Optional col As Variant = 1) As Long
With ws
lastRow = .Cells(.Rows.Count, col).End(xlUp).Row
End With
End Function
The issue is that in an IF()
or Do While
statement, everything is evaluated - even if a previous statement returns FALSE
. So you will need to first check if your cell is a date before trying to proceed.
Thanks very much K.Davis, it works perfectly! Best
– Alan
Nov 9 at 15:22
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You need to check if the cell value is a date first, then proceed. See if this helps:
Sub TEST_LOOP()
Dim i As Long, ws As Worksheet
Set ws = ActiveSheet
For i = 2 To lastRow(ws, "C")
If isDate(ws.Cells(i, 3)) Then
If Int(Mid(ws.Cells(i, 3), 12, 2)) = 21 Then
Value = Value + ws.Cells(i, 4)
End If
End If
Next i
End Sub
Function lastRow(ws As Worksheet, Optional col As Variant = 1) As Long
With ws
lastRow = .Cells(.Rows.Count, col).End(xlUp).Row
End With
End Function
The issue is that in an IF()
or Do While
statement, everything is evaluated - even if a previous statement returns FALSE
. So you will need to first check if your cell is a date before trying to proceed.
You need to check if the cell value is a date first, then proceed. See if this helps:
Sub TEST_LOOP()
Dim i As Long, ws As Worksheet
Set ws = ActiveSheet
For i = 2 To lastRow(ws, "C")
If isDate(ws.Cells(i, 3)) Then
If Int(Mid(ws.Cells(i, 3), 12, 2)) = 21 Then
Value = Value + ws.Cells(i, 4)
End If
End If
Next i
End Sub
Function lastRow(ws As Worksheet, Optional col As Variant = 1) As Long
With ws
lastRow = .Cells(.Rows.Count, col).End(xlUp).Row
End With
End Function
The issue is that in an IF()
or Do While
statement, everything is evaluated - even if a previous statement returns FALSE
. So you will need to first check if your cell is a date before trying to proceed.
edited Nov 8 at 20:29
answered Nov 8 at 20:19
K.Dᴀᴠɪs
5,940102140
5,940102140
Thanks very much K.Davis, it works perfectly! Best
– Alan
Nov 9 at 15:22
add a comment |
Thanks very much K.Davis, it works perfectly! Best
– Alan
Nov 9 at 15:22
Thanks very much K.Davis, it works perfectly! Best
– Alan
Nov 9 at 15:22
Thanks very much K.Davis, it works perfectly! Best
– Alan
Nov 9 at 15:22
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53215398%2fdo-while-on-two-conditions%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Unfortunately VBA doesn't have
continue
orbreak
statements. See stackoverflow.com/a/12960972 for a solution.– Robert Harvey♦
Nov 8 at 20:14