简体   繁体   中英

Check Each Value In Range On Last Row [VBA]

I've got a sheet set up to get the contents of the last row. I want to check the values on that last row from J to W. I want to check if all the values are "YES" and if so return an OK into a variable. Here is what I have so far, it should be clear from the below what I am trying to do:

lastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
sName = ActiveSheet.Name

For Each c In Worksheets(sName).Range(Cells(J, lastRow), Cells(W, lastRow))
    If c.Value = "YES" Then
       vData = "OK"
    Else
        vData = "Error."
    End If
Next c

Thanks.

Cells(x,y) takes two integers as arguments, and it's row, column not column, row!

Try

For Each c In Sheets(sName).Range(Cells(lastRow, 10), Cells(lastRow, 23))
Dim lRow As Long
Dim lCol As Long
Dim ws As Excel.Worksheet

Set ws = Application.ActiveSheet
lRow = ws.UsedRange.Rows.count

lCol = 10
Do While lCol <= 21
    If ws.Cells(lRow, lCol).Value <> "YES" Then
        vData = "Error."
        Exit Sub
    End If
    lCol = lCol + 1
Loop

Try this one:

Public Sub checking()

    Dim lastRow As Long

    'Here, I take row count by using column "J"
    'You can modify it if you need
    lastRow = Sheets("sheetname").Range("J" & Rows.Count).End(xlUp).row

    For Each cell In Sheets("sheetname").Range("J" & lastRow & ":W" & lastRow)

        If cell.Value = "YES" Then

            vData = "OK"

        Else

            vData = "Error."

            Exit For

        End If

    Next cell

    'Show result
    MsgBox vData

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM