Solved

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

Posted on 2012-03-21
3
536 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

911 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

20 Experts available now in Live!

Get 1:1 Help Now