Help with the Do While Loop

PresidentEvil

New Member
Joined
Jan 2, 2021
Messages
34
Office Version
  1. 2021
  2. 2016
Platform
  1. Windows
Hi,

Code mentioned below has an issue to it. I have two columns ( A & B )

A contains unique IDs
B contains values

I'm using a macro with a loop to update the values in B column for respective IDs. However, the condition to stop the loop is not working. The null value condition is working without a problem but the condition after 'OR' operator is not working. Purpose is to stop the loop when there is a blank in column A or has the text "Access Denied" in column B.

Any hep is highly appreciated.

VBA Code:
Set indexSheet = ActiveSheet
Sheets("Update").Select
irow = 2
A1 = 2
Do While indexSheet.Cells(irow, 1).Value <> vbNullString Or indexSheet.Cells(irow, "B").Value <> "Not Authorized"

' codes to run in the loop is here (I use SENDKEYS)

irow = irow + 1
A1 = A1 + 1
Loop
On Error GoTo 0
[/CODE
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hi
VBA Code:
Do While indexSheet.Cells(irow, 1).Value <> 0 Or indexSheet.Cells(irow, "B").Value <> "Not Authorized"
 
Upvote 0
Hi
VBA Code:
Do While indexSheet.Cells(irow, 1).Value <> 0 Or indexSheet.Cells(irow, "B").Value <> "Not Authorized"
Thank you for the response. The first check is working fine with vbNullString. The issue is with the second one "Not Authorized".

Is it connected somewhat to that vbNullString and hence not checking the second condition ? I'll try with 0 in the first check, but want to understand what's going wrong there.
 
Upvote 0
so what about
VBA Code:
Do While (indexSheet.Cells(irow, 1).Value <> vbNullString And indexSheet.Cells(irow, "B").Value = "Access Denied") = False

"Access Denied" OR "Not Authorized"
 
Upvote 0
Solution
Alternatively, if there is some code that can be used to skip the row if the value contains 'Not Authorized' and just move to next row and continue with the loop.

Ex: if there are 10 rows with IDs and value. And macro is looping through each row and executing the command. Row 7 with ID and value, the value is 'Not Authorized'. The loop should skip that row and jump to 8th row and continue the loop.
 
Upvote 0
so what about
VBA Code:
Do While (indexSheet.Cells(irow, 1).Value <> vbNullString And indexSheet.Cells(irow, "B").Value = "Access Denied") = False

"Access Denied" OR "Not Authorized"
I'll probably use 'Not Authorized'. But this code has the AND Operator which means it takes both the things into consideration before exiting the loop. It should be either 1st one or second one. If one of these is true then it should exit the loop.
 
Upvote 0
But this code has the AND Operator which means it takes both the things into consideration before exiting the loop. It should be either 1st one or second one. If one of these is true then it should exit the loop.
Are you sure that is what happens?
with this code
VBA Code:
Sub xxxxx()
    Dim indexSheet As Worksheet, irow As Long, A1xx As Long
    Set indexSheet = ActiveSheet
    'Sheets("Update").Select
    irow = 2
    A1xx = 2
    Do While indexSheet.Cells(irow, 1).Value <> vbNullString And indexSheet.Cells(irow, "B").Value <> "Not Authorized"

        ' codes to run in the loop is here (I use SENDKEYS)

        irow = irow + 1
        A1xx = A1xx + 1
    Loop
    MsgBox indexSheet.Cells(irow, 1).Row
End Sub
For me it stops at row 9 with the below

1671265792670.png


and at row 3 with the below

1671265875253.png
 
Upvote 0
Are you sure that is what happens?
with this code
VBA Code:
Sub xxxxx()
    Dim indexSheet As Worksheet, irow As Long, A1xx As Long
    Set indexSheet = ActiveSheet
    'Sheets("Update").Select
    irow = 2
    A1xx = 2
    Do While indexSheet.Cells(irow, 1).Value <> vbNullString And indexSheet.Cells(irow, "B").Value <> "Not Authorized"

        ' codes to run in the loop is here (I use SENDKEYS)

        irow = irow + 1
        A1xx = A1xx + 1
    Loop
    MsgBox indexSheet.Cells(irow, 1).Row
End Sub
For me it stops at row 9 with the below

View attachment 81105

and at row 3 with the below

View attachment 81106
Trying that now.

Is it possible to do the below instead? Not stop but skip if there are more values after 'Not Authorized'?

Alternatively, if there is some code that can be used to skip the row if the value contains 'Not Authorized' and just move to next row and continue with the loop.

Ex: if there are 10 rows with IDs and value. And macro is looping through each row and executing the command. Row 7 with ID and value, the value is 'Not Authorized'. The loop should skip that row and jump to 8th row and continue the loop.
 
Upvote 0
Is it possible to do the below instead?
Maybe...
VBA Code:
    Dim myCell As Range
    For Each myCell In indexSheet.Range("B2:B" & indexSheet.Range("A" & Rows.Count).End(xlUp).Row)
        If LCase(myCell.Value) <> "not authorized" Then MsgBox "Do something here"
    Next
 
Upvote 0
Maybe...
VBA Code:
    Dim myCell As Range
    For Each myCell In indexSheet.Range("B2:B" & indexSheet.Range("A" & Rows.Count).End(xlUp).Row)
        If LCase(myCell.Value) <> "not authorized" Then MsgBox "Do something here"
    Next
Tried it. It's not stopping/skipping at "Not Authorized" in B Column or blank cell in A column.
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,820
Members
449,049
Latest member
cybersurfer5000

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top