Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.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 VBA。查找范围异常_Excel_Find_Solution_Vba - Fatal编程技术网

Excel VBA。查找范围异常

Excel VBA。查找范围异常,excel,find,solution,vba,Excel,Find,Solution,Vba,“发现了一个有趣的例子——在我把头发扯掉4个小时后 如果第一列的宽度对于使用的字体大小来说太窄,Excel 2010 VBA似乎无法在跨单元格合并的范围内找到日期值。(这类似于Excel VBA无法在隐藏的行/列中找到日期值) 3种可能的解决方案:最佳优先 将LookIn参数更改为xlFormulas 加宽列,直到宏使用LookIn:=xlValues 减小字体大小,直到宏使用LookIn:=xlValues 复制步骤: 在A2中插入日期(如7/3) 跨4列合并(A2:D2)-这是要查找的日期的

“发现了一个有趣的例子——在我把头发扯掉4个小时后

如果第一列的宽度对于使用的字体大小来说太窄,Excel 2010 VBA似乎无法在跨单元格合并的范围内找到日期值。(这类似于Excel VBA无法在隐藏的行/列中找到日期值)

3种可能的解决方案:最佳优先

  • 将LookIn参数更改为xlFormulas
  • 加宽列,直到宏使用LookIn:=xlValues
  • 减小字体大小,直到宏使用LookIn:=xlValues
  • 复制步骤:

  • 在A2中插入日期(如7/3)
  • 跨4列合并(A2:D2)-这是要查找的日期的字段
  • 在单元格A4:A35(例如1/3到31/3)中创建一组连续日期
  • 跨4列合并(A4:D35)
  • 运行以下代码:

    Sub findDate()
    Dim myRange As Range
    Dim myDate As Date
    Dim myFindDate As Date
    Dim myRow As Integer
    
    With ActiveSheet
    
        Set myRange = .[A2]
    
        myFindDate = .[A4:D35].Value
    
        On Error Resume Next
    
        myRow = myRange.Find( _
            what:=myFindDate, _
            LookIn:=xlValues, _
            LookAt:=xlWhole, _
            SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, _
            MatchCase:=False, _
            SearchFormat:=False).Row
    
        On Error GoTo 0
    
        If myRow <> 0 Then
            MsgBox "The date is in row number = " & myRow
        Else
            MsgBox "Column A too narrow.  Either use LookIn:=xlFormulas, widen Column A or reduce the font size."
        End If
    
    End With
    
    End Sub
    
    Sub findDate()
    将myRange变暗为Range
    将myDate设置为日期
    暗myFindDate作为日期
    将myRow设置为整数
    使用ActiveSheet
    设置myRange=[A2]
    myFindDate=[A4:D35]。值
    出错时继续下一步
    myRow=myRange.Find(_
    什么:=myFindDate_
    LookIn:=xlValues_
    看:=xlother_
    搜索顺序:=xlByRows_
    SearchDirection:=xlNext_
    MatchCase:=假_
    SearchFormat:=False)。行
    错误转到0
    如果我的行是0,那么
    MsgBox“日期在行号中=”&myRow
    其他的
    MsgBox“A列太窄。请使用LookIn:=xlFormulas、加宽A列或减小字体大小。”
    如果结束
    以
    端接头
    
    请注意,消息框显示了相关的行号

    现在将A列的宽度减少到2.4,然后再次运行代码

    请注意生成的消息框:Excel VBA不再能够找到日期

    以上是解决方案1的代码:

    Sub findDate()
    Dim myRange As Range
    Dim myDate As Date
    Dim myFindDate As Date
    Dim myRow As Integer
    
    With ActiveSheet
    
        Set myRange = .[A2]
    
        myFindDate = .[A4:D35].Value
    
        On Error Resume Next
    
        myRow = myRange.Find( _
            what:=myFindDate, _
            LookIn:=xlFormulas, _
            LookAt:=xlWhole, _
            SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, _
            MatchCase:=False, _
            SearchFormat:=False).Row
    
        On Error GoTo 0
    
        If myRow <> 0 Then
            MsgBox "The date is in row number = " & myRow
        Else
            MsgBox "Column A too narrow.  Either use LookIn:=xlFormulas, widen Column A or reduce the font size."
        End If
    
    End With
    
    End Sub
    
    Sub findDate()
    将myRange变暗为Range
    将myDate设置为日期
    暗myFindDate作为日期
    将myRow设置为整数
    使用ActiveSheet
    设置myRange=[A2]
    myFindDate=[A4:D35]。值
    出错时继续下一步
    myRow=myRange.Find(_
    什么:=myFindDate_
    LookIn:=xl公式_
    看:=xlother_
    搜索顺序:=xlByRows_
    SearchDirection:=xlNext_
    MatchCase:=假_
    SearchFormat:=False)。行
    错误转到0
    如果我的行是0,那么
    MsgBox“日期在行号中=”&myRow
    其他的
    MsgBox“A列太窄。请使用LookIn:=xlFormulas、加宽A列或减小字体大小。”
    如果结束
    以
    端接头
    
    (唯一的更改是LookIn参数:xlFormulas而不是xlValues)

    如果运行第二位代码,消息框将再次显示行号

    “希望这能帮别人减轻我的痛苦

    加里

    我遵循了你的“复制步骤”说明,你的例子对我不起作用

    不过我注意到了一些事情

    Dim myDate As Date
    Dim myFindDate As Date
    Dim myRow As Integer
    
    这些值可能是日期,但您正在使用范围。 所以正确地启动代码

     Dim myRange As Range, myFindDate As Range, myRow As Range
    
    然后正确设置范围

     Set myRange = [A2]
     Set myFindDate = [A4:D35]
     Set myRow = myFindDate.Find(what:=myRange, lookat:=xlWhole)
    
    以这种方式使用代码,不管列有多宽

    完整的代码

    Sub findDateB()
        Dim myRange As Range, myFindDate As Range, myRow As Range
    
        Set myRange = [A2]
        Set myFindDate = [A4:D35]
        Set myRow = myFindDate.Find(what:=myRange, lookat:=xlWhole)
    
        If Not myRow Is Nothing Then
            MsgBox "The date is in row number = " & myRow.Row
        Else: MsgBox "Not Found"
            Exit Sub
        End If
    
    End Sub