Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 3rd, 2005, 04:22 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default Compensate for a space or multiple spaces

The code i have below works fine if the cell is empty (has had nothing entered into it and has not been editted):

Code:
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

www.crmpicco.co.uk
www.crmpicco.co.uk.tt
www.milklemonadechocolate.uk.tt
www.griswolds.uk.tt
www.piccosmini.co.uk.tt
www.morton.uk.tt
__________________
_______________________
Ayrshire Minis - a Mini E-Community
http://www.ayrshireminis.com
http://www.crmpicco.co.uk
 
Old May 3rd, 2005, 04:58 AM
Authorized User
 
Join Date: Jun 2003
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Use the Trim function to remove any excess white space to the left and right of the string.

I also use the Len function, rather than compare with a literal string (your zero-length string, that is), although either way try to use the built-in constant vbNullString rather than ""

so, all of the following should work:

Code:
If Len(Trim$(Range(sRange).Text)) <> 0 Then
   Range(sRange).Interior.ColorIndex = iColour
End If

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

If Trim$(Range(sRange).Text) <> vbNullString Then
   Range(sRange).Interior.ColorIndex = iColour
End If

 
Old May 3rd, 2005, 05:15 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

Thanks that one worked a treat:

Code:
If Trim$(Range(sRange).Text) <> "" Then
   Range(sRange).Interior.ColorIndex = iColour
End If
www.crmpicco.co.uk
www.crmpicco.co.uk.tt
www.milklemonadechocolate.uk.tt
www.griswolds.uk.tt
www.piccosmini.co.uk.tt
www.morton.uk.tt
 
Old May 3rd, 2005, 05:22 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

why is the $ needed?

www.crmpicco.co.uk
www.crmpicco.co.uk.tt
www.milklemonadechocolate.uk.tt
www.griswolds.uk.tt
www.piccosmini.co.uk.tt
www.morton.uk.tt
 
Old May 3rd, 2005, 05:39 AM
Authorized User
 
Join Date: Jun 2003
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It's not needed: "Trim" on its own will do just fine.

just remembered that Trim$ is technically more optimal than Trim: Trim on its own will accept a variant as a parameter, whereas Trim$ only accepts a string

in fact, to make your code run more optimally you probably need to code something like

Code:
If LenB(Trim$(Range(sRange).Text)) <> 0 Then
   Range(sRange).Interior.ColorIndex = iColour
End If
That's because LenB is more optimal than Len.

of course i doubt this'll make much of an impact on your application, unless you're doing several thousand iterations, but who knows..
 
Old May 3rd, 2005, 01:49 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Many of the string functions have a version with the $ and one without. The one without receives a Variant (and therefore 1) can handle Nulls, and 2) takes time to convert the string you send it to Variant(String)).

I believe there is also a LenB$().

What are all those URLs?





Similar Threads
Thread Thread Starter Forum Replies Last Post
Diference between these two name spaces pradeepn XSLT 1 June 25th, 2007 02:31 AM
disappearing spaces gezi XSLT 0 March 26th, 2006 02:23 AM
Removing spaces Dave Brown Beginning PHP 2 October 28th, 2005 12:02 PM
adding multiple spaces otter606 Javascript 2 March 3rd, 2005 11:00 AM
Frame Spaces EvilGuyWhoEatsBrains HTML Code Clinic 2 January 5th, 2005 12:24 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.