Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

How to clear contents of named range in Excel?

This article will show you method of clearing contents of a specified named range as well as clearing contents of all named ranges but keeping the range names in an active worksheet in Excel.

Clear contents of named range with VBA code


Clear contents of named range with VBA code


Please do as follows to clear contents of a specified named range or all named range in the current worksheet.

1. In the worksheet contains the named range you will clear contents from, press the Alt + F11 keys to open the Microsoft Visual Basic for Applications window.

2. In the Microsoft Visual Basic for Applications window, click Insert > Module. Then copy below VBA code into the Code window.

For clearing contents of a specified named range in the worksheet, please apply the below VBA code.

VBA code 1: Clear contents of a specified named range in Excel

Sub Clear_ActiveSheet_Name_Ranges()
    Dim xName As Name
    Dim xInput As String
    Dim xRg As Range
    On Error Resume Next
    xInput = Application.InputBox("Enter the name of the named range you will clear contents from:", "KuTools For Excel", , , , , , 2)
    If xInput = "False" Then Exit Sub
    Application.ScreenUpdating = False
    Set xName = ActiveWorkbook.Names(xInput)
    If Not xName Is Nothing Then
         xName.RefersToRange.Clear
    End If
    Application.ScreenUpdating = True
End Sub

3. Press the F5 key to run the code.

4. Then a Kutools for Excel dialog box pops up. Please enter the name of the named range you will clear contents from, and then click the OK button. See screenshot:

Then all contents in cells of the specified named range are cleared immediately.

For clearing contents of all named ranges in active worksheet, please apply the below VBA code.

VBA code 2: Clear contents of all named ranges in active sheet in Excel

Sub Clear_All_ActiveSheet_Name_Ranges()
    Dim xRange As Range
    Dim xName  As Name
    Dim xReSponse As Long
    On Error Resume Next
    xReSponse = MsgBox("For clearing contents of all named ranges of active sheet in " & ActiveWorkbook.Name _
            & Chr(10) & "('OK' to Delete, 'Cancel' to Quit.)", vbOKCancel, "KuTools For Excel")
    If xReSponse = 2 Then
        MsgBox "User chose to cancel.", vbOKOnly, "KuTools For Excel"
        Exit Sub
    End If
    For Each xName In ActiveWorkbook.Names
        Set xRange = Nothing
        Set xRange = Intersect(ActiveSheet.UsedRange, xName.RefersToRange)
        If Not xRange Is Nothing Then xRange.Clear
    Next xName
    Application.ScreenUpdating = True
End Sub

3. Press the F5 key to run the code, then click the OK button in the Kutools for Excel dialog box as below screenshot shown.

Then the contents of all named ranges are cleared from active sheet.


Related articles:



Recommended Productivity Tools for Excel

Kutools for Excel Helps You Always Finish Work Ahead of Time, and Stand Out From Crowd

  • More than 300 powerful advanced features, designed for 1500 work scenarios, increasing productivity by 70%, give you more time to take care of family and enjoy life.
  • No longer need memorizing formulas and VBA codes, give your brain a rest from now on.
  • Become an Excel expert in 3 minutes, Complicated and repeated operations can be done in seconds, 
  • Reduce thousands of keyboard & mouse operations every day, say goodbye to occupational diseases now.
  • 110,000 highly effective people and 300+ world-renowned companies' choice.
  • 60-day full features free trial. 60-day money back guarantees. 2 years of free upgrade and support.

Brings Tabbed Browsing and Editing to Microsoft Office, Far More Powerful Than The Browser's Tabs

  • Office Tab is designed for Word, Excel, PowerPoint and Other Office Applications: Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.

Be the first to comment.