[英]Excel VBA code to clear data from one cell based on another cell value
I'm self taught and having an issue with something I thought would be easy. 我是自学成才,遇到一些我认为很容易的问题。 I have a spreadsheet that i need to loop through column O and find any cell with data and clear contents of the corresponding cell in column H. The spread is sheet is ever changing so the last row of data is always changing. 我有一个电子表格,我需要遍历O列并查找任何包含数据的单元格,并清除H列中相应单元格的内容。跨页是不断变化的,因此最后一行数据总是在变化。 Below is the code I have been playing with but can't seem to get to work. 以下是我一直在使用但似乎无法使用的代码。 Any help would be awesome.. 任何帮助都是极好的..
Dim deleterate As Long
Dim ws As Worksheet
' set object
Set ws = Sheets("Import file")
' loop through the data to find the $$ amounts in column o
For deleterate = ws.Range("O" & Row.count).End(xlUp).Row To 1 Step -1
' indentify values in O which have value
If ws.Range("O" & deleterate).Value <> 0 Then
Cells(0, 7).ClearContents
End If
You can't have a row of 0
, so your line 您不能有0
行,所以您的行
Cells(0, 7).ClearContents
will give an error. 将给出一个错误。 You should also always qualify which sheet you are referring to, unless you know you want to use the ActiveSheet
. 除非您知道要使用ActiveSheet
,否则还应该始终限定要引用的工作表。
So you should use 所以你应该使用
ws.Cells(deleterate, 8).ClearContents
to specify the correct row and column (because "H" is the 8th column, not the 7th) or even 指定正确的行和列(因为“ H”是第8列,而不是第7列),甚至
ws.Cells(deleterate, "H").ClearContents
or, to be consistent with your previous line (the If
statement), you could use 或者,为了与上一行( If
语句)保持一致,可以使用
ws.Range("H" & deleterate).ClearContents
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.