VBA检查范围内的值

VBA检查范围内的值,vba,excel,if-statement,Vba,Excel,If Statement,我试图在一列中循环,如果cells=我要查找的内容,则执行一些操作。 到目前为止,我在if语句中检查名称: Option Explicit Sub test() Dim wksDest As Worksheet Dim wksSource As Worksheet Dim rngSource As Range Dim name As String Dim LastRow

我试图在一列中循环,如果cells=我要查找的内容,则执行一些操作。 到目前为止,我在if语句中检查名称:

Option Explicit

Sub test()

Dim wksDest             As Worksheet
Dim wksSource           As Worksheet

Dim rngSource           As Range

Dim name                 As String

Dim LastRow             As Long
Dim LastCol             As Long
Dim c                   As Long

Application.ScreenUpdating = False

Set wksSource = Worksheets("Sheet1")

With wksSource
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
For c = 16 To 20
LastRow = .Cells(.Rows.Count, c).End(xlUp).Row
Set rngSource = .Range(.Cells(5, 16), .Cells(LastRow, 16))
name = rngSource.Value

If name = "mark"
do something 
End If

Next c
End With

Application.ScreenUpdating = True

'MsgBox "Done!", vbExclamation

End Sub

我猜您真正想做的是在范围rngSource中循环。所以试试看


我猜您真正想做的是在范围rngSource中循环。所以试试看

好的,克里斯 可能需要一些简化,但也需要一些假设。 LastCol似乎没有被用于任何用途——因此,让我们假设这是要循环通过的列。 您的循环有固定的开始和结束值,但您正在确定最后一行-因此,假设您希望从代码中的第5行开始,并循环到最后一行。 为了确定LastCol,您必须在用于确定LastCol的行中包含数据——因此,假设在代码中,从第1行到第16列的所有列中都有值。 如果要在这种情况下测试单个字符串值,则必须将rngSource设置为单个单元格值。除非需要再次使用,否则也不需要将其分配给变量。 最后,如果您想检查其他值,您可能需要考虑使用SELECT CASE结构代替IF结构。 看看下面的内容,改变我的假设以满足您的要求-祝您好运

Sub test()

Dim wksDest             As Worksheet
Dim wksSource           As Worksheet

Dim rngSource           As Range

Dim name                 As String

Dim LastRow             As Long
Dim LastCol             As Long
Dim c                   As Long

Application.ScreenUpdating = False

Set wksSource = Worksheets("Sheet1")

    With wksSource
        LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
        LastRow = .Cells(Rows.Count, LastCol).End(xlUp).Row

        FirstRow = 5
            For c = FirstRow To LastRow
                If .Range(.Cells(c, LastCol), .Cells(c, LastCol)).Value = "Mark" Then
                    MsgBox ("do something")
                End If
            Next c
    End With

End Sub
好的,克里斯 可能需要一些简化,但也需要一些假设。 LastCol似乎没有被用于任何用途——因此,让我们假设这是要循环通过的列。 您的循环有固定的开始和结束值,但您正在确定最后一行-因此,假设您希望从代码中的第5行开始,并循环到最后一行。 为了确定LastCol,您必须在用于确定LastCol的行中包含数据——因此,假设在代码中,从第1行到第16列的所有列中都有值。 如果要在这种情况下测试单个字符串值,则必须将rngSource设置为单个单元格值。除非需要再次使用,否则也不需要将其分配给变量。 最后,如果您想检查其他值,您可能需要考虑使用SELECT CASE结构代替IF结构。 看看下面的内容,改变我的假设以满足您的要求-祝您好运

Sub test()

Dim wksDest             As Worksheet
Dim wksSource           As Worksheet

Dim rngSource           As Range

Dim name                 As String

Dim LastRow             As Long
Dim LastCol             As Long
Dim c                   As Long

Application.ScreenUpdating = False

Set wksSource = Worksheets("Sheet1")

    With wksSource
        LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
        LastRow = .Cells(Rows.Count, LastCol).End(xlUp).Row

        FirstRow = 5
            For c = FirstRow To LastRow
                If .Range(.Cells(c, LastCol), .Cells(c, LastCol)).Value = "Mark" Then
                    MsgBox ("do something")
                End If
            Next c
    End With

End Sub

传递值以查找需要检查值的列。如果找到的else返回0,它将返回row num

Function checkForValue(FindString As String,ColumnToCheck as String) As Long
        SheetLastRow = Sheets("Sheet1").Cells.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).row
         With Sheets("Sheet1").Range("$" & ColumnToCheck & "$1:$" & ColumnToCheck & "$" & CStr(SheetLastRow) ) 
                Set rng = .Find(What:=FindString, _
                                After:=.Cells(.Cells.Count), _
                                LookIn:=xlValues, _
                                lookat:=xlWhole, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlNext, _
                                MatchCase:=False)
                If Not rng Is Nothing Then
                    checkForValue = rng.row 'return row its found
                    'write code you want.                   
                Else
                    checkForValue = 0
                End If
            End With
End Function

传递值以查找需要检查值的列。如果找到的else返回0,它将返回row num

Function checkForValue(FindString As String,ColumnToCheck as String) As Long
        SheetLastRow = Sheets("Sheet1").Cells.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).row
         With Sheets("Sheet1").Range("$" & ColumnToCheck & "$1:$" & ColumnToCheck & "$" & CStr(SheetLastRow) ) 
                Set rng = .Find(What:=FindString, _
                                After:=.Cells(.Cells.Count), _
                                LookIn:=xlValues, _
                                lookat:=xlWhole, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlNext, _
                                MatchCase:=False)
                If Not rng Is Nothing Then
                    checkForValue = rng.row 'return row its found
                    'write code you want.                   
                Else
                    checkForValue = 0
                End If
            End With
End Function

你只需要一行就可以做到

If Not IsError(Application.Match(ValueToSearchFor, RangeToSearchIn, 0)) Then
'The value found in the given range
End If
例如: 在名为Country的工作表的C列中搜索加拿大

If Not IsError(Application.Match("Canada", Sheets("Country").Range("C:C"), 0)) Then
   'The value found in the given range
End If

你只需要一行就可以做到

If Not IsError(Application.Match(ValueToSearchFor, RangeToSearchIn, 0)) Then
'The value found in the given range
End If
例如: 在名为Country的工作表的C列中搜索加拿大

If Not IsError(Application.Match("Canada", Sheets("Country").Range("C:C"), 0)) Then
   'The value found in the given range
End If

我试过Hari的建议,但是Application.Match在不识别区域名称的情况下工作得很奇怪

已更改为:WorksheetFunction.Match。。。 它可以工作,但当值不存在时,运行时错误会在IsError之前跳转。。。进行了评估。 因此,我必须编写一个简单的-无循环-解决方案:

dim Index as Long

Index = -1
On Error Resume Next
  Index = WorksheetFunction.Match(Target,Range("Edificios"), 0) 'look for Target value in range named: Edificios
On Error GoTo 0
If Index > 0 Then
    ' code for existing value found in Range @ Index row
End If
请记住Excel函数的第一个索引=1不以零为基础


希望这能有所帮助。

我尝试了Hari的建议,但Application.Match在无法识别范围名称的情况下工作异常

已更改为:WorksheetFunction.Match。。。 它可以工作,但当值不存在时,运行时错误会在IsError之前跳转。。。进行了评估。 因此,我必须编写一个简单的-无循环-解决方案:

dim Index as Long

Index = -1
On Error Resume Next
  Index = WorksheetFunction.Match(Target,Range("Edificios"), 0) 'look for Target value in range named: Edificios
On Error GoTo 0
If Index > 0 Then
    ' code for existing value found in Range @ Index row
End If
请记住Excel函数的第一个索引=1不以零为基础


希望这有帮助。

你可以用它来实现你想要的……你可以用它来实现你想要的……非常感谢!是的,case语句更适合我所做的,因为我要检查的是多个值。rngSource在您的修订版中不再使用,所以我将其删除。现在我只需要弄清楚每次检查时我想要它做什么。非常感谢!是的,case语句更适合我所做的,因为我要检查的是多个值。rngSource在您的修订版中不再使用,所以我将其删除。现在我只需要弄清楚每次检查时我希望它做什么。