[英]Delete multi Rows in Workbook2 depending on cell values in Workbook1
B:B
column (Workbook1) there are values, if these values are the same as those available in A:A
column (Workbook2) delete entire row that contains the same values in (Workbook2). 在B:B
列(Workbook1)中有值,如果这些值与A:A
列(Workbook2)中可用的值相同,则删除包含(Workbook2)中相同值的整行。 Can anyone help me to create the code? 谁能帮我创建代码?
i tried this one ..... 我尝试了这个.....
Private Sub CommandButton1_Click()
Dim WB1, WB2 As Workbook
Dim WS1, WS2 As Worksheet
Set WB1 = ThisWorkbook
CSN = Cells(1, 1)
Set WB2 = Workbooks.Open("C:\Users\Basel\Desktop\" & CSN & "")
Set WS1 = WB1.Worksheets("sheet1")
Set WS2 = WB2.Worksheets("Sheet1")
LastRow1 = WB1.WS1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
LastRow2 = WB2.WS2.Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To 20
If WB1.WS1.Cells(i, 2).Value = WB2.WS2.Cells(i, 1).Value Then
WB2.WS2.Cells(i, 1).EntireRow.Delete
End If
Next i
End Sub
Assuming the following: 假设以下内容:
1) you have created two workbook objects called WorkBook1 and WorkBook2 1)您创建了两个名为WorkBook1和WorkBook2的工作簿对象
2) you are comparing column "A" in both books with the sheet called "sheet1" 2)您正在将两本书中的“ A”列与名为“ sheet1”的工作表进行比较
Dim WB1sheet As Worksheet
Dim WB2sheet As Worksheet
Dim cell As Range
Dim cell2 As Range
Set WB1sheet = WorkBook1.Sheets("sheet1")
Set wb2sheet = Workbook2.Sheets("sheet1")
'Loop through colum A
For Each cell In WB1sheet.Range("a1", "a1000000")
' for each loop through the other sheet
If cell = "" Then
Exit For
End If
For Each cell2 In wb2sheet.Range("a1", "a1000000")
If cell = cell2 Then
cell2.ClearContents
Exit For
End If
Next cell2
Next cell
End Sub
This will just leave a blank cell not delete the row, it is more tricky to delete the row because the for each loop will get out of sink and miss rows. 这将只留下一个空白单元格而不删除该行,删除该行比较棘手,因为for每个循环将退出接收器和丢失行。 If you need the row removed not just cleared then the easy fix is to do a sort on the column afterwards and the blanks will all move to the bottom. 如果您需要删除的行不仅仅被清除,那么简单的解决方法是随后对列进行排序,空白全部移到底部。 Just record the sort using macro record. 只需使用宏记录记录排序。
Good luck 祝好运
thanks really i made some modification on the code but it works. 真的,谢谢,我对代码做了一些修改,但是可以正常工作。
Private Sub CommandButton1_Click()
Dim WB1, WB2 As Workbook
Dim WS1, WS2 As Worksheet
Dim CELL1 As Range
Dim CELL2 As Range
CSN = Cells(1, 1)
Set WB1 = ThisWorkbook
Set WB2 = Workbooks.Open("C:\Users\Basel\Desktop\" & CSN & "")
Set WS1 = WB1.Worksheets("sheet1")
Set WS2 = WB2.Worksheets("Sheet1")
For Each CELL1 In WS1.Range("B1", "B10")
If CELL1 = "" Then
Exit For
End If
For Each CELL2 In WS2.Range("A1", "A10")
If CELL1 = CELL2 Then
CELL2.EntireRow.Delete
Exit For
End If
Next CELL2
Next CELL1
WB2.Save
WB2.Close
Range("B:B").ClearContents
End Sub`
` `
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.