如果日期是今天,我有这个代码用于汇总多行。它工作正常,但我有几个工作表,例如...工作表1,2,3,4,5。如果我在工作表1中输入数据然后运行此代码它工作正常但如果我在sheet1中输入数据然后跳过sheet2然后转到3它就是在sheet2中的零。如果日期小于今天,我会注释掉底部附近的行以退出sub,但这样做代码就会死掉。如何仅在当前使用的工作表上运行此操作?我想忽略所有其他表格。
Sub Sum_TodaysDate()
On Error Resume Next
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Worksheets
Dim LastRow As Long, iCount As Long
Dim icell As Range
Dim dSplit As Variant
Dim dIndex As Date
LastRow = sh.Range("D" & Rows.Count).End(xlUp).Row
iCount = 0
For Each icell In sh.Range("D2:D" & LastRow)
dSplit = Split(icell.Value, " ")
dIndex = Format(dSplit(0), "mm/dd/yyyy")
If dIndex = Date Then
iCount = iCount + 1
icell.Offset(0, 1).Value = "|"
End If
Next icell
'If sh.Range("E" & LastRow).Value < Date Then Exit Sub
sh.Range("E" & LastRow).Value = iCount
sh.Range("E" & LastRow).Font.Color = vbRed
Next sh
Application.ScreenUpdating = False
End Sub
答案 0 :(得分:1)
替换:
For Each sh In ActiveWorkbook.Worksheets
使用:
Set sh = ActiveSheet
然后删除:
Next sh