简体   繁体   English

如果值小于特定值,则从列中删除值

[英]Deleting values from column if they are smaller than a specific value

I want to write a macro which deletes values from column if they are smaller than a specific value indicated somewhere else in the worksheet.我想编写一个宏,如果它们小于工作表中其他地方指示的特定值,则从列中删除值。

My try我的尝试

    Sub clearsmall()
Dim r As Range
num1 = Cells(7, 5).Value
For Each r In Selection
If r.Value < num1 Then
r.Clear
End If
Next
End Sub

the problem is that thois requires the range to be selected by the user.问题是 thois 需要用户选择范围。 Hoever, I want to specify it in the macro - using something like但是,我想在宏中指定它 - 使用类似的东西

Set r = Range("C16:C92")

how would I need to change the code?我需要如何更改代码?

Sub clearsmall()
    Dim cell As Range, r As Range

    Set r= Range("C16:C92")
    num1 = Cells(7, 5).Value
    For Each cell In r
        If cell.Value < num1 Then cell.Clear
    Next
End Sub

You can also shorten it down to您也可以将其缩短为

Sub clearsmall()
    Dim cell As Range

    Num1 = Cells(7, 5).Value
    For Each cell In Range("C16:C92")
        If cell.Value < num1 Then cell.Clear
    Next
End Sub

Finally, you may want to use ClearContents instead of Clear, to clear cell value only, which is faster.最后,您可能希望使用 ClearContents 而不是 Clear 来仅清除单元格值,这样会更快。 This will not touch formats这不会触及格式

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel VBA-循环计算大于/小于特定值的单元格值 - Excel VBA - Looping to count cell values larger than / smaller than specific value 将某些值从一个列复制到另一列并删除原始值 - Copy certain values from one to another column and deleting the original value 根据特定列中的文本值删除行 - Deleting Rows Based on Text Values in Specific Column 从a列中找到特定数字并求和大于 - find specific number from column a and sum up values greater than 如果单元格值的年份小于特定年份,则删除 Excel 行 - Deleting an Excel Row if the year of the cell value is less than a specific year 如何根据特定列的值从 CSV 文件中获取 select 值 - How to select values from a CSV file depending on the value of a specific column 根据单元格值和列标题删除特定行 - Deleting specific rows according to the cell value and column heading 如果大于特定日期,如何从列中返回或查找值,而不会留下任何空白? - How to return or look up values from a column if greater than a specific date, without leaving any gaps? 列中的Excel / VBA宏ClearContents如果值小于10000,则在新文件中打开/保存/关闭 - Excel/VBA Macro ClearContents in column if value smaller than 10000, open/save in new file/close 如何在 Excel 的雷达图中将最大值设置为 5 而所有值都小于 4 - How to set the max value at 5 in a radar graph in Excel whereas all the values are smaller than 4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM