我有一张包含 15 列(包括日期列)的 Excel 工作表。是否可以将其复制到另一张工作表,该工作表仅显示包含今天日期的列/数据?或者显示星期几?
(每天滚动查找数据以找到日期非常困难。)
答案1
我想向您建议两种不同的方法。
一是一个数组公式,并且第二是 VBA 代码,用于过滤与当前日期匹配的记录。
{=IFERROR(INDEX(Sheet1!$A$4:$D$10, SMALL(IF(COUNTIF(Sheet1!$E$4, Sheet1!$A$4:$A$10), ROW(Sheet1!$A$4:$D$10)-MIN(ROW(Sheet1!$A$4:$D$10))+1), ROW(A1)), COLUMN(A1)),"")}
注意:
E4 是写有今日日期的单元格。使用以下公式完成公式Ctrl+Shift+Enter然后向右拖动直到所需的列,然后向下拖动。
如果您有大量数据范围最好使用此 VBA 代码。
Sub CopyRowWithCurrentDate()
Dim xRgS As Range, xRgD As Range, xCell As Range
Dim I As Long, xCol As Long, J As Long
Dim xVal As Variant
On Error Resume Next
Set xRgS = Application.InputBox("Select the Date Column:", "Filter On Date", Selection.Address, , , , , 8)
If xRgS Is Nothing Then Exit Sub
Set xRgD = Application.InputBox("Select a destination cell:", "Filter On Date", , , , , , 8)
If xRgD Is Nothing Then Exit Sub
xCol = xRgS.Rows.Count
Set xRgS = xRgS(1)
Application.CutCopyMode = False
J = 0
For I = 1 To xCol
Set xCell = xRgS.Offset(I - 1, 0)
xVal = xCell.Value
If TypeName(xVal) = "Date" And (xVal <> "") And (xVal = Date) Then 'You should manipulate the days you are filtering by typing accurate days!
xCell.EntireRow.Copy xRgD.Offset(J, 0)
J = J + 1
End If
Next
Application.CutCopyMode = True
End Sub
希望这对你有帮助。