Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Excel VBA - Make cell contents null if cell contains only spaces

Posted on 2012-03-21
3
Medium Priority
?
672 Views
Last Modified: 2012-03-22
How can I do the following w/ VBA:
1) For a given range, make contents of cell "null" if cell contains only spaces.
2) For a given range, replace multiple concatenated space characters with single space

Thanks, --Andres
0
Comment
Question by:AndresHernando
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 4

Assisted Solution

by:petr_hlucin
petr_hlucin earned 400 total points
ID: 37746608
1) Why don't you use TRIM function? You can e.g. create the modified data on another sheet and spread this function on the area as you desire (taking parameters from the original sheet).
2) If the spaces are in the beginning of the string or at its end TRIM(...) & " " probably solves the problem. Otherwise use RegexReplace(..., "[ ]*", " ") VBA function below:

Function RegexReplace(ReplaceIn, _
        ReplaceWhat As String, ReplaceWith As String, Optional IgnoreCase As Boolean = False)
    Dim RE As Object
    Set RE = CreateObject("vbscript.regexp")
    RE.IgnoreCase = IgnoreCase
    RE.Pattern = ReplaceWhat
    RE.Global = True
    RegexReplace = RE.Replace(ReplaceIn, ReplaceWith)
End Function

Open in new window

0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 1600 total points
ID: 37746908
1.
Dim rngCell as Range
For each rngCell in Range("A1:A100").Cells
   If len(trim(rngcell.value)) = 0 then rngCell.Clearcontents
next rngcell

Open in new window

2.
Dim rngCell as Range
For each rngCell in Range("A1:A100").Cells
   If instr(rngcell.value, " ") > 0 then rngcell.value = application.trim(rngCell.value)
next rngcell

Open in new window

0
 

Author Comment

by:AndresHernando
ID: 37752333
Rory, that's exactly what I needed.  Thanks!
Petr, thanks for your input.  I went with Rory's because it seems more straight forward and easier for me to understand.

Thanks again, --Andres
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question