基于date的VBA计数器

我已经创build了一个用户表单来跟踪服务请求,而我在最后一部分遇到问题。

有两列有问题,第一个“A”是服务ID,第二个是“B”date。 我希望“B”是今天的date,“A”从1开始计数,当我明天添加一个新的请求时,我希望“A”再次从1开始。

这里是“A”列的代码,当创build新logging时,“A”上升,但是当date改变时,它不会从0开始。任何想法?

'Service ID 'check row above 'if "Date" then out put "1" 'if yesterday then out put "1" 'if today then ouput + 1 If (Sheet1.Cells(Rows.Count, "B").End(xlUp).Value) = "Date" Then serviceorder = "1" ElseIf (Sheet1.Cells(Rows.Count, "B").End(xlUp).Value) < Date Then serviceorder = "1" ElseIf (Sheet1.Cells(Rows.Count, "B").End(xlUp).Value) = Date Then serviceorder = (Sheet1.Cells(Rows.Count, "A").End(xlUp).Value) + 1 Else End If 

提前致谢。

为什么不把A列作为COUNTIF公式来显示相应的列B单元格中的date在所有列B中出现的次数? 这样当date发生变化时,它会自动重置为1,每当一个请求进入同一天的新时间,它的ID将递增1.VBA公式为:

 With Sheet1.Cells(Rows.Count, "A").End(xlUp).Offset(1) .Formula = "=COUNTIF(B:B,B" & .Row & ")" 'Uncomment this next line if you want column A to contain values instead of formulas 'If you do uncomment it, make sure you have already put the date in the B column for this entry '.Value = .Value End With 

我认为你的函数中的一般问题是Sheet1.Cells(Rows.Count, "B").End(xlUp) 。 Rows.Count意味着 – 就像它说的那样 – 表格中的行数是1048576.在我看来,你拿走了最后一个单元格, End(xlUp)不会让它变得更好。 我认为Sheet1.Cells(1, "B").End(xlUp)应该更好地工作。 对不起,如果我错了,我不能在这里重现你的scheme。