【问题标题】:How to find the row of specific Month and Year in the Column如何在列中找到特定月份和年份的行
【发布时间】:2017-08-18 20:58:23
【问题描述】:

我有一个小问题,下面的pay out date 列的日期格式为dd/mm/yyyy。在那我想找到特定月份和年份的行 - 例如包含 10-2017 的行。

Pay out Date
12-05-2016
18-05-2016
22-06-2016
20-07-2016
24-08-2016
21-09-2016
19-10-2016
23-11-2016
21-12-2016
18-01-2017
22-02-2017
22-03-2017
19-04-2017
24-05-2017
21-06-2017
19-07-2017
23-08-2017
20-09-2017
18-10-2017
22-11-2017
20-12-2017
24-01-2018
21-02-2018
24-03-2018

在普通 excel Ctrl+F 中并给出月份和年份 (10-2017) 准确找到它是哪一行,但通过代码不起作用 - 任何建议都将不胜感激

Sub find()
Dim c1 As Range
Dim cd As Long
 Set c = ThisWorkbook.Sheets(1).Range("A2:A60").find(What:="10-2017", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)

cd = c.Row

  Debug.Print (cd)

End Sub

【问题讨论】:

  • VBA 会将这些视为长格式,而不是您在工作表中看到的日期格式。您需要在代码中考虑到这一点
  • @Tom 嗨,当我按下 Ctrl +F 给出月份和年份时,它找到了行 - 现在我必须做什么

标签: vba excel


【解决方案1】:

这将查找包含在一个月中的日期

Public Sub FindDateInRange()
    Dim rng As Range
    Dim dat As String
    Dim begMonth As Long, endMonth As Long
    Dim c

    Set rng = ThisWorkbook.Sheets(1).Range("A2:A60")

    dat = "10-2017"

    begMonth = DateSerial(Split(dat, "-")(1), Split(dat, "-")(0), 1)
    endMonth = Application.WorksheetFunction.EoMonth(begMonth, 0)

    For Each c In rng
        If c.Value2 >= begMonth And c.Value2 <= endMonth Then
            Debug.Print c.Row
        End If
    Next c
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-20
    • 2012-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多