【问题标题】:Sorting Spreadsheet with Duplicate Values Using VBA使用 VBA 对具有重复值的电子表格进行排序
【发布时间】:2019-01-28 00:18:14
【问题描述】:

我正在尝试创建一个宏来检查 A 列(客户端 ID 号)的值,识别重复值,然后,一旦找到重复值,执行嵌套 If/Then 检查,将值返回到根据它找到的某些单元格。如果重复值对应的行的F列(程序描述)包含子串“UPGRADE”,则应使原值对应的行J列的文本等于重复值中F列的文本。然后,应该删除重复的行,但我还没有做到这一点。

这是我目前所拥有的-

Dim lastrow As Long

lastrow = Cells(Rows.Count, "A").End(xlUp).Row 'find last row in column A

For x = 1 To lastrow
If Cells(x, 1).Value <> ActiveCell.Value Then 'Check if cell in column A contains the same value as the activated cell 

 For y = 1 To lastrow
        If Cells(y, 1).Value = Cells(x, 1).Value Then 'Compares cell against each value in column A. If there is a match, the do the following:

If Cells(y,6).Value <> "UPGRADE" Then 'Checks if duplicate value contains "UPGRADE"

Cells (x,10).Value= Cells(y,10).Value 'If this value is found, copy the value of the duplicate program name into a specified column for that program type in row x.

Else Cells(x,12).Value=Cells(y,12).Value 'If the value is not found, copy the program type into a separate column for that program type in row x.
    End If
    Next y
End If
Next x

我尝试运行此程序并收到“Next without For”错误,但我不确定如何解决它,或者如果我这样做了代码是否可以工作。任何帮助将不胜感激。

【问题讨论】:

  • 数一下您的Ifs 和End Ifs - 您缺少End If。缩进也会立即显示这一点。虽然我确信有一种更有效的方式来做你想做的事。
  • 您对 Next 的看法是正确的,没有 For 错误,非常感谢您。看起来我在最后错过了一个 End If。宏现在运行,但它没有返回任何值。我认为问题出在第四行。我将它设置为 ActiveCell.Value 是因为我以前使用过该属性,但它可能不是最好的。有什么想法吗?

标签: vba excel


【解决方案1】:

目前尚不清楚您想要做什么,但是以下内容对您有用吗?

Public Sub checkDup()
Dim lastrow As Long, x As Long, y As Long

lastrow = Cells(rows.Count, "A").End(xlUp).Row 'find last row in column A
For x = 1 To lastrow
    'If Cells(x, 1).Value <> ActiveCell.Value Then 'Check if cell in column A contains the same value as the activated cell
        For y = x + 1 To lastrow
            If Cells(y, 1).Value = Cells(x, 1).Value Then 'Compares cell against each value in column A. If there is a match, the do the following:
                If Cells(y, 6).Value <> "UPGRADE" Then 'Checks if duplicate value contains "UPGRADE"
                    Cells(x, 10).Value = Cells(y, 6).Value 'If this value is found, copy the value of the duplicate program name into a specified column for that program type in row x.
                'Else
                    'Cells(x, 12).Value = Cells(y, 12).Value 'If the value is not found, copy the program type into a separate column for that program type in row x.
                End If
            End If
        Next y
    'End If
Next x

End Sub

【讨论】:

  • 很遗憾没有。我将尝试更详细地解释一下。我必须检查 A 列中的值是否重复。当找到重复值时,我需要对重复行的第 6 列(代码中的 y)执行 If/Then 检查,以查看它是否包含给定值(“UPGRADE”)。如果是这样,我需要将该单元格的内容复制并粘贴(或简单地镜像)到原始值所在行的第 10 列(代码中的 x)。这有帮助吗?
  • 我们越来越近了,但还没有到。您修改后的代码工作正常,但它也提取了一个没有“UPGRADE”子字符串的结果,这是不可能的。您将“Else”条件隐藏在便条后面,因此如果未找到该值,则不应该发生任何事情。我正在尝试检查代码以查看是否可以评估发生这种情况的原因。我将尝试将一些图片放在一起,让您更清楚地了解发生了什么。
  • 我在测试中没有遇到这种行为。但是,如果您在 A 列中有两次以上相同的值,并且在第 6 列中至少有一个没有 UPGRADE 值,那么所有之前的重复项将看到它们的第 10 列值填充了 UPGRADE 值。
  • 我刚刚再次阅读了您的请求,看到您说等于 UPGRADE,而代码一直与 UPGRADE 不同。如果要检查重复项的第 6 列是否等于 UPGRADE,并且如果第 6 列中先前重复项的值不是 UPGRADE,则仅更改第 10 列值,则需要将行更改为:If Cells(y, 6).Value = "UPGRADE" And Not Cells(x, 6).Value = "UPGRADE" Then
  • 我不确定你的意思,但我相信我找到了问题所在。该代码现在可以正常工作。感谢您的宝贵帮助。
【解决方案2】:

下一个没有for是因为if then count不匹配end if count 在这里,缩进真的很有用。

Dim lastrow As Long
lastrow = Cells(Rows.Count, "A").End(xlUp).Row 
'find last row in column A
For x = 1 To lastrow
    If Cells(x, 1).Value <> ActiveCell.Value Then 
'Check if cell in column A contains the same value as the activated cell 
        For y = 1 To lastrow
            If Cells(y, 1).Value = Cells(x, 1).Value Then 
'Compares cell against each value in column A. If there is a match, the do the following:
                If Cells(y,6).Value <> "UPGRADE" Then 
'Checks if duplicate value contains "UPGRADE"
                    Cells (x,10).Value= Cells(y,10).Value 
'If this value is found, copy the value of the duplicate program name into a specified column for that program type in row x.
                Else Cells(x,12).Value=Cells(y,12).Value 
'If the value is not found, copy the program type into a separate column for that program type in row x.
                End If
'I inserted another end if here
            end if
        Next y
   End If
Next x

【讨论】:

  • 非常感谢语法提示。我尝试运行此代码,但仍然无法正常工作。我认为问题出在第四行,它指定了 y 正在为其寻找副本的 x 的值。
  • 我个人从不使用activecell。我总是指定一个范围,( namedsheet.cells(1,1) 甚至是 namedsheet.[A1] )
  • VonOlhlen 我不应该使用activecell有什么特别的原因,还是只是您的个人喜好?我非常喜欢 activecell,因为它是最简单的(在我看来)导航单元格的方法。
  • 它减慢了你的代码可能是我唯一的客观批评。在我的经验中,它使代码编写变得复杂,因为我必须让单元格的工作表处于活动状态,因此我必须始终跟踪哪个工作表处于活动状态并切换到该工作表只是为了修改一个单元格
猜你喜欢
  • 1970-01-01
  • 2019-11-27
  • 2012-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-25
  • 2016-09-11
相关资源
最近更新 更多