简体   繁体   English

如果值为负,则清除范围内的单元格内容

[英]Clear cell content in a range if value is negative

I'm triyng to make my macro able to clear cells if their value is negative. 我正在尝试使宏能够清除其值为负的单元格。 In particular, the macro should clear all negative cells in columns H to K until last row in column A. 特别是,宏应清除H至K列中的所有负像元,直到A列中的最后一行。

I tried this code I've found on the net but I really don't know how to extend it to columns I to K (works only for column H). 我尝试了在网上找到的这段代码,但我真的不知道如何将其扩展到I到K列(仅适用于H列)。

Dim LC1 As Long, ColNum1 As Long
Application.ScreenUpdating = False
For ColNum1 = 8 To Cells(1, Columns.Count).End(xlToLeft).Column
  For LC1 = 2 To Cells(Rows.Count, ColNum1).End(xlUp).Row
     If Val(CStr(Cells(LC1, ColNum1))) < 0 Then Cells(LC1, ColNum1) = 0
  Next LC1
Next ColNum1
Application.ScreenUpdating = True

I tried to replicate this code changing each time the value ColNum# to 9, 10 and 11 but it's not working. 我尝试复制此代码,每次将ColNum#的值更改为9、10和11时都无法正常工作。 Why? 为什么?

This should work: 这应该工作:

' Get last row of col A...
Dim lastRow as long, r As Long, c As Long
Range("A1").select
lastRow = Cells.Find("*",SearchOrder:=xlByRows,SearchDirection:=xlPrevious).Row

For r = 2 To lastRow
For c = 8 To 11

    If IsNumeric(Cells(r, c)) Then
        If Cells(r, c) < 0 Then
            Cells(r, c).ClearContents
        End If
    End If     

Next
Next

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

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