VBA - 计算两列的重复值并对其进行标记

时间:2016-03-18 07:27:16

标签: vba excel-vba excel

我正在执行一个程序,我需要计算来自Column AColumn B的重复值,如果两列中的重复值大于1,则将其标记为两列,第一列列为"selected",第二个为"updated",将在重复值中标记的列是最接近当前日期的日期..

示例:

Column A| Column B| Column C | Column D| Column E|
  1     |  easy   | 1/2/2016 |         |         |
  2     |  normal | 1/3/2016 |         |         |
  2     |  hard   | 1/4/2016 |         |         |
  1     |  easy   | 1/5/2016 |         |         |

输出:

Column A| Column B| Column C | Column D | Column E|
      1 |  easy   | 1/2/2016 |          |         |
      2 |  normal | 1/3/2016 |          |         |
      2 |  hard   | 1/4/2016 |          |         |
      1 |  easy   | 1/5/2016 | selected | updated |

Column A and B上方的示例输出中,重复的值1 and easy第4行已被标记为Selectedupdated,因为它是今天最接近的日期..如果{{ 1}}与column A and B1,normal没有完成任何操作的值相同

我的代码(已编辑):

1 ,hard

countifs现在仅按一列工作,我需要的是两列中的一对重复值,例如示例输出: Sub sample1() Dim i As Long, lastRow As Long, countRow As Long, countRow1 As Long Dim Var1 As Integer With Worksheets("Sheet1") lastRow = .Range("A" & Rows.Count).End(xlUp).Row For i = 1 To lastRow countRow = Application.CountIf(.Columns(1), .Cells(i, 1)) countRow1 = Application.CountIf(.Columns(2), .Cells(i, 2)) If countRow > 2 Then If Not CBool(Application.CountIfs(.Columns(1), .Cells(i, 1), _ .Columns(3), ">" & .Cells(i, 3))) Then _ .Cells(i, 4) = "selected" If countRow1 > 2 Then If Not CBool(Application.CountIfs(.Columns(2), .Cells(i, 2), _ .Columns(3), ">" & .Cells(i, 3))) Then _ .Cells(i, 5) = "updated" End If End If End If Next End With End Sub Column A and B1easy中是相同的,为什么它的标签我的代码标签他们separetely。请帮我解决这个问题!

2 个答案:

答案 0 :(得分:1)

有些代码我无法调和,所以我删除了它们以提供简化的解决方案。

$scope.fieldsInfo=[];

  $scope.fieldsInfo=[
                      { 
                       "label"    :"from",
                       "required" :false,
                       "isArray"  :false,
                       "type"     : [
                                      {
                                        "label"   :"name",
                                        "type"    : "String",
                                        "required": false
                                      }
                                      {
                                        "label"   : "email",
                                        "type"    : "String",
                                        "required": true 
                                      }                     
                                    ]
                      },
                      { 
                       "label"    :"to",
                       "required" :true,
                       "isArray"  :true,
                       "type"     : [
                                      {
                                        "label"   : "name",
                                        "type"    : "String",
                                        "required": false
                                      }
                                      {
                                        "label"   : "email",
                                        "type"    : "String",
                                        "required": true 
                                      }                       
                                    ]
                      },
                      { 
                       "label"    :"subject",
                       "required" :true,
                       "isArray"  :false,
                       "type"     :"String" 
                      },
                      { 
                       "label"    :"text",
                       "required" :true,
                       "isArray"  :false,
                       "type"     :"String" 
                      },
                      { 
                       "label"    :"html",
                       "required" :true,
                       "isArray"  :false,
                       "type"     :"String" 
                      }
                    ]

Count_two_column_duplicate_before Count_two_column_duplicate_after
数据在before_column_dated_duplicates() 之后的数据two_column_dated_duplicates()

答案 1 :(得分:0)

countRow似乎是问题所在。你在If语句中经常阅读它,但你似乎没有在任何地方设置它。 我猜你正在获取当前条目并计算该列中该值的条目数。如果有多个条目,那么有重复项在这种情况下,我猜你的问题行是

columnA = Application.CountIf(.Column(1), .Cells(i, 1))

这应该不是

countRow= Application.CountIf(.Column(1), .Cells(i, 1))

您的缩进可能更容易阅读。 你也有:

If countRow > 1 Then
Else         If countRow > 1 Then
End If"

你的If和Else具有相同的条件。我无法看到Else正在做什么,因为它只是设置已经设置的columnA(countRow?)。

你似乎也有2个循环,基本上是同样的事情。

你有一个变量i,它被读取但从未设置过,似乎是一个重复的rowcounter

我认为您需要重新开始并了解代码需要执行的操作。在伪代码中你想要这个:

Loop through every row in the table
  if Count of data in current row, column 1 > 1 then
    if Count of data in current row, column 2 > 2 then
        set current row, column 4 = "Selected"
        set current row, column 5 = "Updated"
     endif
   endif
end loop

您的代码应该只有10行或12行,并且涉及带有嵌套ifs的单个循环。