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"
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 .Cells
still it returns error
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
You need Cells
not 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
Try
Range("A1:A20").Value = ""
No matter cells are merged or not this will work everytime.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With