PC Review


Reply
Thread Tools Rate Thread

Check for character in string

 
 
dksaluki
Guest
Posts: n/a
 
      11th Dec 2007
I have a range of cells with a name in them. What is the best way to
check to see if each cell has the "_" character or the "-" character
in the string? I would also like to remove it. Right now i have:

If Not Range("A4").Find("-") Is Nothing Then
fileName = VBA.Replace(Range("A4").Value, "-", "")
ElseIf Not Range("A4").Find("_") Is Nothing Then
fileName = VBA.Replace(Range("A4").Value, "_", "")
End If

I'm using the VBA.Replace method to replace the above characters with
a blank. (If the .Find method is not nothing, replace the characters)
It works fine, just didn't know if there was a faster, more efficient
way? ....like an array or something...

dck
 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      11th Dec 2007
You could use VBA's InStr() function to look for a string within a string.

But why bother? Why not just do the replace:

fileName = Replace(Range("A4").Value, "-", "")
fileName = Replace(Range("A4").Value, "_", "")

Is there a reason you used VBA.Replace instead of Replace? Why the
qualification?

dksaluki wrote:
>
> I have a range of cells with a name in them. What is the best way to
> check to see if each cell has the "_" character or the "-" character
> in the string? I would also like to remove it. Right now i have:
>
> If Not Range("A4").Find("-") Is Nothing Then
> fileName = VBA.Replace(Range("A4").Value, "-", "")
> ElseIf Not Range("A4").Find("_") Is Nothing Then
> fileName = VBA.Replace(Range("A4").Value, "_", "")
> End If
>
> I'm using the VBA.Replace method to replace the above characters with
> a blank. (If the .Find method is not nothing, replace the characters)
> It works fine, just didn't know if there was a faster, more efficient
> way? ....like an array or something...
>
> dck


--

Dave Peterson
 
Reply With Quote
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      11th Dec 2007
Given the If-Then-ElseIf structure the OP used in his message, shouldn't the
second statement use the fileName (as set by the first statement) as the
argument to its Replace function?

fileName = Replace(Range("A4").Value, "-", "")
fileName = Replace(fileName, "_", "")

or, I guess they could be combined into a single statement...

fileName = Replace(Replace(Range("A4").Value, "-", ""), "_", "")

Rick


"Dave Peterson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> You could use VBA's InStr() function to look for a string within a string.
>
> But why bother? Why not just do the replace:
>
> fileName = Replace(Range("A4").Value, "-", "")
> fileName = Replace(Range("A4").Value, "_", "")
>
> Is there a reason you used VBA.Replace instead of Replace? Why the
> qualification?
>
> dksaluki wrote:
>>
>> I have a range of cells with a name in them. What is the best way to
>> check to see if each cell has the "_" character or the "-" character
>> in the string? I would also like to remove it. Right now i have:
>>
>> If Not Range("A4").Find("-") Is Nothing Then
>> fileName = VBA.Replace(Range("A4").Value, "-", "")
>> ElseIf Not Range("A4").Find("_") Is Nothing Then
>> fileName = VBA.Replace(Range("A4").Value, "_", "")
>> End If
>>
>> I'm using the VBA.Replace method to replace the above characters with
>> a blank. (If the .Find method is not nothing, replace the characters)
>> It works fine, just didn't know if there was a faster, more efficient
>> way? ....like an array or something...
>>
>> dck

>
> --
>
> Dave Peterson


 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      11th Dec 2007
I agree.

Thanks for the addendum.

"Rick Rothstein (MVP - VB)" wrote:
>
> Given the If-Then-ElseIf structure the OP used in his message, shouldn't the
> second statement use the fileName (as set by the first statement) as the
> argument to its Replace function?
>
> fileName = Replace(Range("A4").Value, "-", "")
> fileName = Replace(fileName, "_", "")
>
> or, I guess they could be combined into a single statement...
>
> fileName = Replace(Replace(Range("A4").Value, "-", ""), "_", "")
>
> Rick
>
> "Dave Peterson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > You could use VBA's InStr() function to look for a string within a string.
> >
> > But why bother? Why not just do the replace:
> >
> > fileName = Replace(Range("A4").Value, "-", "")
> > fileName = Replace(Range("A4").Value, "_", "")
> >
> > Is there a reason you used VBA.Replace instead of Replace? Why the
> > qualification?
> >
> > dksaluki wrote:
> >>
> >> I have a range of cells with a name in them. What is the best way to
> >> check to see if each cell has the "_" character or the "-" character
> >> in the string? I would also like to remove it. Right now i have:
> >>
> >> If Not Range("A4").Find("-") Is Nothing Then
> >> fileName = VBA.Replace(Range("A4").Value, "-", "")
> >> ElseIf Not Range("A4").Find("_") Is Nothing Then
> >> fileName = VBA.Replace(Range("A4").Value, "_", "")
> >> End If
> >>
> >> I'm using the VBA.Replace method to replace the above characters with
> >> a blank. (If the .Find method is not nothing, replace the characters)
> >> It works fine, just didn't know if there was a faster, more efficient
> >> way? ....like an array or something...
> >>
> >> dck

> >
> > --
> >
> > Dave Peterson


--

Dave Peterson
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Check if character exists in string Lars Brownies Microsoft Access 2 17th Jan 2010 10:16 PM
check if a string contains a particular character kalle Microsoft Excel Programming 3 10th Jul 2009 06:06 AM
How to check if a string starts with a certain character garyusenet@myway.com Microsoft C# .NET 6 2nd May 2006 11:21 AM
Trying to check 3rd character in string as criteria Duane Hookom Microsoft Access Queries 4 28th Oct 2004 10:15 PM
Check if first character of string is in array Jonathan Microsoft Excel Programming 4 11th Oct 2003 12:07 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:47 PM.