【问题标题】:.FIND not finding the date.FIND 找不到日期
【发布时间】:2021-06-28 01:34:47
【问题描述】:

vartype(Date_var) 为 8,即 Vbstring。 Newsheet.Range("B5") 的 vbtype 为 7,即 vbdate,值如下。 Date_var = 20211207,Newsheet.Range("B5") = 07/12/2021 并在 numberformat 之后将其转换为 20211207。虽然存在日期,但 .Find 无法获取列号。我尝试将 date_var 转换为 Cdate 并且我得到类型不匹配。非常感谢任何帮助。

    Date_var = Worksheets("sheet1").Range("AF1:AL1000").Cells(1, 4).Value
    
    If Date_var = "" And Lastrow = 1 Then
       Exit Sub
    End If
    
    With Newsheet.Range("B5:BL5")
       .NumberFormat = "yyyymmdd"
        Set Columnfind = .Find(What:=Date_var, LookIn:=xlValues, lookat:=xlPart, 
        MatchCase:=False, SearchFormat:=False)
        If Columnfind Is Nothing Then
           colnum = 0
        Else
           colnum = Columnfind.Column
      End If
     
    End With

【问题讨论】:

  • 设置NumberFormat 只会改变显示的内容。 Find 搜索值,而不是显示的文本。

标签: excel vba


【解决方案1】:

您可以检查您正在查找的数据的值和类型以及搜索区域中的数据,然后在必要时转换您正在查找的数据。第一个选项,搜索不同类型的数据,没有找到Date(考虑locale)。在第二个变体中,Date 被转换为 String(考虑语言环境)并且搜索成功。

选项 1

Sub test1()
    Date_vars_array = Array(20211207#, 20211207, "20211207", #12/7/2021#)   ' or #7/12/2021# for your locale
    
    For Each Date_var In Date_vars_array
        With NewSheet.Range("B5:BL5")
            .NumberFormat = "yyyymmdd"
            Set Columnfind = .Find(What:=Date_var, LookIn:=xlValues, lookat:=xlWhole, _
                MatchCase:=False, SearchFormat:=False)
            If Columnfind Is Nothing Then
               colnum = 0
            Else
                 colnum = Columnfind.Column
            End If
        End With
        Debug.Print ".Finding for 'Date_var = " & Date_var & "' which has '" & TypeName(Date_var) & "' data type:"
        Debug.Print vbTab & "'NewSheet.Range(""B5"") = " & NewSheet.Range("B5").Value & "' and has '" & TypeName(NewSheet.Range("B5").Value) & "' data type"
        Debug.Print vbTab & "'colnum = " & colnum & "'" & vbLf
    Next
End Sub

输出 1

.Finding for 'Date_var = 20211207' which has 'Double' data type:
    'NewSheet.Range("B5") = 07.12.2021' and has 'Date' data type
    'colnum = 2'

.Finding for 'Date_var = 20211207' which has 'Long' data type:
    'NewSheet.Range("B5") = 07.12.2021' and has 'Date' data type
    'colnum = 2'

.Finding for 'Date_var = 20211207' which has 'String' data type:
    'NewSheet.Range("B5") = 07.12.2021' and has 'Date' data type
    'colnum = 2'

.Finding for 'Date_var = 07.12.2021' which has 'Date' data type:
    'NewSheet.Range("B5") = 07.12.2021' and has 'Date' data type
    'colnum = 0'

选项 2(如果 TypeName(Date_var) = "Date" Then Date_var = Format(Date_var, "yyyymmdd") 添加)

Sub test1()
    Date_vars_array = Array(20211207#, 20211207, "20211207", #12/7/2021#)   ' or #7/12/2021# for your locale
    
    For Each Date_var In Date_vars_array
        With NewSheet.Range("B5:BL5")
            .NumberFormat = "yyyymmdd"
            
            If TypeName(Date_var) = "Date" Then Date_var = Format(Date_var, "yyyymmdd")
            
            Set Columnfind = .Find(What:=Date_var, LookIn:=xlValues, lookat:=xlWhole, _
                MatchCase:=False, SearchFormat:=False)
            If Columnfind Is Nothing Then
               colnum = 0
            Else
                 colnum = Columnfind.Column
            End If
        End With
        Debug.Print ".Finding for 'Date_var = " & Date_var & "' which has '" & TypeName(Date_var) & "' data type:"
        Debug.Print vbTab & "'NewSheet.Range(""B5"") = " & NewSheet.Range("B5").Value & "' and has '" & TypeName(NewSheet.Range("B5").Value) & "' data type"
        Debug.Print vbTab & "'colnum = " & colnum & "'" & vbLf
    Next
End Sub

输出 2

.Finding for 'Date_var = 20211207' which has 'Double' data type:
    'NewSheet.Range("B5") = 07.12.2021' and has 'Date' data type
    'colnum = 2'

.Finding for 'Date_var = 20211207' which has 'Long' data type:
    'NewSheet.Range("B5") = 07.12.2021' and has 'Date' data type
    'colnum = 2'

.Finding for 'Date_var = 20211207' which has 'String' data type:
    'NewSheet.Range("B5") = 07.12.2021' and has 'Date' data type
    'colnum = 2'

.Finding for 'Date_var = 20211207' which has 'String' data type:
    'NewSheet.Range("B5") = 07.12.2021' and has 'Date' data type
    'colnum = 2'

旁注:查找搜索文本

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-06
    • 1970-01-01
    • 2017-03-30
    • 2015-11-10
    相关资源
    最近更新 更多