如何清除合并单元格的内容
我正在尝试清除单元格中的内容,但其中一些已合并,所以我收到错误
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
另一个尝试使用.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
您需要不是Range
Cells
:
ws.Cells(j, l).ClearContents
糟糕 – 忘记了合并位:
If Cells(j, l).MergeCells Then Cells(j, l).MergeArea.ClearContents Else Cells(j, l).ClearContents End If
还有一件事你创buildmacros并使用快捷键(ctrl + m),所以你可以select你想合并和清除的单元格也有这个代码:
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
尝试使用
ws.Range(j,l).Clearcontents