【问题标题】:Check if cell is within range, if so, return word "Marginal" in Excel VBA检查单元格是否在范围内,如果是,则在 Excel VBA 中返回单词“Marginal”
【发布时间】:2017-06-23 17:43:53
【问题描述】:

我正在尝试检查 Excel 电子表格的 N 列中的值是否在 -10 到 10 的范围内。如果是,则在 O 列中返回单词“Marginal”,否则返回 N 中的值。我不知道该怎么做。这是我到目前为止的代码:

Sub SearchFolders()
'UpdatebySUPERtoolsforExcel2016
    Dim xOut As Worksheet
    Dim xWb As Workbook
    Dim xWks As Worksheet
    Dim InterSectRange As Range

    Application.ScreenUpdating = False
    Set xWb = ActiveWorkbook
    For Each xWks In xWb.Sheets

        xRow = 1
        With xWks
            .Activate                    'Activating the worksheet
            .Cells(xRow, 12) = "Meas-LO"
            .Cells(xRow, 13) = "Meas-Hi"
            .Cells(xRow, 14) = "Min Value"
            .Cells(xRow, 15) = "Marginal"
            LastRow = ActiveSheet.UsedRange.Rows.Count
            Range("L2").Formula = "=G2+I2"
            Range("L2").AutoFill Destination:=Range("L2:L" & LastRow)
            Range("M2").Formula = "=I2-F2"
            Range("M2").AutoFill Destination:=Range("M2:M" & LastRow)
            Range("N2").Formula = "=min(L2,I2)"
            'Set InterSectRange = Application.Intersect(rng1, rng2)
            'If
            Range("N2").AutoFill Destination:=Range("N2:N" & LastRow)
         End With
         Application.ScreenUpdating = True 'turn it back on

     Next xWks
End Sub

【问题讨论】:

  • 为什么会有 Next xWks?你真的关心每个工作表吗?我可以重构你的代码吗?
  • 我有这个是因为我想在工作簿中的所有工作表上执行计算和结果。谢谢!
  • 在您的代码中,您将内容发送到 lastRow,如果您想将计算附加到同一行,这应该是 lastCol。
  • 顺便说一句,除非 G2 为负,否则=min(L2,I2)(即=min(G2+I2,I2))不会总是 I2?

标签: vba excel equation


【解决方案1】:
For Each xWks In xWb.Sheets

    xRow = 1
    With xWks
        '.Activate                    '<< not required, and best avoided
        .Cells(xRow, 12) = "Meas-LO"
        .Cells(xRow, 13) = "Meas-Hi"
        .Cells(xRow, 14) = "Min Value"
        .Cells(xRow, 15) = "Marginal"
        LastRow = .UsedRange.Rows.Count
        .Range("L2").Formula = "=G2+I2"
        .Range("L2").AutoFill Destination:=.Range("L2:L" & LastRow)
        .Range("M2").Formula = "=I2-F2"
        .Range("M2").AutoFill Destination:=.Range("M2:M" & LastRow)
        .Range("N2").Formula = "=min(L2,I2)"
        .Range("N2").AutoFill Destination:=.Range("N2:N" & LastRow)

        .Range("O2").Formula = "=IF(AND(N2>=-10, N2<=10), ""Marginal"", N2)"
        .Range("O2").AutoFill Destination:=.Range("O2:O" & LastRow)

     End With
     Application.ScreenUpdating = True 'turn it back on

 Next xWks

【讨论】:

    【解决方案2】:
    1. 您正在使用 With ... End With 块,但在 Range 调用中忽略了它。
    2. 您可以一次将公式放入所有单元格中。无需将公式放入顶部单元格并填写。

      With xWks
          .Cells(xRow, 12).resize(1, 4) = array("Meas-LO", "Meas-Hi", "Min Value", "Marginal")
          LastRow = application.max(.cells(.rows.count, "F").end(xlup).row, _
                                    .cells(.rows.count, "G").end(xlup).row, _
                                    .cells(.rows.count, "I").end(xlup).row, _
                                    .cells(.rows.count, "L").end(xlup).row)
          .Range("L2:L" & LastRow).Formula = "=G2+I2"
          .Range("M2:M" & LastRow").Formula = "=I2-F2"
          .Range("N2:N" & LastRow).Formula = "=min(L2,I2)"
      End With
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-19
      • 1970-01-01
      • 2012-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-27
      • 1970-01-01
      相关资源
      最近更新 更多