Solved

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

Posted on 2012-03-21
3
554 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
3 Comments
 
LVL 4

Assisted Solution

by:petr_hlucin
petr_hlucin earned 100 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 400 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

773 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