简体   繁体   English

尽管值相同,为什么这张表上的日期不突出显示

[英]why isn't the dates on this sheet highlighting although the value is the same

I've got a macro code to highlight cells in a sheet where the value comes from another sheet upon a button click on a separate sheet but it is returning value can't be found/none found when the value on both sheets is actually the same. 我有一个宏代码来突出显示工作表中的单元格,当单击单独的工作表上的按钮时,该值来自另一个工作表,但是当两个工作表上的值实际上是相同。

The value of the cell is a date value. 单元格的值是日期值。

the 1st is the intended sheet and the 2nd one is the code 第一个是预期的工作表,第二个是代码

intended sheet to highlight cells 旨在突出显示单元格的表

Sub HighlightSpecificValue()
    Dim fnd As String, FirstFound As String
    Dim FoundCell As Range, rng As Range
    Dim myRange As Range, LastCell As Range
    fnd = Range("H9").Value
    Sheets("PO copy").Select
    Set myRange = ActiveSheet.UsedRange
    Set LastCell = myRange.Cells(myRange.Cells.Count)
    Set FoundCell = myRange.Find(what:=fnd, after:=LastCell)
    If Not FoundCell Is Nothing Then
        FirstFound = FoundCell.Address
    Else
        GoTo NothingFound
    End If  
    Set rng = FoundCell
    Do Until FoundCell Is Nothing
        Set FoundCell = myRange.FindNext(after:=FoundCell)
        Set rng = Union(rng, FoundCell)
        If FoundCell.Address = FirstFound Then Exit Do
    Loop
    rng.Interior.Color = RGB(255, 255, 0)
    Exit Sub
NothingFound:
    MsgBox "No cells containing: " & fnd & " were found in this worksheet"
End Sub

A couple of things need to be fixed: 有几件事需要修复:

  • You shouldn't try to compare a String to a cell containing a date - so it will be best to define fnd as a Variant 您不应该尝试将String与包含日期的单元格进行比较-因此最好将fnd定义为Variant
  • You are currently not specifying whether to look at values or formulas when doing the Find , or whether to look at part or the whole of the value - you should explicitly define those in order to avoid confusion due to Excel using whatever the user last used 当前,您未指定在执行“ Find时是否查看值或公式,还是查看值的一部分或全部-您应明确定义这些值,以免由于Excel使用用户上次使用的内容而造成混淆

I believe the following, slightly modified, code should work: 我相信以下经过稍微修改的代码应该可以工作:

Sub HighlightSpecificValue()
    Dim fnd As Variant, FirstFound As String
    Dim FoundCell As Range, rng As Range
    Dim myRange As Range, LastCell As Range
    fnd = Range("H9").Value
    Sheets("PO copy").Select
    Set myRange = ActiveSheet.UsedRange
    Set LastCell = myRange.Cells(myRange.Cells.Count)
    Set FoundCell = myRange.Find(what:=fnd, _
                                 after:=LastCell, _
                                 LookIn:=xlValues, _
                                 LookAt:=xlWhole)
    If Not FoundCell Is Nothing Then
        FirstFound = FoundCell.Address
    Else
        GoTo NothingFound
    End If  
    Set rng = FoundCell
    Do Until FoundCell Is Nothing
        Set FoundCell = myRange.FindNext(after:=FoundCell)
        Set rng = Union(rng, FoundCell)
        If FoundCell.Address = FirstFound Then Exit Do
    Loop
    rng.Interior.Color = RGB(255, 255, 0)
    Exit Sub
NothingFound:
    MsgBox "No cells containing: " & fnd & " were found in this worksheet"
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM