?
Solved

how to detect Space inside Excel cell VBA Excel

Posted on 2005-05-03
9
Medium Priority
?
557 Views
Last Modified: 2010-08-05
The code i have below works fine if the cell is empty (has had nothing entered into it and has not been editted):



If Range(sRange).Text <> "" Then
   Range(sRange).Interior.ColorIndex = iColour
End If



But if there is a 'SPACE' (i.e. someone has pressed the spacebar inside the cell) or multiple spaces inside the cell then it colours it as it sees it as NOT EMPTY. How can i get around this? What code can i add into my if statement that i have posted above?

Thanks.

Picco
0
Comment
Question by:crmpicco
  • 4
  • 3
  • 2
9 Comments
 
LVL 15

Expert Comment

by:SnowFlake
ID: 13916154
Hi crmpicco,

If length(trim(Range(sRange).Text))<>0 Then
    Range(sRange).Interior.ColorIndex = iColour
End If



SnowFlake
0
 
LVL 19

Accepted Solution

by:
DreamMaster earned 200 total points
ID: 13917841
If Trim(Range(sRange).Text) <> "" Then
   Range(sRange).Interior.ColorIndex = iColour
End If

This should work as well...

Regards,
Max.
0
 
LVL 2

Expert Comment

by:tonym001
ID: 13948366
You could use the replace function
strRange = Replace(Range(sRange).Text, " ", "")
if strRange = "" then
    Range(sRange).Interior.ColorIndex = iColour
End If
0
Technology Partners: 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!

 
LVL 19

Expert Comment

by:DreamMaster
ID: 13948770
Uhh...tony...I might be mistaking...but don't you mean:

if strRange <> "" then
   Range(sRange).Interior.ColorIndex = iColour
End if

Regards,
Max.
0
 
LVL 2

Expert Comment

by:tonym001
ID: 13948834
yep sorry but you get the idea
0
 
LVL 19

Expert Comment

by:DreamMaster
ID: 13950061
Sure I do...just don't get why you'd replace spaces when you can do it with trim...which is meant for trimming spaces...

Regards,
Max.
0
 
LVL 15

Expert Comment

by:SnowFlake
ID: 13952078
crmpicc,
do you still need more assistance with this ?
0
 
LVL 2

Expert Comment

by:tonym001
ID: 13952127
max agreed - but users are users and i trust them about as far as I can kick them.
0
 
LVL 2

Expert Comment

by:tonym001
ID: 13952130
this question is over n I think dreammaster has the points - and I am a bit pissed so leave me alone
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month17 days, 4 hours left to enroll

862 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