用于计算列中值的VBA代码

时间:2014-02-04 04:14:35

标签: vba count

有谁能告诉我如何为以下实例编写VBA代码? 我每周都有8名学生参加12周的数据。 我已经编写了一个代码,用于在缺席的情况下用红色填充单元格的颜色,在存在的情况下用绿色填充。

现在我必须为每一行中的每个学生添加“缺席”号码,并在下一栏填写该号码。类似地计算每行中个别学生的缺席数量,并在另一列中填写该数字。 最后,我应该计算每个学生的“现在”百分比。

Option Explicit

Sub CopyPaste()
'
' CopyPaste Macro
'

    Sheets("Attendance").Select
    Range("A1:M9").Select
    Selection.Copy
    Sheets("Report").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
End Sub

Sub Filling()
'
' Filling Macro
'

    Dim Cell As Range
    Sheets("Report").Select
    For Each Cell In Range("B2:M9")
    If Cell.Value = "Present" Then
        Cell.Interior.Color = 255
    ElseIf Cell.Value = "Absent" Then
        Cell.Interior.Color = 5287936
    Else
        'do nothing
    End If
    Next Cell
End Sub

Sub Calculate()
'
' Calculate Macro
'
    Sheets("report").Select
    Range("N1").Select
    ActiveCell.FormulaR1C1 = "Total Present"
    Range("N2").Select
    ActiveCell.FormulaR1C1 = "7"
    Range("N3").Select
    ActiveCell.FormulaR1C1 = "8"
    Range("N4").Select
    ActiveCell.FormulaR1C1 = "4"
    Range("N5").Select
    ActiveCell.FormulaR1C1 = "7"
    Range("N6").Select
    ActiveCell.FormulaR1C1 = "8"
    Range("N7").Select
    ActiveCell.FormulaR1C1 = "7"
    Range("N8").Select
    ActiveCell.FormulaR1C1 = "4"
    Range("N9").Select
    ActiveCell.FormulaR1C1 = "7"
    Range("O1").Select
    ActiveCell.FormulaR1C1 = "Total Absent"
    Range("O2").Select
    ActiveCell.FormulaR1C1 = "5"
    Range("O3").Select
    ActiveCell.FormulaR1C1 = "4"
    Range("O4").Select
    ActiveCell.FormulaR1C1 = "8"
    Range("O5").Select
    ActiveCell.FormulaR1C1 = "5"
    Range("O6").Select
    ActiveCell.FormulaR1C1 = "4"
    Range("O7").Select
    ActiveCell.FormulaR1C1 = "5"
    Range("O8").Select
    ActiveCell.FormulaR1C1 = "8"
    Range("O9").Select
    ActiveCell.FormulaR1C1 = "5"
    Range("P1").Select
    ActiveCell.FormulaR1C1 = "Attendance Rate"
    Range("P2").Select
    ActiveCell.FormulaR1C1 = "=(RC[-2]*100)/12"
    Range("P2").Select
    Selection.AutoFill Destination:=Range("P2:P9"), Type:=xlFillDefault
    Range("P2:P9").Select
End Sub

1 个答案:

答案 0 :(得分:1)

将此用作灵感:

Sub countAbsence()
Dim r As Range
Dim c
Set r = Sheets("Sheet1").Range("A1:A8")
c = Application.WorksheetFunction.CountIf(r, "yes")
MsgBox "column A has " & c & " instances of 'yes'"
End Sub

询问您是否需要更多解释。

如果你想把计数和百分比放在一起,你可以这样做:

Range("B1").value = c
Range("C1").value = c / 8
Range("C1").style = "percent"

更新完整代码以获取A2:M9中的表格并生成“现在”,“缺席”和“出勤率”列:

Option Explicit
Option Compare Text

Sub calculcatePresentAbsent()
  Dim absent, present
  Dim i
  Dim record As Range, target As Range

  Set record = Range("B1:M1")
  Set target = Range("N1")
  target.Value = "present"
  target.Offset(0, 1).Value = "absent"
  target.Offset(0, 2).Value = "attendance rate"

  For i = 1 To 8 ' loop over students
    Set record = record.Offset(1, 0)  ' go to next row
    Set target = target.Offset(1, 0)
    absent = Application.WorksheetFunction.CountIf(record, "Absent")
    present = Application.WorksheetFunction.CountIf(record, "Present")
    target.Value = present
    target.Offset(0, 1).Value = absent
    target.Offset(0, 2).Value = present / (present + absent)
    target.Offset(0, 2).Style = "percent"
  Next

End Sub

这基本上可以完成您发布的代码所做的一切 - 除了它“动态”执行,而不是硬编码。

祝你上课好运。