1

我有一个出勤电子表格,我需要对一行进行求和,并在最后一列中有总数。每行代表一名员工,每列代表一个月中的一天。我使用 VBA 的原因是因为某些日期列将包含文本,例如 Tardy 的 TA,如果 TA 存在于该范围内的一个或多个单元格中,则需要在总数中添加 0.5。

我只能填充第一行,但不能填充它下面的行。我猜这是因为我没有正确设置我的范围。这是我到目前为止的代码:

Dim wsJAN As Worksheet      'Used to reference the sheet by its TAB name in the WB
Dim LastRow As Long         'Last used row on sheet
Dim tDays As Range          'Total # of days the employee has actually worked
Dim cDays As Range          'Current # of days the employee should have worked
Dim rowEmployee As Range    'Used to define the columns to be used to when adding attendance for each employee row
Dim rCell As Range

LastRow = Cells.Find(What:="*", After:=[A3], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

Application.ScreenUpdating = False

Set wsJAN = ThisWorkbook.Sheets("JAN")
Set tDays = wsJAN.Range("AG3")
Set cDays = wsJAN.Range("AH3")
Set rowEmployee = wsJAN.Range("B3:AF3")

tDays = "=SUM(B3:AF3)"
cDays = "=SUM(B3:AF3)"

For Each rCell In rowEmployee
If rCell.Value = "TA" Then

    tDays = tDays + 0.5    ' Add only a half day to the # of days the employee has worked in Column AG if tardy.
    cDays = cDays + 1      ' Add a whole day to current days worked in Column AH if employee is tardy.
End If
Next


Application.ScreenUpdating = True

我什至尝试在 For Each 循环周围使用 For i = 1 To LastRow Step 1 并 Do.....Loop until LastRow 没有任何成功。我的行总是从第 3 行开始,所以我需要一些类似的东西:

AG3 =SUM(B3:AF3)
AG4 =SUM(B4:AF4)

一直到最后一行

4

2 回答 2

2

如果 TA 存在于范围内的一个或多个单元格中,则将总数加 0.5。

我很困惑。您想为每个“TA”添加 0.5 还是仅在找到 TA 时添加一次。如果只是一次,请参阅PART A其他请参见PART B下文

第一部分

你需要VBA吗?如果你的列是固定的,即BAF总是total在 Col 中,AG那么这可以通过一个简单的 Excel 公式来实现。

只需在单元格中输入此公式AG3并将其复制下来

=SUM(B3:AF3) + IF(COUNTIF(A3:AF3,"TA")>0,0.5,0)

截屏

在此处输入图像描述

如果你仍然想要 VBA,那么你也可以试试这个

Option Explicit

Sub Sample()
    Dim ws As Worksheet
    Dim LRow As Long

    '~~> Change as applicable
    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        '~~> Assuming that the names are in col A
        LRow = .Range("A" & .Rows.Count).End(xlUp).Row

        .Range("AG3:AG" & LRow).Formula = "=SUM(B3:AF3) + IF(COUNTIF(A3:AF3,""TA"")>0,0.5,0)"
    End With
End Sub

B部分

=SUM(B3:AF3)+COUNTIF(A3:AF3,"TA")*0.5

代码

Option Explicit

Sub Sample()
    Dim ws As Worksheet
    Dim LRow As Long

    '~~> Change as applicable
    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        '~~> Assuming that the names are in col A
        LRow = .Range("A" & .Rows.Count).End(xlUp).Row

        .Range("AG3:AG" & LRow).Formula = "=SUM(B3:AF3)+COUNTIF(A3:AF3,""TA"")*0.5"
    End With
End Sub
于 2013-01-29T07:32:55.007 回答
0

您应该使用两个循环而不是一个循环 - 在列和行中求和。对我来说,浏览这种常规工作表的最简单方法是对给定范围使用 Offset。这是解决方案:

Dim wsJAN As Worksheet      'Used to reference the sheet by its TAB name in the WB
Dim LastRow As Long         'Last used row on sheet
Dim tDays As Double          'Total # of days the employee has actually worked
Dim cDays As Integer          'Current # of days the employee should have worked
Dim rowEmployee As Range    'Used to define the columns to be used to when adding attendance for each employee row
Dim rCell As Range
Dim i As Integer

Application.ScreenUpdating = False

Set wsJAN = ThisWorkbook.Sheets("JAN")
Set rowEmployee = wsJAN.Range("B3:AF3")

i = 0
Do While Range("B3").Offset(i).Value <> Empty        ' for each row
    tDays = 0
    cDays = 0
    For Each rCell In rowEmployee          ' for each column
        If rCell.Offset(i).Value = "TA" Then
            tDays = tDays + 0.5    ' Add only a half day to the # of days the employee has worked in Column AG if tardy.
            cDays = cDays + 1      ' Add a whole day to current days worked in Column AH if employee is tardy.
        End If
    Next
    ' set AG as tDays && AH as cDays
    Range("AG3").Offset(i).Value = tDays
    Range("AH3").Offset(i).Value = cDays
    i = i + 1
Loop

Application.ScreenUpdating = True

现在它只计算 TA-s(您的 IF 语句是这样说的),但您现在可以轻松修改它,以计算所需的任何内容。

于 2013-01-28T23:11:22.137 回答