excel - 如何从一个 Excel 列中删除与另一个 Excel 文件匹配的行?使用 VBA
问题描述
您好,感谢您的帮助。
我有两个 Excel 文件,我们称它们为 Excel 1(活动文件)和 Excel 2(我只需要比较是否有重复文件)。我想从 Excel 1 中删除在 Excel 2 中找到的匹配项。仅从 Excel 1 中删除匹配项,并保持 Excel 2 完整。
我通常使用Vlookup执行此过程,然后删除匹配项。
[示例][1]:=VLOOKUP(C2,'[End Use Screening Log.xlsb]EUS Log'!$A:$A,1,0))
这是Vlookup之后产生的宏代码:
Sub Testing()
'
' Testing Macro
'
'
Workbooks.Open Filename:= _
"Z:\Customer Screening\End User Screening Log\End Use Screening Log.xlsb"
Windows("Copy of WW33 TEST .xlsm").Activate
Range("G2").Select
ActiveCell.FormulaR1C1 = _
"=VLOOKUP(RC[-4],'[End Use Screening Log.xlsb]EUS Log'!C1,1,0)"
Range("G2").Select
Selection.AutoFill Destination:=Range("G2:G16")
Range("G2:G16").Select
Range("G1").Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$Q$16").AutoFilter Field:=7, Criteria1:=Array( _
"4997466", "6392634", "9026175", "9362935", "9363654", "9369599", "9370171"), _
Operator:=xlFilterValues
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
Range("E15").Select
Selection.AutoFilter
Range("G2:G9").Select
Selection.ClearContents
Range("Q2").Select
End Sub
但是,我正在尝试使流程自动化,只需单击一下即可。
我想将来自的列与C
来自Excel 1
的列进行比较。A
Excel 2
我想我需要用 VBA 来做,因为我已经尝试过录制宏但不能正常工作。任何想法如何使这成为可能?
玛丽亚
解决方案
我创建了一个非常粗略的代码,可能只适合您的需求。我不知道您的工作表是什么样的以及您的确切需求是什么,但我只是假设您只是将 Excel 1 的 C 列的每个单元格与 Excel 2 的 A 列的值匹配,如果匹配,则Excel 1 的 C 行的单元格将被删除。
Excel1 工作簿:
Excel2 工作簿:
代码:
Sub Macro1()
Start = 2
'Change path to your excel's file name
'This will open your 2nd excel file so that you won't have to open it evertime. Delete when not needed
Workbooks.Open ("C:\Users\Pops\Desktop\Excel2.xlsm")
'The deletion of the row will mess up with the For-Next loops so I included a GoTo so this is where it will end up
ReLoop:
'Counts how many rows are in your worksheets
Total_rows_Excel1 = Workbooks("Excel1.xlsm").Worksheets("Sheet1").Range("C" & Rows.Count).End(xlUp).Row
Total_rows_Excel2 = Workbooks("Excel2.xlsm").Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
'Loops on all the rows on your worksheet
For i = Start To Total_rows_Excel1
For j = 2 To Total_rows_Excel2
If Workbooks("Excel1.xlsm").Worksheets("Sheet1").Range("C" & i) = Workbooks("Excel2").Worksheets("Sheet1").Range("A" & j) Then
Workbooks("Excel1").Worksheets("Sheet1").Rows(i).Delete Shift:=xlUp 'Deletes the rows in Excel1 that have a match from Excel2
Start = i 'This will let the loop to start at the last row it stopped when it loops again so it's less computationally taxing
GoTo ReLoop
End If
Next j
Next i
End Sub
因此,单击一个按钮,将删除 Excel 1 的 C 列到 Excel 2 的 A 列中的所有匹配项。
结果:
推荐阅读
- node.js - Node - 使用 NODE_MODULE_VERSION 72 针对不同的 Node.js 版本进行编译
- ios - 如何在 Swift 中添加搜索栏?
- python - Twitter bot 未将推文 ID 保存在文本文件中
- math - 6道选择题中3道选择题不正确的概率是多少?
- ruby - Ruby - 将嵌套哈希转换为 CSV
- java - Java 记录 (JEP359) 作为 Spring 控制器请求和响应 DTO
- java - Micronaut 如何仅使用 @Inject 注解进行注入?
- node.js - Nodejs Websockets 在本地工作,但不在服务器上
- swagger-codegen - 如何在 Swagger Codegen Spring 服务器存根中没有 responseWrapper
- here-api - Maps API:如何淡化基础层?