Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 48
Default Check for character in string

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
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default Check for character in string

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
  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2,202
Default Check for character in string

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" wrote in message
...
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


  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default Check for character in string

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" wrote in message
...
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
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
find a character in a string kevcar40 Excel Discussion (Misc queries) 4 June 5th 07 12:10 PM
Excel-Match 1st text character in a string to a known character? bushlite Excel Worksheet Functions 2 January 15th 07 06:36 PM
Problem with VB string character limit. AJL Excel Worksheet Functions 0 November 3rd 06 07:40 PM
check if the text string start with a specific character September21 New Users to Excel 5 September 22nd 05 03:07 PM
Splitting Character String mcertini Excel Worksheet Functions 2 September 12th 05 09:41 AM


All times are GMT +1. The time now is 10:38 PM.

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"