我正在编写一个宏,它将读取一列日期,检查每个项目的月份,在变量中记录数字,并将最终数字放在一个单元格中。该计划正在查看2999项的列。该列未完全填满。选择该数字是因为项目数量可能很高。
for each循环检查每个单元格的月份,并将该月份的变量增加1.但最后,12月变量大约为2500.只有大约500个填充行。月份总和达到2999(被搜索的行数)。我不确定为什么会这样做。我尝试添加“其他”条件。它甚至没有达到那么远。 A15永远不会充满“蝙蝠侠”。我不得不错过一些简单的东西。帮助
Dim rng3 As Range
Dim JanCount As Integer
Dim FebCount As Integer
Dim MarCount As Integer
Dim AprCount As Integer
Dim MayCount As Integer
Dim JunCount As Integer
Dim JulCount As Integer
Dim AugCount As Integer
Dim SepCount As Integer
Dim OctCount As Integer
Dim NovCount As Integer
Dim DecCount As Integer
Dim dateValue As Date
Dim monthInt As Integer
Set rng3 = Sheets("Sheet2").Range("K2:K3000")
For Each cell In rng3
dateValue = cell.Value
monthInt = month(dateValue)
If monthInt = 1 Then
JanCount = JanCount + 1
ElseIf monthInt = 2 Then
FebCount = FebCount + 1
ElseIf monthInt = 3 Then
MarCount = MarCount + 1
ElseIf monthInt = 4 Then
AprCount = AprCount + 1
ElseIf monthInt = 5 Then
MayCount = MayCount + 1
ElseIf monthInt = 6 Then
JunCount = JunCount + 1
ElseIf monthInt = 7 Then
JulCount = JulCount + 1
ElseIf monthInt = 8 Then
AugCount = AugCount + 1
ElseIf monthInt = 9 Then
SepCount = SepCount + 1
ElseIf monthInt = 10 Then
OctCount = OctCount + 1
ElseIf monthInt = 11 Then
NovCount = NovCount + 1
ElseIf monthInt = 12 Then
DecCount = DecCount + 1
Else
Sheets("Sheet1").Range("A15") = "Batman"
End If
Next cell
Sheets("Sheet1").Range("A10") = "Bugs in Jan"
Sheets("Sheet1").Range("B10") = "Bugs in Feb"
Sheets("Sheet1").Range("C10") = "Bugs in Mar"
Sheets("Sheet1").Range("D10") = "Bugs in Apr"
Sheets("Sheet1").Range("E10") = "Bugs in May"
Sheets("Sheet1").Range("F10") = "Bugs in Jun"
Sheets("Sheet1").Range("G10") = "Bugs in Jul"
Sheets("Sheet1").Range("H10") = "Bugs in Aug"
Sheets("Sheet1").Range("J10") = "Bugs in Oct"
Sheets("Sheet1").Range("K10") = "Bugs in Nov"
Sheets("Sheet1").Range("L10") = "Bugs in Dec"
Sheets("Sheet1").Range("A11") = JanCount
Sheets("Sheet1").Range("B11") = FebCount
Sheets("Sheet1").Range("C11") = MarCount
Sheets("Sheet1").Range("D11") = AprCount
Sheets("Sheet1").Range("E11") = MayCount
Sheets("Sheet1").Range("F11") = JunCount
Sheets("Sheet1").Range("G11") = JulCount
Sheets("Sheet1").Range("H11") = AugCount
Sheets("Sheet1").Range("I11") = SepCount
Sheets("Sheet1").Range("J11") = OctCount
Sheets("Sheet1").Range("K11") = NovCount
Sheets("Sheet1").Range("L11") = DecCount
答案 0 :(得分:5)
尝试首先检查IsDate()
是否可以将单元格值解释为日期。
而且......只是建议让整个事情变得更容易一些:
Dim Rng3 As Range
Dim BugCount(1 To 12)
Set Rng3 = Sheets("Sheet2").Range("K2:K3000")
For Each cell In Rng3
If IsDate(cell.Value) Then
BugCount(Month(cell.Value)) = BugCount(Month(cell.Value)) + 1
End If
Next
For monthctr = 1 To 12
Sheets("Sheet1").Range("A10").Offset(0, monthctr - 1).Value = "Bugs in " & Format(DateSerial(2013, monthctr, 1), "MMM")
Sheets("Sheet1").Range("A11").Offset(0, monthctr - 1).Value = BugCount(monthctr)
Next
答案 1 :(得分:1)
如果您的单元格为空,则单元格的日期值为0.日期值0定义为1900年1月0日,因此您将获得1作为month()函数的结果。 首先检查cell.Value是否为空。
问候 斯蒂芬
答案 2 :(得分:0)
由于某种原因,看起来空白单元格的月份总是返回为12。尝试将12月行更改为:
ElseIf monthInt = 12 And cell.value <> "" Then
答案 3 :(得分:-1)
替换
行Set rng3 = Sheets("Sheet2").Range("K2:K3000")
带
Set rng3 = Sheets("Sheet2").Range("K2:K" & Sheets(2).Cells(Rows.Count, 11).End(xlUp).Row)
基本上是 S. Wirth 说,这是因为空白细胞。所以在上面的代码中,不是让它循环到3000,而是让它直到任何数据都存在。
希望这有帮助。