反击问题VBA

时间:2017-06-29 22:24:57

标签: vba excel-vba excel

挣扎着这个柜台......

我正在浏览多个信息页面并尝试计算过期任务的数量,然后将此信息插入统计信息页面。

Sub data_input_overdue()

Dim rw As Long
Dim Counter As Long

Dim col As Long
col = CountMyCols("Stats")

Worksheets("Stats").Cells(2, col + 1).Value = "Overdue"

Counter = 0


For Each sht In ThisWorkbook.Sheets

    For i = 2 To CountMyRows(sht.Name)
        c_date = Range("E" & i)
        dueDate = CDate(c_date)
        If dueDate < Date And sht.Range("I" & i).Value = "No" Then
        Counter = Counter + CLng(1)
        Worksheets("Stats").Cells(i, col + 1).Value = Counter
    End If
    Next i
Next sht
End Sub

Public Function CountMyRows(SName As String) As Long
On Error Resume Next
With ThisWorkbook.Worksheets(SName)
    CountMyRows = .Cells.Find(What:="*", _
                              After:=.Range("A1"), _
                              Lookat:=xlPart, _
                              LookIn:=xlFormulas, _
                              SearchOrder:=xlByRows, _
                              SearchDirection:=xlPrevious, _
                              MatchCase:=False).Column

它不计算并进入桌面的正确位置。我希望它从第3行开始进入专栏。 在多张表中,有完成和未完成任务的混合

1 个答案:

答案 0 :(得分:1)

我认为这是您要做的事情,但我不确定您将如何使用适用的表单识别每个总数。

Sub data_input_overdue()
    Dim c_date
    Dim dueDate As Date
    Dim i As Long
    Dim Counter As Long
    Dim cntSheet As Long

    Dim col As Long
    col = CountMyCols("Stats")

    Worksheets("Stats").Cells(2, col + 1).Value = "Overdue"

    cntSheet = 0

    For Each sht In ThisWorkbook.Worksheets
        If sht.Name <> "Stats" Then ' Don't process the Stats sheet
            cntSheet = cntSheet + 1
            'Reset counter at the start of each sheet
            Counter = 0
            For i = 2 To CountMyRows(sht.Name)
                c_date = sht.Range("E" & i)
                dueDate = CDate(c_date)
                If dueDate < Date And sht.Range("I" & i).Value = "No" Then
                    Counter = Counter + 1
                End If
            Next i
            'Update Stats sheet after finished counting
            Worksheets("Stats").Cells(2 + cntSheet, col + 1).Value = Counter
        End If
    Next sht

End Sub

Public Function CountMyRows(SName As String) As Long
    On Error Resume Next
    With ThisWorkbook.Worksheets(SName)
        CountMyRows = .Cells.Find(What:="*", _
                                  After:=.Range("A1"), _
                                  Lookat:=xlPart, _
                                  LookIn:=xlFormulas, _
                                  SearchOrder:=xlByRows, _
                                  SearchDirection:=xlPrevious, _
                                  MatchCase:=False).Row
    End With
End Sub

Public Function CountMyCols(SName As String) As Long
    On Error Resume Next
    With ThisWorkbook.Worksheets(SName)
        CountMyCols = .Cells.Find(What:="*", _
                                  After:=.Range("A1"), _
                                  Lookat:=xlPart, _
                                  LookIn:=xlFormulas, _
                                  SearchOrder:=xlByColumns, _
                                  SearchDirection:=xlPrevious, _
                                  MatchCase:=False).Column
    End With
End Sub

如果我这样做,我可能会将工作表名称放在A列中,将总数放在B列中,或者从A列中的工作表名称开始,然后使用该工作表名称来决定计算时要处理的工作表要放在该行上的总数(而不是按Tab键顺序循环遍历所有工作表)。