简体   繁体   English

根据Workbook1中的单元格值删除Workbook2中的多行

[英]Delete multi Rows in Workbook2 depending on cell values in Workbook1

  1. I need VBA code that will open Workbook2 depending on value of a cell in Workbook1 (A1). 我需要根据工作簿1(A1)中单元格的值打开工作簿2的VBA代码。
  2. In 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.

相关问题 根据 Workbook1 中单元格值中的单元格值筛选 Workbook2 中的数据 - Filter Data in Workbook2 based on the cell value in cell values in Workbook1 循环搜索workbook1中的数据,将偏移单元格复制到workbook2 - Loop to search data in workbook1, copy offset cell to workbook2 在工作簿2中找到一个值,并将偏移值复制到工作簿1中 - Find a values in workbook2 and copy offset value to workbook1 从Workbook1中对Workbook2中的数据进行排序 - Sorting Data in Workbook2 from Workbook1 如何在workbook1更新时将workbook1中的数据添加到workbook2和workbook2更新中? - How to add data from workbook1 into workbook2 and workbook2 updates when workbook1 updates? 从 Workbook1、Column1 中查找与 Workbook2、Column 1 中的值不同的值并在新工作表中显示 - Find Values from Workbook1, Column1 that are unique from values in Workbook2, Column 1 and display in new sheet EPPlus 将工作表从 Workbook1 复制到 Workbook2 - EPPlus To Copy Worksheet From Workbook1 to Workbook2 如何将数据从工作簿 1 的工作表 1 复制到工作簿 2 的工作表 2? - How to copy data from sheet1 of workbook1 to sheet 2 of Workbook2? 如何将数据从一个已经打开的Excel文件(Workbook1,Sheet1,单元格A11)复制到另一个已经打开的Excel文件(workbook2,sheet1,A11) - How to copy data from one already opened excel file (Workbook1,Sheet1,cell A11) to another already opened excel file(workbook2,sheet1,A11) 如何将Workbook1 / Sheet3中的选定行与Workbook2 / Sheet3中的选定行进行比较 - How to compare a selected row in Workbook1/Sheet3 with a selected row in Workbook2/Sheet3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM