1

行を合計し、最後の列に合計を入れる必要がある出席スプレッドシートがあります。各行は従業員を表し、各列はその月の日を表します。私が VBA を使用している理由は、一部の日付列にテキスト (Tardy の TA など) が含まれ、TA が範囲内の 1 つ以上のセルに存在する場合は合計に 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

範囲内の 1 つ以上のセルに TA が存在する場合は、合計に 0.5 を追加します。

私は混乱しています。「TA」ごとに .5 を追加しますか、それとも TA が見つかった場合に 1 回だけ追加しますか。一度だけの場合は、PART A他の方法を参照してくださいPART B

パート A

これにはVBAが必要ですか?列が固定されていBAFtotal常に ColAGにある場合、これは単純な 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

列と行で合計するには、1 つではなく 2 つのループを使用する必要があります。私にとって、このような通常のシートを実行する最も簡単な方法は、指定された範囲にオフセットを使用することです。解決策は次のとおりです。

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 のみをカウントするようになりました (IF ステートメントはそう言っています) が、必要なものをカウントするように簡単に変更できるようになりました。

于 2013-01-28T23:11:22.137 に答える