简体   繁体   English

根据两列中的值删除行的条件语句

[英]Conditional statement to delete rows based on values in two columns

I've tried searching here and generally through Google to find a solution to suit my needs, but have come up empty.我试过在这里搜索,通常是通过谷歌搜索来找到适合我需求的解决方案,但结果是空的。 I'm new to using Excel VBA, but I assume this is the correct way to go about things in this case.我是使用 Excel VBA 的新手,但我认为这是在这种情况下处理事情的正确方法。

I have a large spreadsheet - many worksheets with thousands of rows in each.我有一个很大的电子表格 - 许多工作表,每个工作表都有数千行。 Each worksheet has similar data in it.每个工作表中都有相似的数据。 There are 5 columns, AE.有5列,AE。 Column C has duplicate data, and column E disambiguates it. C 列有重复数据,E 列消除了它的歧义。 What I'm looking for is a way to delete rows when the condition of "If c28=c29=c30=c31, and e28:e31 contains '.tif', then delete rows where e28:e31 is not '.tif'."我正在寻找的是一种在“如果 c28=c29=c30=c31 和 e28:e31 包含 '.tif' 的条件下删除行的方法,然后删除 e28:e31 不是 '.tif' 的行。 ”

I hope this makes sense.我希望这是有道理的。

Thanks for any help!谢谢你的帮助!

I do not understand why people waste so much time searching for exactly the code they need for a problem that is unique to them.我不明白为什么人们要浪费这么多时间来准确地搜索他们需要的代码来解决他们独有的问题。 It would be so much easier to learn the basics of VBA and then code their solution themselves学习 VBA 的基础知识然后自己编写解决方案会容易得多

Search for “Excel VBA Tutorial”.搜索“Excel VBA 教程”。 There are many to choose from so pick one that matches your learning style.有很多可供选择,因此请选择与您的学习风格相匹配的一种。 I prefer books.我更喜欢书。 I visited a good library;我参观了一个很好的图书馆; borrowed the most promising Excel VBA Primers to study at home then bought the one I preferred as a permanent reference book.借了最有前途的 Excel VBA Primers 在家学习,然后买了一本我喜欢的作为永久参考书。 A few hours learning VBA and you could solve this problem and the next and the next without wasting hours wandering around the web looking at code you do not understand.几个小时学习 VBA,你就可以解决这个问题,然后解决这个问题,而不用浪费几个小时在网上闲逛看你不理解的代码。

Think about your requirement.想想你的要求。 I assume c28=c29=c30=c31 is an example.我假设c28=c29=c30=c31是一个例子。 c31=c32=c33=c34 would also be of interest. c31=c32=c33=c34也很有趣。 You need to generalise your specification.您需要概括您的规范。

I will replace c28 by Cells(28, "C") which happens to be VBA syntax but any suitable notation will do at this stage.我将用Cells(28, "C")替换c28 ,这恰好是 VBA 语法,但在此阶段可以使用任何合适的符号。 I think you mean:我想你的意思是:

Cells(RowCurrent, "C") = Cells(RowCurrent+1, "C") And _
Cells(RowCurrent, "C") = Cells(RowCurrent+2, "C") And _
Cells(RowCurrent, "C") = Cells(RowCurrent+3, "C")

for all permitted values for RowCurrent .对于RowCurrent所有允许值。

What are the permitted values for RowCurrent ? RowCurrent允许的值是RowCurrent If you have one header row, the first data row will be 2 so the minimum value for RowCurrent will be 2 .如果您有一个标题行,则第一个数据行将为2因此RowCurrent的最小值将为2 If RowLast is the last used row of the worksheet, the maximum value for RowCurrent is RowLast – 3 .如果RowLast是在工作表的最后使用排,为最大值RowCurrentRowLast – 3

When you say e28:e31 do you means cells e28, c29, c30 and e31 have been merged?当您说e28:e31 ,您的意思是单元格 e28、c29、c30 和 e31 已合并吗?

Do you mean e28:e31 contains .tif or e28:e31 ends .tif ?你的意思是e28:e31包含.tif还是e28:e31结束.tif Would you keep abc.tif.txt ?你会保留abc.tif.txt吗? When programming, your specification must be absolutely precise.编程时,您的规范必须绝对精确。 The computer will do what you say not what you meant.计算机会做你所说的而不是你的意思。

You say you have many worksheets and you want to read each of them in turn.你说你有很多工作表,你想依次阅读每个工作表。 I would expect any tutorial to explain how to loop through all the worksheets of a workbook.我希望任何教程都能解释如何遍历工作簿的所有工作表。 Alternatively, this is the sort of question you can search for.或者,这是您可以搜索的问题类型。 If you have a single requirement, you can usually find a suitable match.如果您只有一个需求,通常可以找到合适的匹配项。 Put another way, searching for D may be easy but searching for A and D and I is difficult.换句话说,搜索 D 可能很容易,但搜索 A、D 和 I 却很困难。

You have an outer loop for each worksheet.每个工作表都有一个外循环。 You need to find the last used row of each worksheet.您需要找到每个工作表的最后使用的行。 You have an inner loop for each block of rows.每个行块都有一个内部循环。 You need to delete uninteresting blocks.您需要删除无趣的块。 I would expect you to learn how to do each of those from your tutorial.我希望你从你的教程中学习如何做每一个。 Can you put those four separate techniques together?你能把这四种不同的技术放在一起吗?

I can only see one serious complication with your requirement;我只能看到一个严重的问题与您的要求; you do not delete uninteresting rows, you keep interesting rows.你不会删除不感兴趣的行,你会保留感兴趣的行。 If you examine rows 2 to 5 and find they do not conform to your template, you cannot delete them because rows 3 to 6 might conform to your template.如果您检查第 2 行到第 5 行并发现它们不符合您的模板,则不能删除它们,因为第 3 行到第 6 行可能符合您的模板。 I do not believe you could possibly find any code that would meet this requirement.我不相信您可能会找到任何符合此要求的代码。 You would have to design the solution yourself.您必须自己设计解决方案。 I have a couple of suggestions but I would need to have more confidence in your exact requirement before suggesting them.我有一些建议,但在提出建议之前,我需要对您的确切要求更有信心。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM