从下到上循环删除excel中的每一行,无论是否满足条件 [英] Bottom to top loop deletes every row in excel regardless if the If requirements are met
问题描述
我尝试了以下代码的几种不同变体,以尝试找到一种可行的方法.我的最后一次迭代使我离开:
I've tried several different variations of the below code to try to find a method that may work. My last iteration left me at:
Dim D As Range
Dim x As Integer, c As Long, lre As Long
c = Range("A" & Rows.Count).End(xlUp).Row
lre = Sheets("Exception Report").Cells(Sheets("ExceptionReport").Rows.Count, "A").End(xlUp).Row
Range("D2:D" & lre).TextToColumns
Range("F2:F" & lre).TextToColumns
For x = c To 2 Step -1
If -0.1 < CDbl((Cells(x, "D").Value) - (Cells(x, "E").Value)) < 0.1 And -0.1 < CDbl((Cells(x, "F").Value) - (Cells(x, "G").Value)) < 0.1 Then
Cells(x, 1).EntireRow.Delete
End If
Next
这部分中的代码着眼于两对单元格,我从另一对中减去一个,以观察差异.如果它在上述范围内(-0.1 <代码<0.1),则将其删除,基本上留下某些离群值.文本到列部分将一些数据(存储为文本)转换为数字数据类型,因此我可以为此目的使用它.
The code in this part look at two pairs of cells, and I subtract one from the other in order to look at the difference. If it is within the range above (The -0.1 < code < 0.1) then it is to be deleted, basically leaving certain outliers. The text to columns part turns some of the data (which is stored as text) into number datatype so I can work with it for this purpose.
我一直面临的问题是,无论使用什么数字,它的评估结果都为true,然后删除整个行,使报告为空白,即使我知道有些行不符合标准.
The problem I have been facing is that no matter the numbers used, it evaluates as true which then deletes the entire row, leaving the report blank, even though I know that there are rows which do not meet the criteria.
我已经没有足够的想法了,而我对excel的新手经验使我无法调试它.有帮助/评论/想法吗?
I've run out of ideas and my novice experience with excel has left me unable to debug this. Any help/comments/ideas?
推荐答案
您不能有多个比较,例如a < b < c
,需要使用a < b And b < c
.
You can't have multiple comparisons like a < b < c
, you need to use a < b And b < c
.
我不确定会发生什么,但是首先评估<
之一,然后将结果(True
或False
)与最后一个将结果混合的数字进行比较.
I am not exactly sure what happens but one of the <
s is evaluated first and the result (True
or False
) is then compared to the last number which mixes things up.
第一个<
似乎首先被评估,给出True
或False
,因为当被转换为Double
时True
被评估为-1
且False
被评估为0
,您总是在检查<0.1
时得到True
.
the first <
seems to be evaluated first, giving True
or False
since True
is evaluated as -1
and False
as 0
when being cast as Double
, you always get True
when checking <0.1
.
这篇关于从下到上循环删除excel中的每一行,无论是否满足条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!