vba 如何在Excel VBA中删除范围的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3589399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete values of a range in Excel VBA
提问by guest1
I have a range in excel named " ValuesRange" located at Cell "B5". I have four values: 3,4,5,6 located at cells B6,B7,B8,B9 respectively. Given that B10 is always blank. how can delete the four values one by one? Here is the code that I used:
我在 Excel 中有一个名为“ValuesRange”的范围,位于单元格“B5”处。我有四个值:分别位于单元格 B6、B7、B8、B9 的 3、4、5、6。鉴于 B10 始终为空白。如何将四个值一一删除?这是我使用的代码:
Dim startRange as Range
Set startRange= Range("ValuesRange").offset.(1,0)
While Not IsEmpty(startRange)
startRange.value= " "
startRange=startRange(1,0)
Wend
This code does not work. what it does is that it deletes the first values (3) and then replace it with number 4 and keeps doing that in infinite loop. please help me to fix this code. Thanks a lot!
此代码不起作用。它的作用是删除第一个值 (3),然后用数字 4 替换它,并在无限循环中继续这样做。请帮我修复此代码。非常感谢!
采纳答案by Tahbaza
Here's one way:
这是一种方法:
Public Function ClearCellsBelowValuesRange()
Dim Rng As Excel.Range, offset As Integer
Set Rng = ThisWorkbook.Worksheets("Sheet1").Range("ValuesRange")
offset = 1
Do
Rng.offset(offset, 0).Value = ""
offset = offset + 1
Loop Until IsEmpty(Rng.offset(offset, 0).Value)
End Function
回答by MeLine
If you want just to delete values you can try:
如果您只想删除值,您可以尝试:
startRange.Select
Selection.ClearContents