VBA Clear contents column

pantakos

Board Regular
Joined
Oct 10, 2012
Messages
161
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hello all,

I am trying to clear only the numeric values from column E.

With my little knowledge (and help from google) I have created this

Sub ClearContentsITEMS()
ActiveSheet.Range("E1", Range("E" & Columns("E").SpecialCells(xlCellTypeConstants, xlNumbers).Row)).ClearContents
MsgBox "ITEMS form cleared!"
End Sub

And error 1004 ...
What I am doing wrong?

I want to clear all the contents from column E that are Numbers (keep formulas, special char, text etc)

Any help will be appreciated.

Thank you !
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Try this:

VBA Code:
Sub ClearContentsITEMS()
  Range("E1", Range("E" & Rows.Count).End(3)).SpecialCells(xlCellTypeConstants, xlNumbers).ClearContents
  MsgBox "ITEMS form cleared!"
End Sub
 
Upvote 0
@DanteAmor ! thank you for your quick reply, working like a charm.
The problem now is that if there is notthing to clear VBA gives me an error. If I enter data at column E everything is fine
Strange...
Is there a way to check if there is something to clear, then clear, if not thent display a message like "Nothing to Clear!"
And something more, is it possible to include and another column from the same sheet? Lets say Column K
 
Upvote 0
Is there a way to check if there is something to clear, then clear, if not thent display a message like "Nothing to Clear!"
And something more, is it possible to include and another column from the same sheet? Lets say Column K

Try:

VBA Code:
Sub ClearContentsITEMS()
  Dim n As Variant
  On Error Resume Next
  Range("E1", Range("E" & Rows.Count).End(3)).SpecialCells(xlCellTypeConstants, xlNumbers).ClearContents
  n = Err.Number
  If n = 0 Then
    MsgBox "Column E: ITEMS form cleared!"
  Else
    MsgBox "Column E: Nothing to Clear!"
  End If
  On Error GoTo 0
  
  On Error Resume Next
  Range("K1", Range("K" & Rows.Count).End(3)).SpecialCells(xlCellTypeConstants, xlNumbers).ClearContents
  n = Err.Number
  If n = 0 Then
    MsgBox "Column K: ITEMS form cleared!"
  Else
    MsgBox "Column K: Nothing to Clear!"
  End If
  On Error GoTo 0
  
End Sub
 
Upvote 0
Solution
@DanteAmor And one last !
Is it possible to combine both to only have one message (one for cleared and one for not cleared).
Combine mean E & K at the same formula
 
Upvote 0
Is it possible to combine both to only have one message
Try:

VBA Code:
Sub ClearContentsITEMS()
  Dim msg As String
  
  On Error Resume Next
  Range("E1", Range("E" & Rows.Count).End(3)).SpecialCells(xlCellTypeConstants, xlNumbers).ClearContents
  msg = "Column E: " & IIf(Err.Number = 0, "ITEMS form cleared!", "Nothing to Clear!")
  On Error GoTo 0
  
  On Error Resume Next
  Range("K1", Range("K" & Rows.Count).End(3)).SpecialCells(xlCellTypeConstants, xlNumbers).ClearContents
  msg = msg & vbCr & "Column K: " & IIf(Err.Number = 0, "ITEMS form cleared!", "Nothing to Clear!")
  On Error GoTo 0
  
  MsgBox msg
End Sub
 
Upvote 0
My wrong!
Saying combine mean that display one message if both columns are cleared or not, no matter if one of them has data
Hope you understand, either way the above script do the job just fine!
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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