Excel - 使用 VBA 根据今天的日期跳转到特定列
Excel - jump to a specific column according to today's date using VBA
我尝试将光标调整到 excel 工作表的匹配日期列。在我看来,所有 'Find' 或 'Match' 功能都无法正常工作。
我正在使用 Excel 2007。
日期值在 R10:HU10 范围内,格式如下:DD.MM.YYYY,用户定义的格式(也尝试了日期和文本 - 没有区别)。今天的日期在单元格 (CI10) 中。
工作表 window 冻结在第 'Q' 列 - 日期条目开始前的最后一列。
我试图完成的是向右滚动,因此 'CI10' 列将紧挨着 'Q'
列
出于测试原因,我尝试使用以下 VBA 代码为特定列着色的解决方案:
Private Sub Worksheet_Activate()
Dim TodaysDate As Date
Dim Rng As Range
TodaysDate = Date
With Rows("10:10")
Set Rng = .Find(what:=TodaysDate, _
after:=.Cells(.Cells.Count), _
LookIn:=xlFormulas, _
lookat:=xlWhole, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, MatchCase:=False)
If Not Rng Is Nothing Then
Rng.EntireColumn.Interior.Color = vbMagenta
Else
'Give a message that today's date was not found
MsgBox "Nothing found"
End If
End With
End Sub
它没有用,因为 Rng 将永远是 'Nothing'。
任何帮助将不胜感激,尤其是有关系统日期和搜索范围内日期条目之间正确比较方法的提示
我这样做了,效果很好。
dateRange 可能类似于 "A1:H1",当前数据输入单元格 H1,当 运行.
时脚本跳转到单元格 H1
Sub jumpToDate()
Dim c As Range
Dim d As Date
d = Date
For Each c In Range("dateRange")
If c = d Then
c.Select
End If
Next c
End Sub
我想这就是您要找的,只是看了标题。
我正在使用非 English/excel 日期格式 (dd/mm/YYYY),这通常让我头疼工作日期格式,但 excel 无论如何都设法正确处理日期
我建议您在单元格的值中搜索日期,而不是在用于计算单元格的公式中搜索。
因此将您的 LookIn:=xlFormulas
更改为 LookIn:=xlValues
。
Private Sub Worksheet_Activate()
Dim TodaysDate As Date
Dim Rng As Range
TodaysDate = Date
With Rows("10:10")
Set Rng = .Find(what:=TodaysDate, _
after:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
lookat:=xlWhole, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Rng.EntireColumn.Interior.Color = vbMagenta
'Set the window so that the date is in the top-left corner
Application.GoTo Rng, True
Else
'Give a message that today's date was not found
MsgBox "Nothing found"
End If
End With
End Sub
我尝试将光标调整到 excel 工作表的匹配日期列。在我看来,所有 'Find' 或 'Match' 功能都无法正常工作。
我正在使用 Excel 2007。
日期值在 R10:HU10 范围内,格式如下:DD.MM.YYYY,用户定义的格式(也尝试了日期和文本 - 没有区别)。今天的日期在单元格 (CI10) 中。
工作表 window 冻结在第 'Q' 列 - 日期条目开始前的最后一列。
我试图完成的是向右滚动,因此 'CI10' 列将紧挨着 'Q'
列出于测试原因,我尝试使用以下 VBA 代码为特定列着色的解决方案:
Private Sub Worksheet_Activate()
Dim TodaysDate As Date
Dim Rng As Range
TodaysDate = Date
With Rows("10:10")
Set Rng = .Find(what:=TodaysDate, _
after:=.Cells(.Cells.Count), _
LookIn:=xlFormulas, _
lookat:=xlWhole, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, MatchCase:=False)
If Not Rng Is Nothing Then
Rng.EntireColumn.Interior.Color = vbMagenta
Else
'Give a message that today's date was not found
MsgBox "Nothing found"
End If
End With
End Sub
它没有用,因为 Rng 将永远是 'Nothing'。
任何帮助将不胜感激,尤其是有关系统日期和搜索范围内日期条目之间正确比较方法的提示
我这样做了,效果很好。 dateRange 可能类似于 "A1:H1",当前数据输入单元格 H1,当 运行.
时脚本跳转到单元格 H1Sub jumpToDate()
Dim c As Range
Dim d As Date
d = Date
For Each c In Range("dateRange")
If c = d Then
c.Select
End If
Next c
End Sub
我想这就是您要找的,只是看了标题。
我正在使用非 English/excel 日期格式 (dd/mm/YYYY),这通常让我头疼工作日期格式,但 excel 无论如何都设法正确处理日期
我建议您在单元格的值中搜索日期,而不是在用于计算单元格的公式中搜索。
因此将您的 LookIn:=xlFormulas
更改为 LookIn:=xlValues
。
Private Sub Worksheet_Activate()
Dim TodaysDate As Date
Dim Rng As Range
TodaysDate = Date
With Rows("10:10")
Set Rng = .Find(what:=TodaysDate, _
after:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
lookat:=xlWhole, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Rng.EntireColumn.Interior.Color = vbMagenta
'Set the window so that the date is in the top-left corner
Application.GoTo Rng, True
Else
'Give a message that today's date was not found
MsgBox "Nothing found"
End If
End With
End Sub