【问题标题】:Excel VBA comparing two rows in dynamic cell rangeExcel VBA比较动态单元格范围中的两行
【发布时间】:2014-02-22 01:21:46
【问题描述】:

好的,这是我的第一篇文章(当然不是最后一篇,因为我是这个项目的唯一工作人员)。我的 VBA 经验非常少,所以你看到的有点类似于我的“hello world”。

长话短说...我想比较动态范围表中的两行(在打开时从访问数据库中更新)并找到与前四列完全匹配的行(见下面的代码)。

然后比较两个比较行中的最大值的第七列。 (Full、Adequate、Basic是从高到低比较的值)

然后,在满足所有这些标准后,丢弃最低的并保留最高的。 FOR ALL ROWS(也就是 While 循环)。

此代码用于我工作地点的培训数据库,随着员工继续培训,有许多具有不同理解水平的相同条目。此代码应完成所有培训并仅保留最高价值(其能力的最佳代表)并丢弃其他“过时”条目。

这是我毫无价值的代码:

Sub RemoveDuplicates()
Dim Bottom As Integer
Dim FalseBottom As Integer
'remove lower leveled duplicate entries
Bottom = CInt(Cells(Rows.Count, "A").End(xlUp).Row) 'initializes the bottom of the    list (total number of entries)
Do Until Bottom = 0
 FalseBottom = 1
 If Not IsEmpty(Cells(Bottom, "A")) Then
  Do Until FalseBottom = Bottom
   If ((Cells(FalseBottom, "A").Text = Cells(Bottom, "A").Text) And (Cells  (FalseBottom, "B").Text = Cells(Bottom, "B").Text) And (Cells(FalseBottom, "C").Text = Cells(Bottom, "C").Text) And (Cells(FalseBottom, "D").Text = Cells(Bottom, "D").Text)) Then
  '(Cells(FalseBottom, "G").Text > Cells(Bottom, "G").Text)
    If (Cells(Bottom, "G").Text = "Full") Then
     Rows(FalseBottom).Delete Shift:=xlUp
     FalseBottom = FalseBottom - 1
    End If
    If ((Cells(Bottom, "G").Text = "Adequate") And (Cells(FalseBottom, "G").Text = "Basic")) Then
     Rows(FalseBottom).Delete Shift:=xlUp
     FalseBottom = FalseBottom - 1
    End If
    If (Cells(FalseBottom, "G").Text = "Full") Then
     Rows(Bottom).Delete Shift:=xlUp
    End If
    If ((Cells(FalseBottom, "G").Text = "Adequate") And (Cells(Bottom, "G").Text = "Basic")) Then
     Rows(Bottom).Delete Shift:=xlUp
    End If
  End If
  FalseBottom = FalseBottom + 1
  Loop
  End If
 Bottom = Bottom - 1
Loop
End Sub

为了更好地进一步解释,在我的 excel 表中我有

A    |B    |C       |D      |E          |F          |G
---------------------------------------------------------------
First|Last |Category|Task   |Performance|Requirement|Understanding
---------------------------------------------------------------
Joe  |Smoe |Cleaning|Toilets|10         |10         |Basic
Joe  |Smoe |Cleaning|Toilets|10         |10         |Adequate
Joe  |Smoe |Cleaning|Toilets|10         |10         |Full
Joe  |Smoe |Cleaning|Showers|10         |10         |Basic
Jane |Plane|Cleaning|Toilets|10         |10         |Basic
...
...

基本上有没有办法找到(在所有行中)前 4 列匹配的位置,然后比较最后一列以查看哪个是最高级别并丢弃其他匹配项?

【问题讨论】:

  • 欢迎来到 SO! See this,尤其是第一。您包含了太多代码,并且包含了难以帮助您的广泛陈述。请尝试调试您的代码 (SEE THIS),并根据您在完成后遇到的具体问题修改您的问题。
  • 感谢您的建议 :) 我编辑了问题,希望它能更好地解释我的问题。

标签: excel comparison row dynamic-data vba


【解决方案1】:

已解决问题!!

将 .Text 更改为 .Value

拆分 If 语句

将值分配给自己的变量以提高可读性

而且工作起来就像一个魅力......不知道为什么,但我会接受它。

【讨论】:

    猜你喜欢
    • 2012-08-17
    • 1970-01-01
    • 2015-11-26
    • 1970-01-01
    • 1970-01-01
    • 2011-12-12
    • 1970-01-01
    • 2018-10-06
    • 2019-12-22
    相关资源
    最近更新 更多