根据当前日期在Excel日历中查找特定的行和单元格

问题描述 投票:-1回答:1

没有运气在网上找到解决方案,我想知道这里是否有人可以帮助我。以下是我的日历示例。 Calendar

以上是我的日历,包括月份,周数和最高日期。在最左边有名字。我想知道当天有单身格F的人的名字(如果4月13日是今天)。日期有DD.MM.YYYY格式。

详细解释:我正在寻找一个代码,在第3行找到今天的日期。当找到该日期时,查找W列中包含“F”的所有单元格。然后我想要该行中的人的名字。

最后是一个糟糕的插图形象,我在想什么。 enter image description here

输出应该看起来像这样。所有名称不应列在一个单元格中。 output

希望得到一些帮助! -J

excel excel-vba vba
1个回答
0
投票

如果您的第3行的日期类似于DD / MM / YYYY,但您将格式更改为自定义并且仅包含DD,则以下内容将起作用:

Sub foo()
Dim LastRow As Long
Dim LastCol As Long
Dim NextEmptyRow As Long
Dim SearchDate As String

LastRow = Sheet1.Cells(Sheet1.Rows.Count, "D").End(xlUp).Row
'SearchDate = Format(Now(), "dd/mm/yyyy")
'change the line below with the one above to search for today's date
SearchDate = "13/04/2017" 'find "today's" date
LastCol = Sheet1.Cells(3, Sheet1.Columns.Count).End(xlToLeft).Column 'get the last Column
Sheet2.Cells(1, 1).Value = "Output"
For i = 1 To LastCol 'loop through all columns
ValueToFind = Format(Sheet1.Cells(3, i).Value, "dd/mm/yyyy") 'format the values on row 3 to full date as dd/mm/yyy
    If ValueToFind = SearchDate Then 'if date on Row 3 matches SearchDate then
        For x = 4 To LastRow 'loop through rows
            If Sheet1.Cells(x, i).Value = "F" Then 'if F exists on that column
                NextEmptyRow = Sheet2.Cells(Sheet2.Rows.Count, "A").End(xlUp).Row + 1 'check the next free row on Sheet 2 column A
                Sheet2.Cells(NextEmptyRow, 1).Value = Sheet1.Cells(x, 4).Value 'copy the name to Sheet2 on the next available Row
            End If
        Next x
    End If
Next i
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.