[英]How to copy entire row based on Percentage comparison of 2 Cells
I'm trying to copy an entire row to another sheet if a cell is greater than 2% of another cell in the same row. 如果一个单元格大于同一行中另一个单元格的2%,我试图将整行复制到另一张纸上。 This is what I'm stuck, getting an IF Greater based off a formula comparing 2 cells:
这就是我所坚持的,基于比较两个单元格的公式获得了IF Greater:
Sub Filtration()
For Each Cell In Sheets(1).Range("R:R")
If Formula = "(R1 / P1)" > 0.021 Then
matchRow = Cell.Row
Rows(matchRow & ":" & matchRow).Select
Selection.Copy
Sheets("Sheet2").Select
ActiveSheet.Rows(matchRow).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet1").Select
End If
Next
EDIT: Thanks to @JNevil & @ShaiRado and the help they provide, I think I'm only stuck on the Macro returning an Overflow. 编辑:感谢@JNevil和@ShaiRado及其提供的帮助,我认为我只是停留在返回溢出的宏上。 I think that the
percentage = Cell.Value / Cell.Offset(, -2).Value
is being confused/conflicted some how and believes that percentage = 0
always. 我认为该
percentage = Cell.Value / Cell.Offset(, -2).Value
在某种程度上被混淆/冲突,并且始终认为该percentage = 0
。 There is also the issue that my Sheet1
contains blank lines, a header, and possible negative numbers on future updates. 还有一个问题是,我的
Sheet1
包含空白行,标题和将来更新时可能的负数。 The full code is as follows: 完整的代码如下:
Sub Filtration()
Dim writeRow As Integer
Dim percentage As Double
For Each Cell In Sheets(1).Range("R:R")
'because we don't want to do this for every cell in Column R (There are one million), then exit the loop when we need to
If Cell.Value = "" Or Cell.Value <= 0 Then
'Lets make sure we won't be dividing by zero. If we are then set the result to 0
If Cell.Offset(, -2).Value < 0 Then
percentage = 0
Else
percentage = Cell.Value / Cell.Offset(, -2).Value
End If
'divide the current cell's value by the the cell one column over's value and compare
If percentage > 0.021 Then
'Write this out to the writeRow variable in the other sheet and increment that number by 1 after writing
Sheet1.Rows(Cell.Row).Copy Destination:=Sheet2.Cells(writeRow, 1).Paste
writeRow = writeRow + 1
End If
End If
Next
End Sub
For having only been writing VBA for a week, you are doing a pretty good job here. 由于仅编写VBA一周,您在这里做得很好。 Most people avoid For loops like the plague when they are learning.
大多数人在学习时都会避免像瘟疫这样的For循环。
A couple of things. 有几件事。
.select
that is a user interface thing. .select
是用户界面。 VBA doesn't need to "Select" anything. activesheet
and activeworkbook
. activesheet
和activeworkbook
。 When you are a couple hundred lines of code down don't risk assuming that you know what will be the active sheet or active workbook at that point in time. ThisWorkbook.Sheets("mysheetname")
or similar. ThisWorkbook.Sheets("mysheetname")
或类似名称。 formula
or whatever. formula
或任何其他内容。 VBA is good at it. Something like the following should get you in the ballpark: 如下所示的内容应该可以帮助您:
Sub Filtration()
For Each Cell In Sheets(1).Range("R:R")
'divide the current cell's value by the the cell one column over's value and compare
If cell.value/cell.offset(,-2).value > .021 Then
Sheet1.Rows(cell.row).Copy Destination:=Sheet2.Cells(cell.row, 1)
'because we don't want to do this for every cell in Column R (There are one million), then exit the loop when we need to
If cell.value = "" Then Exit For
End If
Next
You may want to write to the other sheet without jumping rows. 您可能要写另一张纸而不跳行。 In that case you can employ a variable to track which row to write to:
在这种情况下,您可以使用变量来跟踪要写入的行:
Sub Filtration()
Dim writeRow as integer
For Each Cell In Sheets(1).Range("R:R")
'because we don't want to do this for every cell in Column R (There are one million), then exit the loop when we need to
If cell.value = "" Then Exit For
'divide the current cell's value by the the cell one column over's value and compare
If cell.value/cell.offset(,-2).value > .021 Then
'Write this out to the writeRow variable in the other sheet and increment that number by 1 after writing
Sheet1.Rows(cell.row).Copy Destination:=Sheet2.Cells(writeRow, 1)
writeRow = writeRow + 1
End If
Next
Because dividing by zero will cause this code to fail, it may be a good idea to do your division before you check the result in the if
statement. 因为除以零将导致此代码失败,所以在检查
if
语句中的结果之前进行除法可能是个好主意。 You can use a variable to capture the value for later comparison: 您可以使用变量捕获值以供以后比较:
Sub Filtration()
Dim writeRow as integer
Dim percentage as double
For Each Cell In Sheets(1).Range("R:R")
'because we don't want to do this for every cell in Column R (There are one million), then exit the loop when we need to
If cell.value = "" Then Exit For
'Lets make sure we won't be dividing by zero. If we are then set the result to 0
if cell.offset(,-2).value = 0 Then
percentage = 0
else
percentage=cell.value/cell.offset(,-2).value
end if
'divide the current cell's value by the the cell one column over's value and compare
If percentage > .021 Then
'Write this out to the writeRow variable in the other sheet and increment that number by 1 after writing
Sheet1.Rows(cell.row).Copy Destination:=Sheet2.Cells(writeRow, 1)
writeRow = writeRow + 1
End If
Next
Try the code below: 请尝试以下代码:
Sub Filtration()
Dim Cell As Range
With Sheets(1)
For Each Cell In .Range("R1:R" & .Cells(.Rows.Count, "R").End(xlUp).Row)
If Cell.Value / Cell.Offset(, -2).Value > 0.021 Then
.Rows(Cell.Row).Copy
Sheets("Sheet2").Range("A" & Cell.Row).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
End If
Next
End With
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.