检查最后一行范围内的每个值

我有一张表格来获取最后一行的内容。 我想检查最后一行从J到W的值。我想检查所有的值是否为“是”,如果是的话,返回一个OK到一个variables。 这是我到目前为止,从下面应该清楚我正在尝试做什么:

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 

谢谢。

Cells(x,y)以两个整数作为参数,它是行,列而不是列,行!

尝试

 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 

试试这个:

 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