Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
Max Max is offline
external usenet poster
 
Posts: 9,221
Default Sub to clear cells with zeros or null strings within a selected ra

Hi guys,

I'm looking for a sub to clear cells with zeros or null strings within a
selected range. I'd select the range, then run the sub to clear all such
cells within the range. Thanks for insights.
--
Max
Singapore
http://savefile.com/projects/236895
xdemechanik
---
  #2   Report Post  
Posted to microsoft.public.excel.programming
Jay Jay is offline
external usenet poster
 
Posts: 671
Default Sub to clear cells with zeros or null strings within a selected ra

Hi Max -

Although VBA variables can evaluate to "Null", worksheet cells cannot. A
cell is considered empty if it evaluates to a zero-length string (zls),
designated as "". So the procedure below sets cells in the selection to ""
if they containing a zero or spaces only.

Sub MaxSingapore()
Set rng = Selection
For Each itm In rng
If Trim(itm.Value) = "" Or itm.Value = 0 Then itm.Value = ""
Next 'itm
End Sub
--
Jay


"Max" wrote:

Hi guys,

I'm looking for a sub to clear cells with zeros or null strings within a
selected range. I'd select the range, then run the sub to clear all such
cells within the range. Thanks for insights.
--
Max
Singapore
http://savefile.com/projects/236895
xdemechanik
---

  #3   Report Post  
Posted to microsoft.public.excel.programming
Max Max is offline
external usenet poster
 
Posts: 9,221
Default Sub to clear cells with zeros or null strings within a selecte

Thanks, Jay. But I want the sub to clear such cells (ie like CTRL-selecting
these cells at one go, then pressing the Delete key), not set these cells to
"". How would your sub then look like ? The selected range may contain cells
with "" and cells with zeros.
--
Max
Singapore
http://savefile.com/projects/236895
xdemechanik
---
"Jay" wrote:
Hi Max -

Although VBA variables can evaluate to "Null", worksheet cells cannot. A
cell is considered empty if it evaluates to a zero-length string (zls),
designated as "". So the procedure below sets cells in the selection to ""
if they containing a zero or spaces only.

Sub MaxSingapore()
Set rng = Selection
For Each itm In rng
If Trim(itm.Value) = "" Or itm.Value = 0 Then itm.Value = ""
Next 'itm
End Sub
--
Jay

  #4   Report Post  
Posted to microsoft.public.excel.programming
Jay Jay is offline
external usenet poster
 
Posts: 671
Default Sub to clear cells with zeros or null strings within a selecte

Sub MaxSingapore()
Set rng = Selection
For Each ar In Selection.Areas
For Each itm In ar
If Trim(itm.Value) = "" Or itm.Value = 0 Then itm.Clear
Next 'itm
Next 'ar
End Sub
--
Jay


"Max" wrote:

Thanks, Jay. But I want the sub to clear such cells (ie like CTRL-selecting
these cells at one go, then pressing the Delete key), not set these cells to
"". How would your sub then look like ? The selected range may contain cells
with "" and cells with zeros.
--
Max
Singapore
http://savefile.com/projects/236895
xdemechanik
---
"Jay" wrote:
Hi Max -

Although VBA variables can evaluate to "Null", worksheet cells cannot. A
cell is considered empty if it evaluates to a zero-length string (zls),
designated as "". So the procedure below sets cells in the selection to ""
if they containing a zero or spaces only.

Sub MaxSingapore()
Set rng = Selection
For Each itm In rng
If Trim(itm.Value) = "" Or itm.Value = 0 Then itm.Value = ""
Next 'itm
End Sub
--
Jay

  #5   Report Post  
Posted to microsoft.public.excel.programming
Max Max is offline
external usenet poster
 
Posts: 9,221
Default Sub to clear cells with zeros or null strings within a selecte

Runs great, Jay. Many thanks.

I changed: itm.Clear
to itm.ClearContents
to suit my purpose
--
Max
Singapore
http://savefile.com/projects/236895
xdemechanik
---
"Jay" wrote:
Sub MaxSingapore()
Set rng = Selection
For Each ar In Selection.Areas
For Each itm In ar
If Trim(itm.Value) = "" Or itm.Value = 0 Then itm.Clear
Next 'itm
Next 'ar
End Sub
--
Jay



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Sub to clear cells with zeros or null strings within a selected ra

You want to clean up the stuff left over from changing ="" to values (as well as
0's)?

Record a macro when you do 3 edit|Replaces
select your range
first one:
edit|Replace
what: 0
with: (leave blank)
(remember to match entire cell!)

Second:
edit replace
what: (leave blank)
with: $$$$$
replace all

Last one
edit|replace
what: $$$$$
with: (leave blank)

Use a nice unique string if $$$$$ is used somewhere in that range (don't use
apostrophes!).


Max wrote:

Hi guys,

I'm looking for a sub to clear cells with zeros or null strings within a
selected range. I'd select the range, then run the sub to clear all such
cells within the range. Thanks for insights.
--
Max
Singapore
http://savefile.com/projects/236895
xdemechanik
---


--

Dave Peterson
  #7   Report Post  
Posted to microsoft.public.excel.programming
Max Max is offline
external usenet poster
 
Posts: 9,221
Default Sub to clear cells with zeros or null strings within a selected ra

Thanks for the response, Dave. Yes, that does it too, but I was also looking
for a generic way which would apply to any selection(s) irrespective. The
macro recorder doesn't give that.
--
Max
Singapore
http://savefile.com/projects/236895
xdemechanik
---
"Dave Peterson" wrote in message
...
You want to clean up the stuff left over from changing ="" to values (as
well as
0's)?

Record a macro when you do 3 edit|Replaces
select your range
first one:
edit|Replace
what: 0
with: (leave blank)
(remember to match entire cell!)

Second:
edit replace
what: (leave blank)
with: $$$$$
replace all

Last one
edit|replace
what: $$$$$
with: (leave blank)

Use a nice unique string if $$$$$ is used somewhere in that range (don't
use
apostrophes!).



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Sub to clear cells with zeros or null strings within a selected ra

You can tweak your recorded macro to use the Selection instead of any specific
range:

Option Explicit
Sub testme()
Dim myRng As Range
Set myRng = Selection
With myRng
.Replace What:="0", Replacement:="", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False
.Replace What:="", Replacement:="$$$$$", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False
.Replace What:="$$$$$", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False
End With
End Sub





Max wrote:

Thanks for the response, Dave. Yes, that does it too, but I was also looking
for a generic way which would apply to any selection(s) irrespective. The
macro recorder doesn't give that.
--
Max
Singapore
http://savefile.com/projects/236895
xdemechanik
---
"Dave Peterson" wrote in message
...
You want to clean up the stuff left over from changing ="" to values (as
well as
0's)?

Record a macro when you do 3 edit|Replaces
select your range
first one:
edit|Replace
what: 0
with: (leave blank)
(remember to match entire cell!)

Second:
edit replace
what: (leave blank)
with: $$$$$
replace all

Last one
edit|replace
what: $$$$$
with: (leave blank)

Use a nice unique string if $$$$$ is used somewhere in that range (don't
use
apostrophes!).


--

Dave Peterson
  #9   Report Post  
Posted to microsoft.public.excel.programming
Max Max is offline
external usenet poster
 
Posts: 9,221
Default Sub to clear cells with zeros or null strings within a selected ra

Thanks for the tweak, Dave. Runs good!
--
Max
Singapore
http://savefile.com/projects/236895
xdemechanik
---
"Dave Peterson" wrote in message
...
You can tweak your recorded macro to use the Selection instead of any
specific
range:

Option Explicit
Sub testme()
Dim myRng As Range
Set myRng = Selection
With myRng
.Replace What:="0", Replacement:="", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False
.Replace What:="", Replacement:="$$$$$", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False
.Replace What:="$$$$$", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False
End With
End Sub



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
Empty strings appear as zeros Paul Martin[_2_] Charts and Charting in Excel 5 May 9th 08 12:37 AM
clear data from selected cells Steve Excel Programming 3 January 23rd 06 06:11 PM
Automatically clear values from a range of selected cells John Davies Excel Discussion (Misc queries) 1 June 28th 05 04:42 PM
How do I clear data in selected cells automatically Jim Train Excel Discussion (Misc queries) 1 May 25th 05 06:45 PM
Clear Empty Strings Tim Tabor Excel Programming 3 October 6th 03 02:12 AM


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

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

About Us

"It's about Microsoft Excel"