EXCEL使用宏实现筛选重复项并对该行进行填充内容的操作

EXCEL使用宏实现筛选重复项并对该行进行填充内容的操作

需求

1.一个excel中有多张不同的sheet

2.筛选出sheet1中A、B列与sheet2中A、B列中非重复行

3.在非重复行对应的D列填充内容

原始表:需要排出专家1wbb在自己没课的时候可以听其他人课的时间,在专家1中做上标记"zj1"(代表此时wbb无课,可以作为专家1去听课)

sheet1:记录一段时间内所有课程安排

sheet2:一段时间内专家1wbb的课程安排

方法

使用宏:

创建一个宏,名字随便自己起,将编写的好的宏复制进去,然后在运行

代码

vbnet 复制代码
Sub find1()
'
' find1 Macro
'
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim lastRow1 As Long, lastRow2 As Long, i As Long, j As Long
    Dim cellValue1A As String, cellValue1B As String
    Dim cellValue2A As String, cellValue2B As String
    Dim combinedValue1 As String, combinedValue2 As String
    Dim isDuplicate As Boolean
    
    ' 设置工作表
    Set ws1 = ThisWorkbook.Sheets("Sheet1")
    Set ws2 = ThisWorkbook.Sheets("Sheet2")
    
    ' 获取两个工作表的最后一行
    lastRow1 = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row
    lastRow2 = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row
    
    ' 遍历Sheet1的A和B列
    For i = 1 To lastRow1
        cellValue1A = ws1.Cells(i, 1).Value ' 日期
        cellValue1B = ws1.Cells(i, 2).Value ' 节次
        combinedValue1 = cellValue1A & cellValue1B ' 拼接日期和节次
        
        isDuplicate = False ' 假设当前行不是重复行
        
        ' 遍历Sheet2的A和B列
        For j = 1 To lastRow2
            cellValue2A = ws2.Cells(j, 1).Value ' 日期
            cellValue2B = ws2.Cells(j, 2).Value ' 节次
            combinedValue2 = cellValue2A & cellValue2B ' 拼接日期和节次
            
            ' 如果拼接值相同,则是非重复行
            If combinedValue1 = combinedValue2 Then
                isDuplicate = True
                Exit For
            End If
        Next j
        
        ' 如果不是重复行,则在D列对应行填写"zj1"
        If Not isDuplicate Then
            ws1.Cells(i, 4).Value = "zj1" ' D列是第4列
        End If
    Next i
    
    ' 提示完成
    MsgBox "处理完成!非重复行已在Sheet1的D列标记为'zj1'。"
End Sub

执行宏后的效果

相关推荐
allbs11 小时前
spring boot项目excel导出功能封装——2.高级导出
spring boot·后端·excel
CHN悠远15 小时前
飞腾D3000安装debian13后使用WPS的方法
wps
睿思达DBA_WGX1 天前
使用 Python 的第三方库 xlrd 读取 Excel 文件
python·excel
JCGKS1 天前
Go| excelize的流式迭代器
后端·golang·excel·excelize·流式读取·文件解析
裤裤兔1 天前
利用VBA批处理word 文档,使用宏对docx文件内容进行批量替换
c#·word·.net··vba·office·宏操作
❀͜͡傀儡师1 天前
docker 安装WPS
docker·容器·wps
yesyesyoucan1 天前
文本与表格格式转换助手:轻松实现TXT/CSV互转及Excel转CSV的实用工具
科技·程序人生·excel·交互·媒体
我命由我123451 天前
Excel - Excel 找回意外关闭的未保存的文档
学习·职场和发展·excel·求职招聘·职场发展·运维开发·学习方法
罗政2 天前
WPS Excel快速插入一批图片
excel·wps
daols882 天前
vxe-table 如何实现跟 excel 一样的筛选框,支持字符串、数值、日期类型筛选
前端·javascript·excel·vxe-table