检查单元格是否在范围内,如果是,return word "Marginal" in Excel VBA
Check if cell is within range, if so, return word "Marginal" in Excel VBA
我正在尝试检查 excel 电子表格的 N 列中的值是否在 -10 到 10 的范围内。如果是,那么 return 单词 "Marginal" 在 O 列中,否则 return 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
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
- 您正在使用 With ... End With 块,但在 Range 调用中忽略了它。
您可以一次将公式放入所有单元格。不用把公式放到最上面的单元格再往下填。
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
我正在尝试检查 excel 电子表格的 N 列中的值是否在 -10 到 10 的范围内。如果是,那么 return 单词 "Marginal" 在 O 列中,否则 return 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
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
- 您正在使用 With ... End With 块,但在 Range 调用中忽略了它。
您可以一次将公式放入所有单元格。不用把公式放到最上面的单元格再往下填。
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