Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 在列和选项中查找特定值_Excel_Vba - Fatal编程技术网

Excel 在列和选项中查找特定值

Excel 在列和选项中查找特定值,excel,vba,Excel,Vba,我只是想检查一下我的代码是否有问题。我的代码试图在A列中查找这个值1234,一旦找到它,如果满足条件,它将选择它旁边的值。如果没有1234,那么它将一事无成。不幸的是,即使1234在A列中,它也不会执行任何操作。当我删除Else语句时,它能够按照正常方式运行。我可以知道它有什么毛病吗 Sub testr1() Dim vCell As Range Dim otherrow As Long otherrow = 1 Do Set vCell = Sheets(She

我只是想检查一下我的代码是否有问题。我的代码试图在A列中查找这个值1234,一旦找到它,如果满足条件,它将选择它旁边的值。如果没有1234,那么它将一事无成。不幸的是,即使1234在A列中,它也不会执行任何操作。当我删除Else语句时,它能够按照正常方式运行。我可以知道它有什么毛病吗

Sub testr1()

Dim vCell As Range
Dim otherrow As Long

otherrow = 1

    Do

        Set vCell = Sheets(Sheet1").Cells(otherrow, 1)

        If vCell = 1234 Then
            If Range("B5") = "B" Then
                Cells(otherrow, 2).Select
            End If
        Else
        'Do nothing if 1234 is N.A.
        Exit Do
        End If
        otherrow = otherrow + 1
   Loop

End Sub
请看一看。它解释了如何选择单元及其偏移

将您从上述答案中学到的知识应用于注释为“找到匹配,做点什么”的for each循环中。您可以删除该注释,或只是在其下方的
If
End If
之间添加代码,这表明存在匹配项

Sub testr1()

    Dim myRange As Range
    
    ' change this to any range you like to check
    Set myRange = Range("A1:A100")
    
    
    Dim searchTerm As String
    ' specify your search term
    searchTerm = "1234"
    
    Dim cell As Range
    ' cell will be each cell in your range
    For Each cell In myRange
        ' checks whether the cell matches the searchTerm
        If StrComp(cell, searchTerm, vbTextCompare) = 0 Then
            ' match found do something
            
        End If
    Next
End Sub
此代码遍历
myRange
变量中的所有单元格。在上面的示例中,范围设置为
A1
A100
,因此循环中的
单元格
变量将是第一次迭代的
A1
,第二次迭代的
A2
,依此类推,直到
A100

StrComp()
函数将每个单元格的值与示例中的
1234
搜索项进行比较

match found do something
是我希望您应用我在上面提供的链接的答案中的逻辑的地方


您的原始但修订的代码

Sub testr1()

Dim vCell As Range
Dim otherrow As Long

otherrow = 1

    Do

        Set vCell = Sheets("Sheet1").Cells(otherrow, 1)

        If vCell = 1234 Then
            If Range("B5") = "B" Then
                Cells(otherrow, 2).Select
            End If
        End If
        otherrow = otherrow + 1
   Loop Until IsEmpty(vCell)

End Sub
去掉else语句。如果未找到
1234
,则退出循环。因此,如果第一个单元格不等于1234,则退出循环,它不会移动到第二行

此外,还需要在循环中添加一个布尔语句,以防止出现错误。我添加了
循环,直到isEmpty(vCell)
,让循环知道一旦有一个空单元格终止循环。还有其他更好的方法,但如果您不想太多地修改原始代码,那么这就足以避免无限循环

你的代码实际上在做什么

我一直在问自己,你想用你的代码实现什么,我似乎不能给出一个很好的理由。修改后的代码(上面的代码)将遍历列A中的所有单元格,直到找到一个空的单元格。它将查找与你的<代码> 1234代码>匹配的代码(这应该是一个字符串,而不是一个独立的数字——考虑用双引号包装它)。一旦找到匹配项,它将检查相应行上的列
B
中是否有
B
值。如果它这样做,那么它将选择该单元格

逻辑失败的地方

由于只会选择最后的1234及其对应的
B
,因此迭代整个列的意义是什么?这对我来说毫无意义。试着更好地解释你想要达到的目标

解决方案 根据最新的评论,我编辑了代码以满足标准

Sub testr1()

Dim vCell As Range
Dim otherrow As Long

otherrow = 1

    Do

        Set vCell = Sheets("Sheet1").Cells(otherrow, 1)

        If StrComp(vCell, "1234", vbTextCompare) = 0 Then
            If StrComp(vCell.Offset(0, 1), "B", vbTextCompare) = 0 Then
                vCell.Offset(0, 1).Select
                ' if all criterias are met then the cell will be highlighted in red
                vCell.Offset(0,1).Interior.Color = RGB(255, 0, 0)
            End If
        End If
        otherrow = otherrow + 1
   Loop Until IsEmpty(vCell)

End Sub

您需要使用范围对象(
vCell
)的
.Offset(0,1)
属性来选择相应的单元格(右侧的一个)

特别感谢@mehow给了我一个让代码工作的想法

Sub testr1()
Dim vCell As Range
Dim otherrow As Long

otherrow = 1

    Do While otherrow <= Rows.Count

        Set vCell = Sheets("Sheet1").Cells(otherrow, 1)

         If vCell = 1234 Then
            If Range("B5") = "B" Then
                Cells(otherrow, 2).Select
            End If
        Exit Do
        End If
        otherrow = otherrow + 1

        Loop

End Sub
subtestr1()
调暗vCell作为范围
把另一排排排得一样长
otherrow=1

当其他行运行时,为什么循环?你可以使用
.AutoFilter
.Find/.FindNext
@SiddharthRout我需要找到1234,然后在它旁边做一个选择,或者在它后面做几个列,我想这可能就是方法。我不太确定如何进行自动筛选或.find
我不太确定如何进行自动筛选或.find
我已经在我之前的评论中为您提供了一个板块上的链接。你需要仔细阅读并理解它们。如果你是初学者,那么你不能在大约7分钟内做到:)@SiddharthRout对不起,我完全遗漏了“示例”。。我现在就看一看有没有可能我没有使用搜索来定位1234的单元格?当我试图定位1234以外的单元格时,它看起来很复杂。在我的代码中,我使用do循环来尝试同时执行这两个操作time@user1204868对不起,我很难理解你。你能更努力地解释你的意思吗?例如,我需要在A列中查找1234。一旦我找到它,我将不得不在1234旁边做一个选择,它可以是B列进行说明。换句话说,如果1234位于A12,那么我想选择B12,依此类推。。我希望我已经澄清了这个疑问(非常感谢你的努力!)我无法使用修订的still,因为我的文件中间确实有空列。幸运的是,您在Loop Until IsEmpty(vCell)上的声明给了我一个想法,我找到了一种方法让它工作@用户回答:如果答案是有用的,请考虑接受: