?
Solved

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

Posted on 2012-03-21
3
Medium Priority
?
642 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

765 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