gpt4 book ai didi

vba - Excel 找不到日期(无论格式选项如何)

转载 作者:行者123 更新时间:2023-12-04 21:16:40 25 4
gpt4 key购买 nike

我的老板保存了一个小的 Excel 文件,它模仿了我们用来跟踪工作中随机事物的台历,例如小时数和发送报告的数量。它以 7x5 的网格布局(大约 5 个,最后一行只有几个单元格,因为月份通常不能被 7 整除)。
日期和此类工作的方式是第一个单元格 A1 具有该月的第一个日期。随后的每个单元格都由我的老板手动编码,以获取其前面的瓦片中日期单元格的值并加 1。但是,这些单元格不是并排的,它们是展开的。我试图让它总结每周的总小时数(和报告),但是当我尝试调用 Cells.Find方法,它不返回任何东西。
我想这可能是因为单元格以 mm/dd/yy 的美国方式格式化。 ,所以我尝试搜索05/12/14它没有用。所以我尝试搜索 Date() 的默认日期格式。函数返回,m/d/yyyy ,它仍然没有找到任何东西。我尝试设置 .Find()检查值和公式,两者都不起作用。即使我使用 ctrl+F手动找到它们,它告诉我工作表中没有这样的值。
我四处搜索,发现的帖子都没有有效的解决方案 - Cells.Find方法总是返回 A1 ,第一次调用,或Nothing在随后的每次通话中。
我的代码目前很少,但在这里。
代码

 Sub Worksheet()
Dim r As Range, d As Date, fwkn As Integer, lwkn As Integer, wkn As Integer, col As Collection
'The purpose of this is to get the week totals using the below functions.

d = DateValue([A1].Value)
fwkn = WeekNum(d) 'First week number
d = DateSerial(Year(d), month(d) + 1, 1) - 1 'Gets us the last day of the month
lwkn = WeekNum(d) 'Last Week Number
last_of_month = Day(d)
For i = 1 To last_of_month

d = DateSerial(Year(d), month(d), i)
Set r = Cells.Find(What:=d, LookIn:=xlFormulas, LookAt:=xlWhole, After:=Range("a1"))
If Not r Is Nothing Then
Debug.Print r.Address
End If

Next

End Sub
Function WeekNum(d As Date) As Integer
' You can see examples of this at
' http://www.cpearson.com/excel/weeknum.htm
WeekNum = CInt(Format(d, "ww", vbMonday))
End Function
有谁知道为什么 Excel 找不到日期?

最佳答案

这是使用循环查找日期的示例。日期有多种格式,包括文本:

Sample Dates on Sheet

代码:

Option Explicit
Sub FindDatesLooping()
'Some date in A1
Dim R As Range, C As Range
Dim D As Date
Dim I As Long

Set R = ActiveSheet.UsedRange
D = CDate([A1])

For I = 1 To Day(DateSerial(Year(D), Month(D) + 1, 0))
D = DateSerial(Year(D), Month(D), I)
For Each C In R
If D = C Then
Debug.Print C.Address, D
End If
Next C
Next I
End Sub

在立即窗口中返回:
$C$3          5/3/2014 
$E$5 5/15/2014
$D$7 5/20/2014
$A$1 5/28/2014
$G$8 5/31/2014

即时窗口中的日期采用美国格式 m/d/yyyy;但如果我更改区域设置,即时窗口日期将以任何格式返回(并且所有五个日期仍会返回)。

关于vba - Excel 找不到日期(无论格式选项如何),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23897313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com