我正在尝试在Excel VBA中构建一个宏,该宏具有一组任务的输入以及每个任务的执行方式。每个任务有多个实例。
我想对每个任务的过期次数进行计数。过期之前的时间取决于任务,因此我添加了一个输入框,以便用户可以为每个任务输入该时间。然后,对于每个任务,宏应检查多少个实例花费了比此输入更长的时间。
因此,例如,如果任务1的3个实例分别花费了2天,3天和4天,而我输入的预期时间为2天,则应该返回任务的2个实例已过期。
我的代码正在输出看似合理的数字,但是当我手动检查时,它们是错误的-通常比实际数字低得多。我曾尝试确保所有内容都是整数,以防万一,但问题并未改变。谁能帮助我找出问题所在或当前输出的代码?预先感谢!
Sub Macro1()
Dim AshRow As Integer
Dim EweRow As Integer
Dim lastCell As Long
Dim all As Range
Dim allRows As Range
Dim count As Integer
Dim origTasks As Range
'Previously tried Dim expTime As Variant
Dim expTime As Integer
Dim m As Integer
Dim task As String
EweRow = Sheets("Sheet1").UsedRange.Rows.count
With Sheets("Sheet1")
lastCell = Cells(Rows.count, "A").End(xlUp).Row
Set all = Range("A1", Range("A" & lastCell))
End With
Set origTasks = Worksheets("Page1_1").Range("A2", "A" & EweRow)
'For each task, cycle through each row of the original report and note anything over the given value
Set allRows = Range("A2", Range("A" & lastCell))
For Each r In allRows.Cells
count = 0
task = r.Value
expTime = InputBox("Please enter the expected time for the task: " & task)
'Check this is an integer and make it one if not
If Not IsNumeric(expTime) Then
expTime = CInt(expTime)
End If
For Each n In origTasks.Cells
If n.Value = task Then
m = n.Offset(0, 1).Value
'Check value is an integer
If Not IsNumeric(m) Then
m = CInt(m)
End If
If m > expTime Then
count = count + 1
End If
End If
Next n
r.Offset(0, 4).Value = count
Next r
'Format
Worksheets("Sheet1").Range("A1:E1").Font.Bold = True
End Sub