[英]Excel if value in range is larger than value in a cell, clear value of cell in range
I want to type a number in B3. 我想在B3中键入一个数字。 If a number in the range B8 to B200 is larger than the value in B3, the cell in the range whose value is larger than that of B3's value will be cleared of its contents. 如果B8到B200范围内的数字大于B3中的值,则将清除其值大于B3值的范围内的单元格的内容。 (I tried doing this with the code attached) (我尝试通过附加的代码来执行此操作)
(OR: If a value is entered in B3, a drop down of all the values that are less than or equal to the value in B3 is generated (that way there is no way to exceed the value in B3).) (或:如果在B3中输入了一个值,则会生成所有小于或等于B3中的值的下拉列表(这样就无法超过B3中的值)。)
Sub ProcessLineNumberValidation()
Dim QTY As Integer
Dim ProcessNum As Integer
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Ozone Generator")
QTY = ws.Sheets("Ozone Generator").Cells(3, 2).Value
For i = 8 To 200
ProcessNum = ws.Sheets("Ozone Generator").Cells(i, 2).Value
If ProcessNum > QTY Then
ws.Sheets("Ozone Generator").Cells(i, 2).ClearContents
End If
Next i
End Sub
First, you use Set ws = Thisworkbook.Sheets("Ozone Generator")
首先,使用Set ws = Thisworkbook.Sheets("Ozone Generator")
Then, you use ws.Sheets("Ozone Generator")
on multiple lines which is the likely source of your problem. 然后,在多行上使用ws.Sheets("Ozone Generator")
,这可能是问题的根源。 If you substitute ws
back in to the above line of code you get: 如果将ws
替换回上面的代码行,则会得到:
Thisworkbook.Sheets("Ozone Generator").Sheets("Ozone Generator")
Which is not a valid cell reference. 这不是有效的单元格引用。 Just use ws.Cells(....
which will result in the below code (corrected for problem and applied more standard spacing, ordering, & indentation methods to code) 只需使用ws.Cells(....
将产生以下代码(针对问题进行了更正,并应用了更多的标准间距,排序和缩进方法进行编码)
Option Explicit
Sub ProcessLineNumberValidation()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Ozone Generator")
Dim QTY As Integer, ProcessNum as Integer, i
QTY = ws.Cells(3, 2).Value
For i = 8 To 200
ProcessNum = ws.Cells(i, 2).Value
If ProcessNum > QTY Then
ws.Cells(i, 2).ClearContents
End If
Next i
End Sub
You can consider this alternative that has the same output but will be quicker. 您可以考虑使用具有相同输出但会更快的替代方法。 For Each
loops are faster than For i =
loops when looping through ranges like this. 当遍历这样的范围时, For Each
循环比For i =
循环快。 Also toggling off ScreenUpdating
will make this look cleaner from a user standpoint. 从用户的角度来看,也可以关闭ScreenUpdating
使其看起来更干净。
Sub ProcessLineNumberValidation()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Ozone Generator")
Dim MyRange As Range: Set MyRange = ws.Range("B8:B200")
Dim MyCell As Range
Application.ScreenUpdating = False
For Each MyCell In MyRange
If MyCell > ws.Cells(3, 2) Then MyCell.ClearContents
Next MyCell
Application.ScreenUpdating = True
End Sub
this could be a work for Autofilter()
: 这可能是Autofilter()
的工作:
Sub ProcessLineNumberValidation()
With ThisWorkbook.Sheets("Ozone Generator").Range("B7:B200") 'reference your sheet range B7:B200 (B7 is the header, values are from B8 downwards)
.AutoFilter field:=1, Criteria1:=">" & .Parent.Range("B3").Value2 ' filter referenced range with values greatre than referenced range sheet cell B3
If Application.WorksheetFunction.Subtotal(103, .Resize(, 1)) > 1 Then .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).ClearContents ' clear any filtered cell other than header
.Parent.AutoFilterMode = False ' remove autofilter
End With
End Sub
and if you want ProcessLineNumberValidation()
being called on every "Ozone Generator" sheet B3 cell change then place this code in that sheet code pane: 如果希望在每个“臭氧发生器”工作表B3单元格更改中调用ProcessLineNumberValidation()
,则将此代码放在该工作表代码窗格中:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$3" Then 'if cell B3 has changed
Application.EnableEvents = False ' disable events to prevent this event handler being called in a possibly infinite loop
ProcessLineNumberValidation ' process your range
Application.EnableEvents = True ' enable events back on
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.