Solved

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

Posted on 2012-03-21
3
522 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
Comment Utility
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
Comment Utility
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
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Drop Down List with Unique/Distinct Values (enhancing the Combo-Box with a few steps and a little code) David miller (dlmille) Intro Have you ever created a data validation list from a database field or spreadsheet column (e.g., Zip Codes or Co…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
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 use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now