Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 62
Default Macro to clear contents of certain cells

I have a range of cells that contains mostly numeric values but some cells
are populated with an alpha dash (-). I would like to clear the contents of
only those cells that contain a dash and leave the other cells untouched.

Replace won't work because it replaces a dash with an alpha blank cell and
the cell contents need to be clear or numeric to perform sumproduct()
calculations on them or else it results in a #VALUE! error. Yes, I know I
could replace "-" with 0, but I need the cell to be blank (no contents). I
assume a macro is required. Any ideas?
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 11,058
Default Macro to clear contents of certain cells

Sub ClearDash()
Dim s As String
s = "-"
For Each r In ActiveSheet.UsedRange
If InStr(r.Value, s) 0 Then
r.Clear
End If
Next
End Sub

--
Gary''s Student - gsnu201001


"MrAcquire" wrote:

I have a range of cells that contains mostly numeric values but some cells
are populated with an alpha dash (-). I would like to clear the contents of
only those cells that contain a dash and leave the other cells untouched.

Replace won't work because it replaces a dash with an alpha blank cell and
the cell contents need to be clear or numeric to perform sumproduct()
calculations on them or else it results in a #VALUE! error. Yes, I know I
could replace "-" with 0, but I need the cell to be blank (no contents). I
assume a macro is required. Any ideas?

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 62
Default Macro to clear contents of certain cells

Thanks. Your macro achieves the desired result except that I had to modify
it to ClearContents instead of just Clear so it wouldn't clear the formatting
(see below)

Sub ClearDash()
Dim s As String
s = "-"
For Each r In ActiveSheet.UsedRange
If InStr(r.Value, s) 0 Then
r.ClearContents
End If
Next
End Sub

But the macro results in a visual basic "Type mismatch" error when the macro
is finished. I just click OK and everything seems to have worked but macros
should not result in errors. Any idea what's causing this and how to fix it?


"Gary''s Student" wrote:

Sub ClearDash()
Dim s As String
s = "-"
For Each r In ActiveSheet.UsedRange
If InStr(r.Value, s) 0 Then
r.Clear
End If
Next
End Sub

--
Gary''s Student - gsnu201001


"MrAcquire" wrote:

I have a range of cells that contains mostly numeric values but some cells
are populated with an alpha dash (-). I would like to clear the contents of
only those cells that contain a dash and leave the other cells untouched.

Replace won't work because it replaces a dash with an alpha blank cell and
the cell contents need to be clear or numeric to perform sumproduct()
calculations on them or else it results in a #VALUE! error. Yes, I know I
could replace "-" with 0, but I need the cell to be blank (no contents). I
assume a macro is required. Any ideas?

  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 62
Default Macro to clear contents of certain cells

Thanks. I changed r.Clear to r.ClearContents and it worked as desired
(otherwise it clears the formatting, too). But the macro is resulting in a
visual basic Type Mismatch error when it's through. I click OK and the macro
appears to have worked but macros shouldn't result in error messages. Any
idea what's causing this error and how to fix it?

"Gary''s Student" wrote:

Sub ClearDash()
Dim s As String
s = "-"
For Each r In ActiveSheet.UsedRange
If InStr(r.Value, s) 0 Then
r.Clear
End If
Next
End Sub

--
Gary''s Student - gsnu201001


"MrAcquire" wrote:

I have a range of cells that contains mostly numeric values but some cells
are populated with an alpha dash (-). I would like to clear the contents of
only those cells that contain a dash and leave the other cells untouched.

Replace won't work because it replaces a dash with an alpha blank cell and
the cell contents need to be clear or numeric to perform sumproduct()
calculations on them or else it results in a #VALUE! error. Yes, I know I
could replace "-" with 0, but I need the cell to be blank (no contents). I
assume a macro is required. Any ideas?

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to clear contents and put an X bevchapman Excel Discussion (Misc queries) 3 March 17th 09 07:03 PM
Clear Contents Of Cells Where Value = 0 carl Excel Worksheet Functions 3 July 6th 07 06:02 PM
Macro to clear contents of unprotected cells AND drop down boxes JB2010 Excel Discussion (Misc queries) 3 March 30th 06 10:13 AM
HOW DO I PROTECT VALUES WHEN CREATING CLEAR CONTENTS MACRO? the holster Excel Discussion (Misc queries) 1 February 20th 06 06:33 PM
Clear Contents Macro SJC Excel Worksheet Functions 3 October 27th 05 07:26 PM


All times are GMT +1. The time now is 03:13 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"