简体   繁体   English

如何基于2个单元格的百分比比较来复制整行

[英]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. 有几件事。

  1. Avoid .select that is a user interface thing. 避免.select是用户界面。 VBA doesn't need to "Select" anything. VBA不需要“选择”任何内容。 You just point it at a range or a cell or a object and do what you need it to do. 您只需将其指向某个范围,一个单元格或一个对象,然后执行所需的操作即可。
  2. Avoid activesheet and activeworkbook . 避免使用activesheetactiveworkbook 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. 当您编写几百行代码时,不要冒险假设您知道在那个时间点将是活动工作表或活动工作簿。 Don't trust it. 不要相信 Be explicit ThisWorkbook.Sheets("mysheetname") or similar. 是明确的ThisWorkbook.Sheets("mysheetname")或类似名称。
  3. Do the math in VBA. 在VBA中进行数学运算。 Don't call formula or whatever. 不要调用formula或任何其他内容。 VBA is good at it. VBA擅长于此。

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.

相关问题 根据多个单元格中的条件将一行中的单元格(不是整行)复制到另一个工作表 - Copy cells in a row (not entire row) based on criteria in multiple cells to another sheet 如果这些列中的单元格满足条件,如何逐步比较两个列的值并复制整个行 - How to compare two column values incrementally and copy entire row if the cells in those columns meet a condition 根据行计数宏复制单元格 - Copy cells based on row count macro 比较范围并复制整个行(如果某些单元格匹配)? - Comparing ranges and copy entire row, when some cells match? Excel 2010-单元格的宏复制范围,而不是整个行 - Excel 2010 - Macro Copy range of cells instead of entire row 通过检查条件复制整行并粘贴到不同范围的单元格中 - Copy entire row by checking condition and paste into different ranges of cells aside 如何根据来自两个不同 Excel 表匹配的两个单元格返回整行? - How to return the entire row based on two cells from two different Excel sheets matching? 如何遍历特定列中的单元格并根据其内容删除整行? - How do I loop through cells in a specific column and delete the entire row based on its contents? Excel:如何根据其中一列中的最大值选择表格的一行,然后复制该行的单元格? - Excel: How can I pick one row of a table based on the max value in one of the columns, and then copy cells of that row? 如何基于该行中某个单元格的值复制行值? - How can I copy row values based on the value of one of the cells in that row?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM