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

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
Vba Microsoft Visual Basic运行时错误91_Vba_Excel - Fatal编程技术网

Vba Microsoft Visual Basic运行时错误91

Vba Microsoft Visual Basic运行时错误91,vba,excel,Vba,Excel,当它到达Cells.Find时,它会给出一个运行时错误“91” “未设置对象变量或带块变量” 始终在模块中使用选项Explicit,并声明所有变量类型 始终使用变量限定对象 直接使用对象(避免。选择或。激活) 在Find调用中,语法已关闭。无需将变量w括在括号中 (1到3是最佳实践,但不是必需的。忽略某些内容可能会产生意外的结果) 很好的提示,我发现通过遵守前三个提示,与不使用选项Explicit或回避相比,您可以学到更多关于VBA和如何使用VBA的知识。选择。说得好@BruceWayne.)这

当它到达Cells.Find时,它会给出一个运行时错误“91” “未设置对象变量或带块变量”

  • 始终在模块中使用
    选项Explicit
    ,并声明所有变量类型
  • 始终使用变量限定对象
  • 直接使用对象(避免
    。选择
    。激活
  • Find
    调用中,语法已关闭。无需将变量
    w
    括在括号中
  • (1到3是最佳实践,但不是必需的。忽略某些内容可能会产生意外的结果)


    很好的提示,我发现通过遵守前三个提示,与不使用
    选项Explicit
    或回避
    相比,您可以学到更多关于VBA和如何使用VBA的知识。选择
    。说得好@BruceWayne.)这是唯一高亮显示一个单元格还是所有单元格的选项?也就是说,当我运行宏时,它只选择12:00:00 AM单元格中的一个。不完全是,它只选择1。您是否需要它在12:00:00 AM时选择所有单元格?并突出显示?是的,我需要更改什么?在找到单元格后,是否有方法将单元格格式设置为日期
    
    Sub find_highlight()
    
    w = "12:00:00 AM"
    
    Cells.Find(What:=(w), After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False).Activate
     Range("B:B").Select
    With Selection.Interior
        .ColorIndex = 6
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
      End With
    End Sub
    
    Option Explicit
    
    Sub find_highlight()
    
    Dim ws As Worksheet
    Set ws = Sheets("Sheet4") 'change as needed
    
    Dim w As String 'maybe Date?
    w = "12:00:00 AM"
    
    Dim rng As Range, sFA  As String, rngFull As Range
    
    Set rng = ws.Cells.Find(What:=w, After:=ActiveCell, LookIn:=xlFormulas, _
               LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
               MatchCase:=False)
    
    sFA = rng.Address
    
    Do 'load all cells where w exists into range
    
        If Not rng Is Nothing And rngFull Is Nothing Then
            Set rngFull = rng
        ElseIf Not rng Is Nothing Then
            Set rngFull = Union(rng, rngFull)
        End If
    
        Set rng = ws.Cells.FindNext(rng)
    
    Loop Until rng Is Nothing Or rng.Address = sFA
    
    'highlight all cells found
    With rngFull.Interior
        .ColorIndex = 6
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
    End With
    
    rngFull.NumberFormat = "mm/dd/yyyy" 'format as date -> change as needed
    
    
    End Sub