Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing Formats From Entire Worksheet with VBA

Tags:

excel

vba

Using VBA, I am trying to Clear all Formats all entire of the active Worksheet.

Dim ws As Worksheet
 Set ws = ThisWorkbook.ActiveSheet
 With ws
           .ClearFormats
 End With

But I am getting following error:

enter image description here

However using the same code with .UsedRange works fine as:

With .UsedRange
    .ClearFormats
End With

Can you please let me know how fix this to format entire sheet!

like image 628
Behseini Avatar asked Dec 30 '13 04:12

Behseini


People also ask

Can you clear an entire worksheet in Excel?

To clear all contents, formats, and comments that are contained in the selected cells, click Clear All. To clear only the formats that are applied to the selected cells, click Clear Formats. To clear only the contents in the selected cells, leaving any formats and comments in place, click Clear Contents.

What does clear do in VBA?

Excel VBA Clear Contents. ClearContents is a method in VBA used to delete or remove the values stored in the cells provided to it. This method makes the cell range empty. It is used with the range property to access the specified cell range.

How do I remove formatting from a range of cells?

To remove cell formatting in Excel, select the cells from which you want to remove all of the formatting. Then click the “Home” tab in the Ribbon. Then click the “Clear” button in the “Editing” button group. Finally, select the “Clear Formats” command from the drop-down menu that appears.

How do I clear contents in Excel without deleting formatting VBA?

First, select the cells of your dataset and press ALT+F11 to open the VBA window. After that, press CTRL+G. It will open the Immediate Window. The code will clear the contents from your selected cells without deleting the formula.


1 Answers

ClearFormats is a method of a Range, wereas ActiveSheet is a Worksheet

You could use

ThisWorkbook.ActiveSheet.Cells.ClearFormats

(Cells is a range of all the cells on a worksheet)

like image 70
chris neilsen Avatar answered Sep 20 '22 15:09

chris neilsen