如何在Excel中使用VBA汇总Range中的每一行

我有一个考勤电子表格,我需要汇总一行,并在最后一列有总计。 每一行代表一名雇员,每列表示一个月中的一天。 我使用VBA的原因是因为某些date列将包含文本,例如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 I = 1到LastRow步骤1围绕For Each循环,并做…..直到LastRow与任何成功。 我的行总是从第3行开始,所以我需要沿着以下几行:

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

直到最后一行

你应该使用两个循环,而不是一个 – 以cols和行的总和。 对我来说,最简单的方法是在给定的范围内使用偏移量。 这是解决scheme:

 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声明是这样说的),但现在你可以很容易地修改它,来计算需要的东西。

如果TA存在于一个或多个范围内的细胞中,则加总0.5。

我很困惑。 您是否想为每个“TA”添加.5,或者如果findTA,您只需添加一次。 如果只是一次,则参见PART A PART B参见下面的PART B

第一部分

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

只要在单元格AG3input此公式,并将其复制下来

 =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