使用VBA对具有重复值的电子表格进行排序

时间:2018-08-21 18:49:26

标签: vba excel-vba

我正在尝试创建一个宏,该宏将检查列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

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

2 个答案:

答案 0 :(得分:0)

下一个没有for的原因是if计数不匹配if末尾 在这里,缩进非常有用。

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

答案 1 :(得分:0)

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

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