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

时间:2015-07-21 12:09:50

标签: excel vba excel-vba

我已经设置了一张表来获取最后一行的内容。我想检查从J到W的最后一行的值。我想检查所有值是否为"是"如果是,则将OK返回到变量中。以下是我到目前为止,从下面应该清楚我要做的事情:

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

感谢。

3 个答案:

答案 0 :(得分:4)

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

尝试

For Each c In Sheets(sName).Range(Cells(lastRow, 10), Cells(lastRow, 23))

答案 1 :(得分:2)

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

答案 2 :(得分:1)

试试这个:

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