【发布时间】: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?