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

学习者04390

我正在尝试创建一个宏,该宏将检查 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”错误,但我不确定如何解决它,或者如果我这样做了代码是否可以工作。任何帮助将不胜感激。

来宾

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Excel VBA从关闭的电子表格中提取支票簿值

使用VBA在Excel电子表格中设置数据格式

打开电子表格时,使用当前日期填充单元格VBA

使用VBA识别并删除电子表格中的不间断空格字符

在未打开的电子表格中使用VBA FIND()函数(或类推)

使用VBA脚本将数据从.txt转换为Excel电子表格

使用VBA将VLOOKUP插入电子表格,但表数组长度可调

使用VBA将电子表格范围的各个部分设为绝对

使用VBA将特定的文本文件导入Excel电子表格

如何使用VBA在Excel电子表格中的列表中打开文件夹

使用 PHP API 在 Google Drive 电子表格中对表格进行排序

使用VBA将所有内容从打开的记事本复制到电子表格,而无需保存记事本

如何使用带有电子表格 ID、电子表格名称、表格 ID 和值的 golang 在 googlesheet 中插入数据

使用countif()和外部电子表格中的值进行条件格式化

Google电子表格:使用值作为行号

使用gspread更新具有相同数据的多个电子表格文件

如何使用pandas“取消透视”具有多个嵌套维度的电子表格?

如何将一些VBA代码嵌入到使用ClosedXML创建的电子表格中?

添加唯一ID字段以标识使用VBA从不同Excel文件导入的电子表格

使用VBA将数据从Excel消息框中提取到电子表格中(从Zillow中提取数据)

VBA脚本使用不同的颜色,缓慢的电子表格格式化单元格

使用现有范围名称从电子表格读取或检索值

如何使用JavaScript从Google电子表格中列的所有值中获取排序后的唯一值

是否有关于使用CTRL + SHIFT + ENTER填充Excel电子表格单元格的问题,从而无法成功进行排序?

使用链接到应用程序脚本的按钮在电子表格上对范围进行排序

VBA循环遍历范围并排序到单独的电子表格

使用python,Google Seets API,v4更新电子表格时出错:参数'range'具有多个值

从Excel电子表格读取错误到VBA

电子表格中的VBA UserForm标签