excel - .FIND 找不到日期
问题描述
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
解决方案
您可以检查您正在查找的数据的值和类型以及搜索区域中的数据,然后根据需要转换您正在查找的数据。在第一个选项中,搜索不同类型的数据,并且Date
(考虑语言环境)未找到。在第二个变体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'
推荐阅读
- ios - 如何在 iOS 14 中更改 TabBar 位置
- docker - Nginx+Tomcat+Docker 组成 SSL
- java - 在 Eclipse 中使用 Gradle 在构建路径中使用其他项目
- logging - 如何审查/隐藏 HOCON 值使其不会出现在 Ktor 日志中
- angular - 可观察到的竞争条件 - 代码执行得太快
- python - 使用 BERT 或 LSTM 模型在大型文档中实现语义相似度的最佳方法
- javascript - 我如何读取 html 文件并获取数据?(不在浏览器中)
- css - 对齐输入字段(每行 2 个) - VueJS
- javascript - 如何将此json数据转换为js中的键值对数组?
- javascript - 如何在Angular8的Highcharts工具提示中呈现角度指令