简体   繁体   中英

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. 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. (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).)

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")
Then, you use ws.Sheets("Ozone Generator") on multiple lines which is the likely source of your problem. If you substitute ws back in to the above line of code you get:

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)

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. Also toggling off ScreenUpdating will make this look cleaner from a user standpoint.

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() :

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM