vba 删除单元格而不将它们向上移动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16046268/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 20:37:52  来源:igfitidea点击:

Delete cells without shifting them up

excelexcel-vbadelete-rowshiftvba

提问by Jill448

I have vba macro to delete a selection of cells. Its shifting the cells up automatically after deleting them. I don't want the cells to be shifted up, as I have a chart below the cells and everything gets disturbed if they are shifted.

我有 vba 宏来删除选定的单元格。删除它们后,它会自动将单元格向上移动。我不希望单元格向上移动,因为我在单元格下方有一个图表,如果它们被移动,一切都会受到干扰。

Here is the code I am using to delete cells

这是我用来删除单元格的代码

ActiveSheet.UsedRange.Select
ActiveSheet.Range("C2:H8").Select
Selection.Delete

Let me know how can I delete them without shifting up.

让我知道如何在不向上移动的情况下删除它们。

回答by P. O.

You could use

你可以用

ActiveSheet.Range("C2:H8").ClearContents 

that'll empty the content without shifting anything.

这将清空内容而不移动任何内容。

回答by adpda

If i understand correctly, I think using ClearContents instead of Delete should provide the functionality you are looking for.

如果我理解正确,我认为使用 ClearContents 而不是 Delete 应该提供您正在寻找的功能。

http://msdn.microsoft.com/en-us/library/office/aa223828%28v=office.11%29.aspx

http://msdn.microsoft.com/en-us/library/office/aa223828%28v=office.11​​%29.aspx

Alternatively, you can specify how you want the cells to shift, if that is better suited to what you are trying to do:

或者,您可以指定您希望单元格如何移动,如果这更适合您要执行的操作:

From: http://msdn.microsoft.com/en-us/library/office/aa223863%28v=office.11%29.aspx

来自:http: //msdn.microsoft.com/en-us/library/office/aa223863%28v=office.11​​%29.aspx

expression.Delete(Shift)

expression Required. An expression that returns a Range object.

Shift Optional Variant. Used only with Range objects. Specifies how to shift cells to replace deleted cells. Can be one of the following XlDeleteShiftDirection constants: xlShiftToLeft or xlShiftUp. If this argument is omitted, Microsoft Excel decides based on the shape of the range.

表达式.Delete(Shift)

表达式 必需。返回 Range 对象的表达式。

Shift 可选变体。仅用于 Range 对象。指定如何移动单元格以替换已删除的单元格。可以是以下 XlDeleteShiftDirection 常量之一:xlShiftToLeft 或 xlShiftUp。如果省略此参数,Microsoft Excel 将根据范围的形状进行决定。

回答by The_Barman

Have you considered just ClearContents?

您是否只考虑过 ClearContents?

ActiveSheet.Range("C2:H8").ClearContents

If you have problems with merged cells try:

如果合并单元格有问题,请尝试:

ActiveSheet.Range("C2:H8").Value = ""

回答by Tomasz

In my case it was easy to use Delete without disturbing row sequence simply starting the loop from the very bottom line towards the header of the selected scope:

在我的情况下,很容易使用 Delete 而不会干扰行序列,只需从最底线开始循环到所选范围的标题:

Cells(rw, 4).Activate
For i = rw To 1 Step -1
   Set Clr = Cells(i, 4)
   If Not Clr.Text Like "R*" Then
    Clr.EntireRow.Delete Shift:=xlUp

   End If
Next i