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

Roh*_*han 9 excel vba excel-vba

我试图清除单元格中的内容,但其中一些已合并,所以我收到错误

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 
Run Code Online (Sandbox Code Playgroud)

另一个尝试使用.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 
Run Code Online (Sandbox Code Playgroud)

Ror*_*ory 13

你需要Cells不是Range:

ws.Cells(j, l).ClearContents
Run Code Online (Sandbox Code Playgroud)

糟糕 - 忘了合并的位:

        If Cells(j, l).MergeCells Then
            Cells(j, l).MergeArea.ClearContents
        Else
            Cells(j, l).ClearContents
        End If
Run Code Online (Sandbox Code Playgroud)

  • 啊,我的错误, ws.Range("NamedRange").MergeArea.ClearContents 有效。 (2认同)