简体   繁体   English

Excel VBA清除内容

[英]Excel VBA Clear Contents

I have a column in excel sheet that contains the IF condition ie 我在Excel工作表中有一个包含IF条件的列,即

=If(Cond 1 is TRUE, X, Y)

Now, after using this condition, i get certain values in the column. 现在,使用此条件后,我将在列中获得某些值。 Following format can be considered (these are actual values): 可以考虑以下格式(这些是实际值):

4L  
4L  
4L  


4L 

Note: The two empty cells in the above col are an outcome of the TRUE condition(entry 4 and 5, i entered total 6 entries, two are empty cells ). 注意:上面col中的两个空单元格是TRUE条件的结果(条目4和5,我总共输入了6个条目,其中两个是空单元格)。 Therefore, they are valid. 因此,它们是有效的。 (let me call the above col "A" for future reference) (让我称以上栏为“ A”,以备将来参考)

Now, these empty cells actually contains formulas (the if condition). 现在,这些空单元格实际上包含公式(if条件)。 I want to CLEARCONTENT them using VBA but it is not working. 我想使用VBA 清除它们,但不起作用。

And I'm trying the below code: 我正在尝试以下代码:

If ActiveSheet.Cells(row_no, col_no) = "" Then
ActiveSheet.Cells(row_no, col_no).ClearContents
End If

But this does not work. 但这是行不通的。 I just want to CLEAR CONTENT those empty cells by running a loop over the whole column. 我只想通过在整个列上运行循环来清除那些空单元格。 The cells of the column where TEXT exist (ie 4L), that should be skipped but as soon the code encounters the EMPTY CELL (which actually have an IF condition), it should CLEAR CONTENT it and complete the loop. TEXT存在的列的单元格(即4L)应被跳过,但是一旦代码遇到EMPTY CELL(实际上具有IF条件),它应清除CONTENT并完成循环。 So the final result would be again the same as column "A", the only difference would be that the empty cells will be BLANK now ie they will not have any IF condition. 因此,最终结果将再次与“ A”列相同,唯一的区别是,空单元格现在将变为空白,即它们将不具有任何IF条件。

I do not have problem running the loops but i am not getting how to tell VBA about that empty cell and clear contenting it. 我没有运行循环的问题,但是我没有得到如何将空单元告诉VBA并清除内容的方法。 Hopefully i was able to post a clear query. 希望我能够发布明确的查询。 Thanking in advance. 在此先感谢。

Regards Nayyar 问候纳亚尔

Please try the below sample code it should work.. 请尝试下面的示例代码,它应该可以工作。

Sub test()
'Assuming your data in column  A from A2

Set Rng = Range("A2", Cells(Rows.Count, 1).End(xlUp))

For Each cell In Rng
    If cell.Value = "" Then
    cell.ClearContents
    End If
Next

End Sub

And in your formula =If(Cond 1 is TRUE, X, Y) your not giving any output like "" which will give you blank. 在您的公式=If(Cond 1 is TRUE, X, Y)您没有给出任何类似“”的输出,这将使您空白。 Please update it and then try :) 请更新它,然后尝试:)

Try this as well 也尝试一下

If (Range("A35").Value = "") Then
    Range("A35").Formula = ""
End If

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

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