如果范围内的值大于单元格中的值,则Excel,清除范围内的单元格值

时间:2018-08-18 19:27:24

标签: excel vba if-statement

我想在B3中键入一个数字。如果B8到B200范围内的数字大于B3中的值,则将清除其值大于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

2 个答案:

答案 0 :(得分:4)

首先,您使用Set ws = Thisworkbook.Sheets("Ozone Generator")
然后,在多行上使用ws.Sheets("Ozone Generator"),这可能是问题的根源。如果将ws替换为上面的代码行,则会得到:

Thisworkbook.Sheets("Ozone Generator").Sheets("Ozone Generator")

这不是有效的单元格引用。只需使用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

您可以考虑这种替代方法,它具有相同的输出,但是会更快。当遍历这样的范围时,For Each循环比For i =循环快。从用户的角度来看,也可以切换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

答案 1 :(得分:3)

这可能是from PyQt5.QtWidgets import QCompleter, QPlainTextEdit, QApplication, QWidget, QHBoxLayout import sys from PyQt5.QtCore import Qt, pyqtSignal from PyQt5.QtGui import QTextCursor, QFont, QTextOption import keyword class Completer(QCompleter): insertText = pyqtSignal(str) def __init__(self, myKeywords=None, parent=None): myKeywords = keyword.kwlist QCompleter.__init__(self, myKeywords, parent) self.activated.connect(self.changeCompletion) def changeCompletion(self, completion): if completion.find("(") != -1: completion = completion[:completion.find("(")] print(completion) print("completion is " + str(completion)) self.insertText.emit(completion + " ") self.popup().hide() class MyTextEdit(QWidget): def __init__(self, *args): super().__init__(*args) font = QFont() font.setPointSize(12) self.editor = QPlainTextEdit() self.setFont(font) self.completer = None self.hbox = QHBoxLayout(self) self.editor.textChanged.connect(self.complete) self.hbox.addWidget(self.editor) def setCompleter(self, completer): if self.completer: print("completer is: " + str(completer)) self.disconnect() if not completer: print("completer is: " + str(completer)) return completer.setWidget(self) completer.setCompletionMode(QCompleter.PopupCompletion) completer.setCaseSensitivity(Qt.CaseInsensitive) self.completer = completer self.completer.insertText.connect(self.insertCompletion) def insertCompletion(self, completion): tc = self.editor.textCursor() extra = (len(completion) - len(self.completer.completionPrefix())) tc.movePosition(QTextCursor.Left) tc.movePosition(QTextCursor.EndOfWord) tc.insertText(completion[-extra:]) self.editor.setTextCursor(tc) def textUnderCursor(self): tc = self.editor.textCursor() tc.select(QTextCursor.WordUnderCursor) return tc.selectedText() def complete(self): completionPrefix = self.textUnderCursor() print("completion prefix is: " + str(completionPrefix)) self.completer.setCompletionPrefix(completionPrefix) popup = self.completer.popup() popup.setCurrentIndex( self.completer.completionModel().index(0, 0)) cr = self.editor.cursorRect() cr.setWidth( self.completer.popup().sizeHintForColumn(0) + self.completer.popup().verticalScrollBar().sizeHint().width()) self.completer.complete(cr) if __name__ == "__main__": app = QApplication(sys.argv) completer = Completer() te = MyTextEdit() te.setCompleter(completer) te.show() sys.exit(app.exec_()) 的作品:

Autofilter()

,如果您想在每个“臭氧发生器”工作表B3单元格更改中调用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 ,请将该代码放在该工作表代码窗格中:

ProcessLineNumberValidation()