如何根据2个单元格的{Percentage比较

时间:2017-12-07 14:39:29

标签: excel vba

如果单元格大于同一行中另一个单元格的2%,我试图将整行复制到另一张表格。这就是我所困扰的,根据比较2个细胞的公式获得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
编辑:感谢@JNevil& @ShaiRado和他们提供的帮助,我想我只是停留在宏返回溢出。我认为percentage = Cell.Value / Cell.Offset(, -2).Value正在混淆/冲突一些如何并且始终相信percentage = 0。还有一个问题是我的Sheet1包含空行,标题以及未来更新时可能出现的负数。完整代码如下:

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

2 个答案:

答案 0 :(得分:2)

因为只有一周写VBA,你在这里做得很好。大多数人在学习时会避免像瘟疫这样的For循环。

有几件事。

  1. 避免.select这是一个用户界面的事情。 VBA并不需要&#34;选择&#34;任何东西。您只需将其指向某个范围或单元格或对象,然后执行您需要它执行的操作。
  2. 避免使用activesheetactiveworkbook。当您有几百行代码时,不要冒险假设您知道该时间点的活动工作表或活动工作簿。不要相信它。明确ThisWorkbook.Sheets("mysheetname")或类似。
  3. 在VBA中进行数学运算。别打电话给formula或其他什么。 VBA很擅长。
  4. 以下内容应该让你进入球场:

    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
    

    您可能希望在不跳跃行的情况下写入其他工作表。在这种情况下,您可以使用变量来跟踪要写入的行:

    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
    

    因为除以零将导致此代码失败,所以在检查if语句中的结果之前进行除法可能是个好主意。您可以使用变量捕获该值以供以后比较:

    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
    

答案 1 :(得分:1)

尝试以下代码:

const data = {
    "prtg-version": "17.3.33.2830",
    "treesize": 4,
    "histdata": [{
        "***datetime***": "04.12.2017 16:35:08",
        "datetime_raw": 43073.5660648727,
        "value": "111.983 KByte",
        "value_raw": 114670936.0000,
        "***value***": "30.579 kbit/s",
        "value_raw": 3822364.5333,
        "value": "107.893 KByte",
        "value_raw": 110482503.0000,
        "value": "29.462 kbit/s",
        "value_raw": 3682750.1000,
        "value": "4.090 KByte",
        "value_raw": 4188433.0000,
        "value": "1.117 kbit/s",
        "value_raw": 139614.4333,
        "coverage": "100 %",
        "coverage_raw": 10000
    }, {
        "***datetime***": "04.12.2017 16:35:38",
        "datetime_raw": 43073.5664120718,
        "value": "85.464 KByte",
        "value_raw": 87514907.0000,
        "***value***": "23.345 kbit/s",
        "value_raw": 2918136.2788,
        "value": "81.993 KByte",
        "value_raw": 83960378.0000,
        "value": "22.397 kbit/s",
        "value_raw": 2799612.4708,
        "value": "3.471 KByte",
        "value_raw": 3554529.0000,
        "value": "948 kbit/s",
        "value_raw": 118523.8079,
        "coverage": "100 %",
        "coverage_raw": 10000
    }]
}

const result = data.histdata.map(hist => {
  const datetime = hist['***datetime***'].split(' ')[1];
  const value = hist['***value***'];
  return `${datetime},${value}`;
});

console.log(result)