Vba to Clear Cell Content But Retain The Formatting With Sheet Name

muhammad susanto

Well-known Member
Joined
Jan 8, 2013
Messages
2,077
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
hi all...
i,m looking formula code to clear content from a sheet with name sheet "Register" cause i have several sheets
and only clear data in specific cell A2 : J30 but keep Retain The Formatting
i found 2 code but i don't know to modified
VBA Code:
Sub sbClearEntireSheetOnlyData()
Sheets("Register").Cells.ClearContents
End Sub
and
VBA Code:
Sub ClearWorksheets()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
    If Right(ws.Name, 4) = "register" Then ws.Cells.ClearContents
Next
End Sub
or maybe another code

any helping, appreciated with greatly.

.susanto
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
ClearContents only removes the values and does not change the cell formatting. The code below will remove the values from the range you provided on sheet "Register"

VBA Code:
Sub sbClearEntireSheetOnlyData()
Sheets("Register").Range("A2:J30").ClearContents
End Sub
 
Upvote 0
ClearContents only removes the values and does not change the cell formatting. The code below will remove the values from the range you provided on sheet "Register"

VBA Code:
Sub sbClearEntireSheetOnlyData()
Sheets("Register").Range("A2:J30").ClearContents
End Sub
hi Jeffrey thank you, but can that code adding script Undo after running your code
 
Upvote 0
how to make code above running in active sheet/current sheet not only specific name sheet and show message box " enter your range that your clear...." e.g. C2:F30 or D3:J100 etc..
 
Upvote 0
hi..
how to modified that code that work in active sheet & can show box "Enter your range to clear.."
VBA Code:
Sub sbClearEntireSheetOnlyData()
Sheets("Register").Range("A2:J30").ClearContents
End Sub
 
Upvote 0
i have trying to make code like this
VBA Code:
Sub clearcontains()
 Dim wkbCrntWorkBook As Workbook
    Dim wkbSourceBook As Workbook
    Dim rngSourceRange As Range
        Set wkbCrntWorkBook = ActiveWorkbook
          Set wkbSourceBook = ActiveWorkbook
            Set rngSourceRange = Application.InputBox(prompt:="Select range", Title:="Range", Default:="", Type:=8).ClearContents
            End Sub

but i don't know how to add code/statement "clear contents" and keep retain formula & formatting
 
Last edited:
Upvote 0
hi man..
i have do and get this code
VBA Code:
Sub clearcontains()
 Dim wkbCrntWorkBook As Workbook
    Dim wkbSourceBook As Workbook
    Dim rngSourceRange As Range
        Set wkbCrntWorkBook = ActiveWorkbook
          Set wkbSourceBook = ActiveWorkbook
            Set rngSourceRange = Application.InputBox(prompt:="Select range", Title:="Range", Default:="", Type:=8).SpecialCells(xlCellTypeConstants).ClearContents
                   End Sub
but after running code in the last moment show message "object required"
how to don't disappear/remove it that message.
 

Attachments

  • objek.png
    objek.png
    5.2 KB · Views: 7
Upvote 0

Forum statistics

Threads
1,215,758
Messages
6,126,717
Members
449,332
Latest member
nokoloina

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top