Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 56
Default Query - Formula (Test for string)

Hi,

Can someone help with the following @ all. I've created the following
VBA which works as expected the first time, however I only want it to
check for the value once, whereas this will add in the prefix more
than once when I run it?

e.g. this is meant to check B4, if this contains CD- don't do anything
if it doesn't add it in.

Any suggestions?

Cheers, Al.

Sub Prefix()
Const Prefix = "CD-"

If Left("B4", 3) = "CD-" Then
Range("B4").Select
ActiveCell.Value = Prefix & ActiveCell.Value
Else
End If
End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,118
Default Query - Formula (Test for string)

Sorry, but I'm confused...

This is what your code is doing:

1) Defines the constant "Prefix" to equal: "CD-"
2) Checks if the left 3 chars of the string "B4" = "CD-"
(which it never will, of course. Did you want it to check cell B4?)
2a) But, if it does....then select cell B4
2b) Next, using the active cell (which can only be B4)...
Prepend the Prefix to its value.
3) Otherwise, do nothing

What did you intend the code to do?

--------------------------

Regards,

Ron (XL2003, Win XP)
Microsoft MVP (Excel)



wrote in message
ups.com...
Hi,

Can someone help with the following @ all. I've created the following
VBA which works as expected the first time, however I only want it to
check for the value once, whereas this will add in the prefix more
than once when I run it?

e.g. this is meant to check B4, if this contains CD- don't do anything
if it doesn't add it in.

Any suggestions?

Cheers, Al.

Sub Prefix()
Const Prefix = "CD-"

If Left("B4", 3) = "CD-" Then
Range("B4").Select
ActiveCell.Value = Prefix & ActiveCell.Value
Else
End If
End Sub




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 226
Default Query - Formula (Test for string)

Hi there

i think the problem is that you are not checking the value held in the
cell, i have modified the code below and i seems to work fine. i have
dim'ed a range called MyCell this allows you to check the value and
change the value of the cell without having to select it which is
neater, i have also changed ="CD-" in the If statement to look to see
if it holds something other than "CD-" by using < instead of = which
means i can do without using an else statement in the if

Sub Prefix()

Dim MyCell As Range
Const Prefix = "CD-"

Set MyCell = [B4]

If Left(MyCell.Value, 3) < "CD-" Then
MyCell.Value = Prefix & MyCell.Value
End If

End Sub


hope this helps

Steve

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 56
Default Query - Formula (Test for string)

Hi Steve,

This works brilliantly.

Thanks for your help on this.

Cheers, Al.

On 8 Nov, 13:07, Incidental wrote:
Hi there

i think the problem is that you are not checking the value held in the
cell, i have modified the code below and i seems to work fine. i have
dim'ed a range called MyCell this allows you to check the value and
change the value of the cell without having to select it which is
neater, i have also changed ="CD-" in the If statement to look to see
if it holds something other than "CD-" by using < instead of = which
means i can do without using an else statement in the if

Sub Prefix()

Dim MyCell As Range
Const Prefix = "CD-"

Set MyCell = [B4]

If Left(MyCell.Value, 3) < "CD-" Then
MyCell.Value = Prefix & MyCell.Value
End If

End Sub

hope this helps

Steve



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 56
Default Query - Formula (Test for string)

Hi Ron,

Apologies, I noticed I'd done an error on this and then tried to add
an updated text but it doesn't appear to have posted.

What in essence I'm trying to achive is:
1) If B4 contains any data (which is prefixed "CD-") don't do
anything.
2) If it contains data, e.g. 123 but not prefixed with CD- then add
it in.
3) If it contains no data, don't do anything.

HTH, Thanks Al.

On 8 Nov, 12:49, "Ron Coderre"
wrote:
Sorry, but I'm confused...

This is what your code is doing:

1) Defines the constant "Prefix" to equal: "CD-"
2) Checks if the left 3 chars of the string "B4" = "CD-"
(which it never will, of course. Did you want it to check cell B4?)
2a) But, if it does....then select cell B4
2b) Next, using the active cell (which can only be B4)...
Prepend the Prefix to its value.
3) Otherwise, do nothing

What did you intend the code to do?

--------------------------

Regards,

Ron (XL2003, Win XP)
Microsoft MVP (Excel)

wrote in message

ups.com...



Hi,


Can someone help with the following @ all. I've created the following
VBA which works as expected the first time, however I only want it to
check for the value once, whereas this will add in the prefix more
than once when I run it?


e.g. this is meant to check B4, if this contains CD- don't do anything
if it doesn't add it in.


Any suggestions?


Cheers, Al.


Sub Prefix()
Const Prefix = "CD-"


If Left("B4", 3) = "CD-" Then
Range("B4").Select
ActiveCell.Value = Prefix & ActiveCell.Value
Else
End If
End Sub- Hide quoted text -


- Show quoted text -





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 274
Default Query - Formula (Test for string)

Hi mralmac,

Try this

If Left(Range("B4").Value, 3) = "CD-" Then

Regards

Michael Beckinsale

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Test String LucB Excel Discussion (Misc queries) 2 November 29th 06 02:49 AM
test if a character in a string is alpha Matilda Excel Programming 7 October 16th 06 02:36 PM
How to test text string to know if it is a date itarnak[_4_] Excel Programming 2 October 7th 05 02:03 PM
Test for exact match in string Jeremy Gollehon[_2_] Excel Programming 3 August 24th 04 12:58 AM
Test a String to see if it begins with a Number Randy[_10_] Excel Programming 3 October 8th 03 12:37 PM


All times are GMT +1. The time now is 06:52 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"