Excel 如何检查行是否包含某些文本

Excel 如何检查行是否包含某些文本,excel,vba,Excel,Vba,我试图检查活动工作表中名为“Exceptions”的第1行是否包含文本“Control Date”(两个空格)或“Control Date” 我的代码发现条件为false 将a调暗为范围 将异常设置为工作表 设置异常=活动工作簿。工作表(“异常”) 对于异常范围(“A1:Z1”)中的每个c 如果c=“控制日期”,则 Cells.Find(What:=“control date”,After:=ActiveCell,LookIn:=xlFormulas_ LookAt:=xlPart,Search

我试图检查活动工作表中名为“Exceptions”的第1行是否包含文本“Control Date”(两个空格)或“Control Date”

我的代码发现条件为false

将a调暗为范围
将异常设置为工作表
设置异常=活动工作簿。工作表(“异常”)
对于异常范围(“A1:Z1”)中的每个c
如果c=“控制日期”,则
Cells.Find(What:=“control date”,After:=ActiveCell,LookIn:=xlFormulas_
LookAt:=xlPart,SearchOrder:=xlByColumns,SearchDirection:=xlNext_
匹配案例:=False,搜索格式:=False)。激活
其他的
Cells.Find(What:=“Control Date”,After:=ActiveCell,LookIn:=xlFormulas_
LookAt:=xlPart,SearchOrder:=xlByColumns,SearchDirection:=xlNext_
匹配案例:=False,搜索格式:=False)。激活
如果结束
下一个c
“控制日期”中有两个空格的工作表示例
为什么要将数据复制到第三列?每当你需要“组合”日期时,只需去获取它,但不要存储两次

Option Explicit    '<<-- always have this


Sub doFindControl()

    Dim a As Range
    Dim c As Range  '<<-- add this
    Dim colDate As Long, colNumber As Long, colBlank As Long  '<<--add this
    Dim exceptions As Worksheet
    Set exceptions = ActiveWorkbook.Worksheets("Exceptions")
    For Each c In exceptions.Range("A1:Z1")
    
        ' first find the 2 key columns
        If InStr(c, "Control") > 0 Then
        
            If InStr(c, "Date") > 0 Then
                colDate = c.Column
            
            ElseIf InStr(c, "Number") > 0 Then
                colNumber = c.Column
            End If
        ' second look for the first blank column for you to put results in
        ElseIf c.Text = "" Then
                colBlank = c.Column
                Exit For    ' stop looking after its found
        End If
        
    Next c

    ' now you have the 2 FROM columns, and the TO column
    MsgBox (colDate & " " & colNumber & " " & colBlank)
    ' and you can loop thru all the rest of the rows doing combine

End Sub
Option Explicit'如何写入条件
至于检查单元格值是单空格的“控制日期”还是双空格的“控制日期”,有两种方法:

使用类似于
操作符的

这样可以很容易地将字符串与基本模式进行比较。在本例中,使用通配符
*
(零个或多个字符)将返回true,而不管
Control
Date
之间有多少空格

如果cell.Value2类似于“Control*Date”,则
“用那间牢房做点什么
如果结束
使用
运算符
也可以使用ok-to-use,尽管它不是一个灵活的选项,并且可能更难看到您的特定示例中发生了什么

如果cell.Value2=“控制日期”或cell.Value2=“控制日期”,则
“用那间牢房做点什么
如果结束

工作表代码名 每个工作表都有一个称为。这是一个引用,可以在代码中通过其名称直接调用该特定工作表

要设置此名称,请在“属性”窗口中更新
name
属性

所以不是

Dim异常作为工作表
设置异常=活动工作簿。工作表(“异常”)
对于Exceptions.Range(“A1:Z1”)中的每个单元格
“做点什么。。。
下一个细胞
您可以直接调用工作表引用

异常范围(“A1:Z1”)中的每个单元格的

“做点什么。。。
下一个细胞

把它放在一起
  • 我不想对变量使用
    c
    ,而是想让变量更容易阅读和理解,所以我使用了
    cell
  • 此外,您可以循环整个第一行的单元格,而不是在范围中硬编码标题列。不过,这是一个选择建议
  • 最后,更明确地说明您在
    单元格中查找的属性。在我的示例中,我使用来显示我正在查找该单元格的值
公共子演示()
暗淡单元格作为范围
对于Exceptions.Rows(1)中的每个单元格。单元格
如果cell.Value2类似于“控制*日期”,则
“用那间牢房做点什么
如果结束
下一个细胞
端接头


谢谢你的回答!罗伯特·托达的回答让我想到了我的灯泡时刻,我不敢相信答案是如此简单。我所要做的就是更改此代码:

Cells.Find(What:="Control Date", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
致:


你能否澄清一下,你的问题中有两次“控制日期”
包含文本“控制日期”或“控制日期”
当c=“控制日期”时,为什么再次搜索几乎相同的文本?请与我们共享正在搜索的列标题的文本,以便我们也能看到数据。很抱歉,为了清晰起见,我编辑了这篇文章。@donPablo搜索是这样的:我的宏选择带有控制日期的列,创建一个新列,然后用控制日期和控制编号填充该新列。目前,在这些类型的工作表上运行宏之前,我必须删除control date之间的额外空格,但我不想再处理这个问题。请注意,第16列和第17列的标题单元格中有其他文本。在起始
*
字符中添加类似“*control*date”的If cell.Value2然后
就可以忽略前面的文本了
Cells.Find(What:="Control*Date", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate