vba 如何清除合并单元格的内容

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

How to clearcontents of a merged cells

excelvbaexcel-vba

提问by Stupid_Intern

I am trying to clear contents from cells but some of them are merged so I am getting the error

我正在尝试清除单元格中的内容,但其中一些已合并,因此出现错误

1004 :"We cant do that to merged cells"

1004 :“我们不能对合并的单元格这样做”

For l = 4 To 9
    If ws.Cells(j, l).Interior.ColorIndex = 19 Then
         ws.Range(j, l).ClearContents  'Error here
    End If
Next l 

Another Try using .Cellsstill it returns error

另一个尝试使用.Cells仍然返回错误

    For l = 4 To 9
        If ws.Cells(j, l).Interior.ColorIndex = 19 Then
             ws.Cells(j, l).ClearContents  'Error here
        End If
    Next l 

回答by Rory

You need Cellsnot Range:

你需要Cells不是Range

ws.Cells(j, l).ClearContents

Oops - forgot about the merged bit:

糟糕 - 忘记了合并位:

        If Cells(j, l).MergeCells Then
            Cells(j, l).MergeArea.ClearContents
        Else
            Cells(j, l).ClearContents
        End If

回答by Luiz Silva

Try

尝试

ws.Range(j, l).Select
Selection.ClearContents

Worked for me.

对我来说有效。

回答by Vanda Ros

One more thing you create macro and use shortcut key (ctrl + m) so you can select cells that you want merge and clear also.here is this code:

您创建宏并使用快捷键(ctrl + m)的另一件事是,您可以选择要合并和清除的单元格。这是以下代码:

Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+m
'
    With Selection
        .HorizontalAlignment = xlGeneral
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        If .MergeCells = True Then
            .MergeCells = False
        Else
            .MergeCells = True
        End If
    End With
End Sub

回答by Shuva

If Sheet Name.Range("cell no").MergeCells Then Sheet Name.Range("cell no").MergeArea.ClearContents Else Sheet Name.Range("cell no").ClearContents End If

If Sheet Name.Range("cell no").MergeCells Then Sheet Name.Range("cell no").MergeArea.ClearContents Else Sheet Name.Range("cell no").ClearContents End If

回答by Stian Yttervik

Try using

尝试使用

ws.Range(j,l).Clearcontents