Метод Range.Clear для полной очистки диапазона ячеек из кода VBA Excel. Методы очистки отдельных свойств и их групп в ячейках. Примеры использования.
Методы очистки ячеек
Метод | Очищаемые свойства | Примечание |
---|---|---|
Range.Clear | Почти все свойства | Ширина и высота ячеек не изменяются |
Range.ClearComments | Комментарии | Для Excel в составе Office 365 |
Range.ClearContents | Формулы и значения | Исходное форматирование сохраняется |
Range.ClearFormats | Свойства, задающие форматы | В том числе отмена объединения ячеек |
Range.ClearHyperlinks | Гиперссылки | Текст и форматирование сохраняются |
Range.ClearNotes | Примечания и заметки | Примечания – для локальных программ Excel, заметки – для Excel в составе Office 365 |
Range.ClearOutline | Структура данных | Смотрите, что такое структурирование данных |
Range – выражение, возвращающее диапазон ячеек.
Примеры использования
1. Удаление гиперссылки из ячейки A1
Cells(1, 1).ClearHyperlinks
2. Очистка диапазона A1:L50 от формул и значений
Range("A1:L50").ClearContents
3. Очистка всех свойств ячеек в столбцах A:K
Columns("A:K").Clear
4. Очистка форматирования ячеек в строках 1:20
Rows("1:20").ClearFormats
Методы очистки диапазонов ячеек в VBA Excel возвращают очищаемые свойства ячеек к значениям по умолчанию. К таким, как на вновь созданном стандартном рабочем листе. При любых методах очистки высота строк и ширина столбцов не изменяются.
Фразы для контекстного поиска: очистка ячеек, очистка ячейки, очистка формул, очистка от формул, удаление формул, очистка значений, удаление значений, очистка форматов, удаление форматирования, удаление форматов.
Return to VBA Code Examples
This tutorial will demonstrate how to clear the formatting of a cell or entire range of cells.
You can clear cell formatting by using the ClearFormats method of the Range Object.
Note: Instead, if you’d like to clear everything about the cell use .Clear instead.
Clear Formats From a Range of Cells
This will clear Cell Formatting for Range A1:C1.
Sub ClearFormats()
Range("A1:C1").ClearFormats
End Sub
If you’d like to clear the formatting of an entire worksheet, use the Cells Object instead:
Sub ClearSheetFormats()
Cells.ClearFormats
End Sub
If you are working with an extremely large range in a large Workbook, Excel could potentially freeze. you could also loop through each cell to prevent Excel from freezing.
Loop through Cells and Clear Formats
The following example will eliminate the formatting for each cell in a range using a loop:
Public Sub RemoveFormatting()
Dim c As Range
For Each c In Range("A:A")
c.ClearFormats
Next c
End Sub
VBA Coding Made Easy
Stop searching for VBA code online. Learn more about AutoMacro – A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!
Learn More!
OBJECTS
Worksheets: The Worksheets object represents all of the worksheets in a workbook, excluding chart sheets.
Range: The Range object is a representation of a single cell or a range of cells in a worksheet.
PREREQUISITES
Cells to clear: This VBA code clears all of the formats in range («A1:B2»), therefore if you are using the exact VBA code the cells in which you want to clear the formatting need to be captured in this range.
Worksheet Name: Have a worksheet named Analysis.
ADJUSTABLE PARAMETERS
Cells to clear: Select the range of cells that you want to clear by changing the range («A1:B2») in the VBA code to any range in the worksheet that contains the cells in which you want to remove the formatting.
Worksheet Selection: Select the worksheet that contains the cells in which you want to remove the formatting by changing the Analysis worksheet name, in the VBA code, to any worksheet in the workbook.
VBA ClearFormats in Excel Explained with Examples
We can use VBA to Clear only the Formats of a specific range, cells or entire worksheet. we can use ClearFormats method of Range Object. Rang.ClearFormats method will clear only the formats of the cells or range. And the data and comments will not be deleted.
Excel VBA to ClearFormats a Range – Syntax
Here is the syntax to clear the formats of a range. You can clear the formats in any range using VBA Range.ClearFormats method.
Range(“YourRange”).ClearFormats
Excel VBA to ClearFormats in a Range – Examples
The below macro will show you how to clear the formats in a range using VBA. In this example, we are clearing the formats of range “A2 to D10” using VBA.
Sub VBA_Clear_Formats_Range() Range("A2:D10").ClearFormats End Sub
Excel VBA to Clear Formats in a Range – Instructions
Please follow the below step by step instructions to execute the above mentioned VBA macros or codes:
- Open an Excel Workbook from your start menu or type Excel in your run command
- Enter some data in any cells in range “A10 to D10” to test this macro. Also format some cell to test this macro.
- Press Alt+F11 to Open VBA Editor or you can go to Developer Table from Excel Ribbon and click on the Visual Basic Command to launch the VBA Editor
- Insert a Module from Insert Menu of VBA
- Copy the above code (for clearing the formats in a range using VBA) and Paste in the code window(VBA Editor)
- Save the file as Macro Enabled Workbook (i.e; .xlsm file format)
- Press ‘F5′ to run it or Keep Pressing ‘F8′ to debug the code line by line.
Now you can observe that the formats in the range “A2 to D10” are cleared. You can apply any formats and background color, and try this macro. This will clear only the formats and the data, comments will be remained same.
Range.ClearFormats:Will clear only the formats of a range, remaining elements untouched.
Range.ClearContents: Will clear only the content/data of the range, formats will remain same.
Range.Clear: Will clear the data including cell formats.
Range.ClearComments: Will clear only the comments, formats and data will remain then same.
A Powerful & Multi-purpose Templates for project management. Now seamlessly manage your projects, tasks, meetings, presentations, teams, customers, stakeholders and time. This page describes all the amazing new features and options that come with our premium templates.
Save Up to 85% LIMITED TIME OFFER
All-in-One Pack
120+ Project Management Templates
Essential Pack
50+ Project Management Templates
Excel Pack
50+ Excel PM Templates
PowerPoint Pack
50+ Excel PM Templates
MS Word Pack
25+ Word PM Templates
Ultimate Project Management Template
Ultimate Resource Management Template
Project Portfolio Management Templates
- Excel VBA to ClearFormats a Range – Syntax
- Excel VBA to ClearFormats in a Range – Examples
- Excel VBA to Clear Formats in a Range – Instructions
VBA Reference
Effortlessly
Manage Your Projects
120+ Project Management Templates
Seamlessly manage your projects with our powerful & multi-purpose templates for project management.
120+ PM Templates Includes:
Effectively Manage Your
Projects and Resources
ANALYSISTABS.COM provides free and premium project management tools, templates and dashboards for effectively managing the projects and analyzing the data.
We’re a crew of professionals expertise in Excel VBA, Business Analysis, Project Management. We’re Sharing our map to Project success with innovative tools, templates, tutorials and tips.
Project Management
Excel VBA
Download Free Excel 2007, 2010, 2013 Add-in for Creating Innovative Dashboards, Tools for Data Mining, Analysis, Visualization. Learn VBA for MS Excel, Word, PowerPoint, Access, Outlook to develop applications for retail, insurance, banking, finance, telecom, healthcare domains.
Page load link
3 Realtime VBA Projects
with Source Code!
Go to Top
Is it possible to clear Excel cell format and content (using VBA) without altering the “NumberFormat
” of the given cell.
I’ve tried with “.ClearContents
” & “.ClearFormats
”, but ClearFormats removed the NumberFormat of the cells too.
Please suggest.
asked Mar 10, 2016 at 17:38
2
You could do something like this:
Dim t
t = Range("A1").NumberFormat
Range("A1").ClearFormats
Range("A1").NumberFormat = t
It will save the number format then reapply it after clearing everything else.
answered Mar 10, 2016 at 18:18
Scott CranerScott Craner
146k9 gold badges47 silver badges80 bronze badges
3