简体   繁体   English

VBA根据另一个工作表上的2个条件从工作表中删除行

[英]VBA to delete rows from a worksheet based on 2 conditions on another worksheet

I have searched the web and tried all possible solutions, but in vain. 我在网上搜索并尝试了所有可能的解决方案,但徒劳无功。

There are auditors looking at specific customers in specific areas. 有审计师在关注特定领域的特定客户。

I have three worksheets with data on it: Summary sheet, Raw Data sheet and an Area Listing sheet. 我有三个工作表,上面有数据:摘要表,原始数据表和区域列表表。 The data gets transferred from the data sheet to the summary sheet, but contains areas that do not belong to the specific auditor. 数据从数据表传输到摘要表,但包含不属于特定审核员的区域。 The area listings have an indicator Yes/No that must be used to clear the summary sheet of unwanted areas. 区域列表中有一个指示器是/否,必须使用它清除不需要的区域摘要表。

I therefore need to delete the unwanted areas from the Summary sheet by first matching the area names on both sheets and then delete those that are marked with a "No" on the area listing sheet. 因此,我需要通过首先匹配两个工作表上的区域名称,然后从区域工作表上标有“否”的区域中删除不需要的区域,以从“摘要”工作表中删除。

The core coding I have tried: 我尝试过的核心编码:

If ActiveCell.Formula = "=VLookup(B2,Areas!C2:D,1,False)"=True _
    And Sheets("Areas").Range("D2").Value = "No" Then     
    Sheets("Summary").EntireRow.Delete   

Where B2 is the column in Summary containing the Areas and C2 is the corresponding column in Areas with D2 the column in Areas listing the selections of Yes / No. 其中B2是摘要中包含Areas的列,C2是Areas中具有D2的对应列,Areas中的列列出是/否的选择。

How do I then write the code to delete the Rows in Summary where the areas are matching in the Summary and the Areas sheet and the indicator in the Areas sheet is "No"? 然后,如何编写代码以删除“摘要”和“面积”表中面积匹配的“摘要”中的行,而“面积”表中的指示器为“否”?

I have also tried: 我也尝试过:

For I = LastRowCheck To 1 Step -1
     If Sheets("Summary").Range("B" & LastRowCheck).Value = _
        Sheets("Areas").Range("C" & sdRow).Value _
        And Sheets("Areas").Range("D" & NoRow).Value = "No" Then
     If DelRange is Nothing Then
       Set DelRange = Sheets("Summary").Rows(i)
  End If
  If not DelRange Is Nothing Then DelRange.EntireRow.Delete
  End If  
  Next i

Can somebody please tell me where I am missing the boat? 有人可以告诉我我在哪里想念小船吗?

it seems to me, that the areas sheet is going to have.. lets say 50 different areas, and the summary sheet is going to have however many rows, each with an area from the area sheet. 在我看来,区域表将要有..可以说50个不同的区域,而摘要表将有很多行,每行都有一个来自区域表的区域。 so multiple rows would have the same area. 因此多行将具有相同的面积。 so, you want to loop through all the rows on the summary sheet, find the corresponding area from the area sheet, and see if that area has a "no" value in column D. 因此,您要遍历摘要表上的所有行,从面积表中找到相应的区域,并查看该区域在D列中是否具有“否”值。

given the above understanding, the following 2 options should both work: 鉴于以上理解,以下两个选项应同时起作用:

A) 一种)

  • loop through all rows on areas sheet and load all area names with D 循环浏览区域表上的所有行,并使用D加载所有区域名称
    column value of "No" into an array. 列值“否”放入数组中。

  • loop through all rows on summary sheet 遍历摘要表上的所有行

  • use inner loop to check and see if current row in summary sheet is found in array of "bad areas" 使用内部循环检查并查看摘要表中的当前行是否在“不良区域”数组中找到
  • delete entire row 删除整行

B) B)

  • use a loop to find all rows in the area sheet with a D column value of "No" and load the names from each of these areas into an array. 使用循环查找D列值为“ No”的区域工作表中的所有行,并将每个这些区域的名称加载到数组中。

  • use said array as the criteria for a data filter, filtering the area name column in the summary sheet 使用所述数组作为数据过滤器的条件,过滤摘要表中的“区域名称”列

  • delete all visible rows (ones with areas found on areas sheet with D column value of "No" 删除所有可见行(具有在区域工作表中找到的区域的D列值为“否”的行)

A Code: 代码:

Dim strArray() as variant
ReDim strArray(1 to 1)
dim deleted as boolean 'keeps track of whether row was delete or not

Sheets("Area Sheet").Activate
Range ("A1").Activate  'where assuming column a has area name
for i = 1 to lastrow
    if ActiveCell.offset(0, 3).formulaR1C1 = "No" then 'column D, I have had bad experience with .value so i always use formulaR1C1
          strArray(i) = ActiveCell.formulaR1C1
    end if
    ActiveCell.offset(1,0).activate
next

Sheets("Summary Sheet").activate
Range("A1") ' again, assuing a has area name
for i = 1 to endrow
    delete = false 'reset delete before inner loop
    for j = 1 to ubound(strArray)
         if ActiveCell.formulaR1C1 = strArray(i) then
              ActiveCell.entireRow.delete xlShiftUp
              deleted = true   
              exit for   'exits inner loop
         else
              deleted = false
         end if
    next
    if  not deleted then ActiveCell.offset(1,0).offset ' move down to next row if curent row was not deleted
next

B Code: B代码:

'use same as A code to get all areas with D Column "No" into array
Dim rng as Range

Range("A1").activate ' after activating summary sheet, again, assuming a has area name
ActiveCell.entireColumn.select
selection.AutoFilter Field:=1, Criteria1:=strArray, Operator:=xlFilterValues ' this will filter so only rows with area and column D of "No" will be visible.
set rng = Range("A1", Cells(lastrow, lastcolumn)).SpecialCells(xlCellTypeVisible) ' this will get all visible cells (ones that are visible with current filter condition)
rng.entirerow.delete xlshiftup 'will delete all rows in rng

depending on the amount of data, A) with application.Screenupdating =false may be faster than filter (use doesnt see whats happening behind the scenes.) do make sure to do Application.screenupdating = true again after to turn that back on. 根据数据量,A)使用application.Screenupdating =false可能比filter更快(用户看不到幕后发生的事情。)请确保在将其重新打开后再次执行Application.screenupdating = true

code may need tweaking for spelling mistakes etc.. but basis should be there I did not set endrow ever as everyone has their own way of getting the last row. 代码可能需要针对拼写错误等进行调整。.但是应该在此基础上,我没有设置endrow,因为每个人都有自己的获取最后一行的方式。

HTH good luck! 祝您好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VBA-删除另一个工作表中的行 - VBA - Delete rows in another worksheet 将基于多个条件的行从一个工作表复制到另一个VBA - Copy Rows Based On Multiple Criteria from one Worksheet to Another VBA 无法使用VBA中另一个工作表中的值基于数组过滤出一个工作表中的行 - Trouble filtering out rows on one worksheet based on an array with values from another worksheet in VBA Excel VBA如何根据不同工作表中单元格的值删除行 - Excel VBA How to delete rows based on the value of a cell in a different worksheet 使用VBA将基于单元格值的行添加到另一个工作表上 - Using VBA to Add Rows based on a cell value onto another worksheet Excel VBA脚本基于特定值将行从一个工作表传输到另一个工作表 - Excel VBA script to transfer rows from one worksheet to another based on a certain value 根据条件将数据从一个工作表复制到另一个工作表 - Copy data based on conditions from one worksheet to another 使用VBA根据不同工作表中的单元格值隐藏工作表中的行 - Hide rows in a worksheet based on a cell value in different worksheet using VBA 根据另一个工作表中的行数在表中插入行 - Insert rows in a table based on number of rows from another worksheet VBA将行从一个工作表复制到另一个工作表(语法错误?) - VBA Copy Rows from One worksheet to Another (Syntax error?)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM