简体   繁体   English

如何使用Excel VBA有条件地添加和删除表中的某些行

[英]How to use Excel VBA to conditionally add and delete certain rows in a table

I have a table with following columns 我有一个包含以下列的表

Date, Employee name, Task#, Hours, day of the month 日期,员工姓名,任务#,小时,每月的某一天

each row contains information for all the columns. 每行包含所有列的信息。

All I am trying to do is to combine (find SUM of hours) of certain rows based on the following criteria/conditions: 我要做的就是根据以下条件/条件组合(查找小时数的SUM):

add and combine rows when following is true 当follow为true时添加和组合行

  1. has same date 有相同的日期
  2. has same employee name 有相同的员工姓名
  3. rows containing task# starting with "LBR" except "LBR 30", "LBR 28" (there are some more LBR# exceptions as seen in my code) 包含任务#的行以“LBR”开头,“LBR 30”除外,“LBR 28”(我的代码中还有一些LBR#例外)

    OR the rows has only the exception LBR (LBR 30, LBR 28..) 或者行只有例外LBR(LBR 30,LBR 28 ..)

    OR the rows that has only Non LBR task# 或者只有非LBR任务的行#

in other words, for a certain date and employee name, I only want to have rows for three different categories 换句话说,对于某个日期和员工姓名,我只想拥有三个不同类别的行

1) total "LBR" hours (not including exceptions)(determined by column "Task#") 1)总“LBR”小时(不包括例外)(由“任务#”栏确定)

2) total "LBR" hours (only the exceptions once) (determined by column "Task#") 2)总“LBR”小时(仅一次例外)(由“任务#”栏确定)

3) total non "LBR" hours 3)总非“LBR”小时

Example: 例:

Date        Employee            Task    Hrs Dom
6/4/2019,   Wright  , Stephen,  CND05,  30, 4   
6/1/2019,   Wimberly, Robert,   LBR16,  30, 1    
6/1/2019,   Wimberly, Robert,   CNRWK,  30, 1    
6/1/2019,   Wimberly, Robert,   A02060, 30, 1    
6/2/2019,   Wimberly, Robert,   LBR16,  30, 2    
6/2/2019,   Wimberly, Robert,   A03000, 30, 2    
6/2/2019,   Wimberly, Robert,   A03000, 30, 2    
6/2/2019,   Wimberly, Robert,   LBR30,  30, 2    
6/2/2019,   Wimberly, Robert,   LBR28,  30, 2    
6/2/2019,   waja,               A02060  30, 2    
6/2/2019,   waja,               A02060, 30, 2

I want to convert it to this: 我想将其转换为:

Date        Employee            Task    Hrs Dom
6/4/2019,   Wright, Stephen     CND05,  30, 4   
6/1/2019,   Wimberly, Robert,   LBR16,  30, 1    
6/1/2019,   Wimberly, Robert,   CNRWK,  60, 1    
6/2/2019,   Wimberly, Robert,   LBR16,  30, 2    
6/2/2019,   Wimberly, Robert,   A03000, 60, 2    
6/2/2019,   Wimberly, Robert,   LBR 30, 60, 2    
6/2/2019,   waja    ,       ,   A02060, 60, 2

What I did is any two rows with similar date, name, and task category(one of three categories), I took sum of their hours and retained one row (deleted anyone and kept the other)(NOTE: it can be more than two rows that needs to be combined). 我做的是任何两行具有相似的日期,名称和任务类别(三个类别之一),我花了他们的小时数并保留一行(删除任何人并保留另一行)(注意:它可以超过两个)需要组合的行)。

I first sorted the table to according to date, name and task# and implemented the following code 我首先根据日期,名称和任务#对表进行排序,并实现以下代码

Sub SortAndSum()

Dim R As Integer
Dim PR As Integer

Dim wkb As Excel.Workbook
Dim wks As Excel.Worksheet

Set wkb = Excel.Workbooks("LaborHours.xlsx")
Set wks = wkb.Worksheets("srg.hours")

R = 6

Do While wks.Cells(R, 2) <> ""

PR = R - 1
If wks.Cells(R, 1) = wks.Cells(PR, 1) Then
    If wks.Cells(R, 2) = wks.Cells(PR, 2) Then
        If InStr(1, wks.Cells(R, 3) & wks.Cells(PR, 3), "LBR") = 0 Then GoTo SumAndDelete
    ElseIf InStr(1, wks.Cells(R, 3) & wks.Cells(PR, 3), "LBR") = 1 Then
        If wks.Cells(R, 3) & wks.Cells(PR, 3) = "LBR10" Or "LBR28" Or "LBR29" Or "LBR291" Or "LBR293" Or "LBR295" Then GoTo SumAndDelete
        ElseIf wks.Cells(R, 3) & wks.Cells(PR, 3) <> "LBR10" Or "LBR28" Or "LBR29" Or "LBR291" Or "LBR293" Or "LBR295" Then GoTo SumAndDelete

SumAndDelete:
wks.Cells(R, 4) = Cells(R, 4) + Cells(PR, 4)
wks.Rows(PR).Delete

        Else
        GoTo NextRow
        End If

End If


NextRow:
R = R + 1
Loop

End Sub

problem with my code: -I have to run this code multiple times to get my final result. 我的代码存在问题: - 我必须多次运行此代码才能获得最终结果。 - when dealing with LBRs, i get run time error 13, TYPE MISMATCH. - 处理LBR时,我得到运行时错误13,TYPE MISMATCH。 when i click on "Debugg" button, it highlights the following part of the code: 当我点击“Debugg”按钮时,它会突出显示代码的以下部分:

ElseIf wks.Cells(R, 3) & wks.Cells(PR, 3) <> "LBR10" Or "LBR28" Or "LBR29" Or "LBR291" Or "LBR293" Or "LBR295" Then GoTo SumAndDelete

@dwirony . @dwirony。 The third last if statement only determines the possibility of first two category cases (referring back to my description right before my example). 第三个if语句仅确定前两个类别案例的可能性(在我的例子之前回顾我的描述)。 So the last two if statements checks for the first and 2nd category respectively. 所以最后两个if语句分别检查第一和第二类。

Also, I wanted the code to run the last two if statements only when the third last if statement was true. 此外,我希望代码仅在第三个if语句为true时运行最后两个if语句。 Not sure what's actually happening. 不确定实际发生了什么。

@MathieuGuindon @dwirony thanks for your feedback. @MathieuGuindon @dwirony感谢您的反馈。 Any other suggestions? 还有其他建议吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM