【发布时间】: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 是因为我以前使用过该属性,但它可能不是最好的。有什么想法吗?