Range.ClearContents - also clears formatting - how to fix?

amyfb

New member
Joined
Dec 18, 2020
Messages
24
Reaction score
0
Points
1
Location
Southeastern PA, USA
Excel Version(s)
O365
All the online advice about this function Range.ClearContents states that the formatting will not be cleared, however, I cannot get the format to stay in place.
I tried adding a specific cell formatting code, but that didn't help.

the desired action flow is:
move the selected data from one tab to another tab, and then clear the data from the specified cells on the first tab.

The contents of the originating cells is moved to the second tab with no issue, and, the originating cells are cleared just fine, but the formatting also disappears and the font size reverts to a very small size, lower right justified.

what I want is for the entered contents to be cleared, but the font size and border to stay as formatted.

I'm not sure what to look for that could be getting in the way of this code working. All ideas welcome, thanks! Amyfb

The code I am using looks like this:
Option Explicit
Private Sub CommandButton1_Click()
Dim SKU As String, Qty As String, TFC As String, LTFC As String
Worksheets("dashboard - beta").Select
SKU = Range("D6")
Qty = Range("L2")
TFC = Range("L3")
LTFC = Range("M3")
Worksheets("Costing Log").Select
Worksheets("Costing Log").Range("B4").Select
If Worksheets("Costing Log").Range("B4").Offset(1, 0) <> "" Then
Worksheets("Costing Log").Range("B4").End(xlDown).Select
End If
ActiveCell.Offset(1, 0).Select
ActiveCell.Value = SKU
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = Qty
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = TFC
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = LTFC


Worksheets("dashboard - beta").Select
Worksheets("dashboard - beta").Range("a5:a34").ClearContents
Worksheets("dashboard - beta").Range("L2").ClearContents

Worksheets("dashboard - beta").Range("j10:j34").ClearContents

End Sub

Sub FontSize()
'code for changing font size
Worksheets("dashboard - beta").Range("L2").Font.Size = 22
End Sub
 
the solution was a conflicting snip of code in another module with a duplicate set of range.clearContents . I removed that and the problem resolved.
thanks to the folks at stackflow for the idea.
 
Just finished testing your macro and I confirm that the .ClearContents of that macro was doing it's job(y).
So the problem was elsewhere ... but you found it by yourself ;).
 
Back
Top