Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default taking out all \/?*[] so input can be name of worksheet

Hi!
I'm reading in a CSV file. I want to use one of the fields as the
name of the WorkSheet. The file is from different clients and the
field is free text. I've come across names with ** or \. Do I have
to use the SUBSTITUTE function 6 times or is there a replaceText
function?
Thanks,
Mechi
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default taking out all \/?*[] so input can be name of worksheet

Look in VBA help for : Replace Function Make count -1 to replace all
possible eplacement.

" wrote:

Hi!
I'm reading in a CSV file. I want to use one of the fields as the
name of the WorkSheet. The file is from different clients and the
field is free text. I've come across names with ** or \. Do I have
to use the SUBSTITUTE function 6 times or is there a replaceText
function?
Thanks,
Mechi

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,533
Default taking out all \/?*[] so input can be name of worksheet

Hi Mechi

Look at Find / Replace on the Edit menu.

Regards,
Per

skrev i meddelelsen
...
Hi!
I'm reading in a CSV file. I want to use one of the fields as the
name of the WorkSheet. The file is from different clients and the
field is free text. I've come across names with ** or \. Do I have
to use the SUBSTITUTE function 6 times or is there a replaceText
function?
Thanks,
Mechi


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default taking out all \/?*[] so input can be name of worksheet

This is a function I use for this purpose:

Function MakeValidRangeName(ByVal strRangeName As String, _
Optional strAddIfLeadingNumericsOrDots As String
= "_", _
Optional strAddIfTrailingNumerics As String =
"_", _
Optional bRemoveLeadingNumericsAndDots As
Boolean, _
Optional bRemoveTrailingNumerics As Boolean, _
Optional strSpaceReplace As String = "_") As
String

If Len(strRangeName) = 0 Then
MakeValidRangeName = "No_Name_Provided"
Exit Function
End If

'1. not longer than 255 characters, but allow for added leading and/or
trailing "_"
'----------------------------------------------------------------------------------
strRangeName = Left$(strRangeName, 253)

'2. clear all these. Note that the characters \ and . are valid
'--------------------------------------------------------------
strRangeName = ClearCharsFromString(strRangeName,
"!£$%^&*()-+={}[]:;@'~#|<,?/")

'3. to avoid range names starting with numerics or dots (trailing dots are
fine)
'-------------------------------------------------------------------------------
If bRemoveLeadingNumericsAndDots Then
strRangeName = ClearCharsFromString(strRangeName, ".0123456789", False,
True)
Else
If InStr(1, ".0123456789", Left$(strRangeName, 1), vbBinaryCompare) 0
Then
strRangeName = strAddIfLeadingNumericsOrDots & strRangeName
End If
End If

'4. to avoid range names ending with numerics
'--------------------------------------------
If bRemoveTrailingNumerics Then
strRangeName = ClearCharsFromString(strRangeName, "0123456789", False, ,
True)
Else
If InStr(1, "0123456789", Right$(strRangeName, 1), vbBinaryCompare) 0
Then
strRangeName = strRangeName & strAddIfTrailingNumerics
End If
End If

'5. replace spaces with "_" or other specified character
'-------------------------------------------------------
strRangeName = Replace(strRangeName, " ", strSpaceReplace, 1, -1,
vbBinaryCompare)

MakeValidRangeName = strRangeName

End Function

Function ClearCharsFromString(strString As String, _
strChars As String, _
Optional bAll As Boolean = True, _
Optional bLeading As Boolean, _
Optional bTrailing As Boolean) As String

Dim i As Long
Dim strChar As String

ClearCharsFromString = strString

If bAll Then
For i = 1 To Len(strChars)
strChar = Mid$(strChars, i, 1)
If InStr(1, strString, strChar) 0 Then
ClearCharsFromString = Replace(ClearCharsFromString, _
strChar, _
vbNullString, _
1, -1, vbBinaryCompare)
End If
Next i
Else
If bLeading Then
Do While InStr(1, strChars, Left$(ClearCharsFromString, 1), _
vbBinaryCompare) 0
ClearCharsFromString = Right$(ClearCharsFromString, _
Len(ClearCharsFromString) - 1)
Loop
End If
If bTrailing Then
Do While InStr(1, strChars, Right$(ClearCharsFromString, 1), _
vbBinaryCompare) 0
ClearCharsFromString = Left$(ClearCharsFromString, _
Len(ClearCharsFromString) - 1)
Loop
End If
End If

End Function


RBS


wrote in message
...
Hi!
I'm reading in a CSV file. I want to use one of the fields as the
name of the WorkSheet. The file is from different clients and the
field is free text. I've come across names with ** or \. Do I have
to use the SUBSTITUTE function 6 times or is there a replaceText
function?
Thanks,
Mechi


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default taking out all \/?*[] so input can be name of worksheet

On Jun 4, 10:55*am, "Per Jessen" wrote:
Hi Mechi

Look at Find / Replace on the Edit menu.

Regards,
Per


I'm reading data from a csv file - I have to replace in strings using
VBScript - before I set the Worksheet name to the string.


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default taking out all \/?*[] so input can be name of worksheet

On Jun 4, 10:46*am, Joel wrote:
Look in VBA help for : Replace Function *Make count -1 to replace all
possible eplacement.


I'll try that
Thanks!
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default taking out all \/?*[] so input can be name of worksheet

On Jun 4, 2:47*pm, "RB Smissaert"
wrote:
This is a function I use for this purpose:

Function MakeValidRangeName(ByVal strRangeName As String, _
* * * * * * * * * * * * * * Optional strAddIfLeadingNumericsOrDots As String
= "_", _
* * * * * * * * * * * * * * Optional strAddIfTrailingNumerics As String =
"_", _
* * * * * * * * * * * * * * Optional bRemoveLeadingNumericsAndDots As
Boolean, _
* * * * * * * * * * * * * * Optional bRemoveTrailingNumerics As Boolean, _
* * * * * * * * * * * * * * Optional strSpaceReplace As String = "_") As
String

* If Len(strRangeName) = 0 Then
* * MakeValidRangeName = "No_Name_Provided"
* * Exit Function
* End If

* '1. not longer than 255 characters, but allow for added leading and/or
trailing "_"
* '--------------------------------------------------------------------------*--------
* strRangeName = Left$(strRangeName, 253)

* '2. clear all these. Note that the characters \ and . are valid
* '--------------------------------------------------------------
* strRangeName = ClearCharsFromString(strRangeName,
"!£$%^&*()-+={}[]:;@'~#|<,?/")

* '3. to avoid range names starting with numerics or dots (trailing dots are
fine)
* '--------------------------------------------------------------------------*-----
* If bRemoveLeadingNumericsAndDots Then
* * strRangeName = ClearCharsFromString(strRangeName, ".0123456789", False,
True)
* Else
* * If InStr(1, ".0123456789", Left$(strRangeName, 1), vbBinaryCompare) 0
Then
* * * strRangeName = strAddIfLeadingNumericsOrDots & strRangeName
* * End If
* End If

* '4. to avoid range names ending with numerics
* '--------------------------------------------
* If bRemoveTrailingNumerics Then
* * strRangeName = ClearCharsFromString(strRangeName, "0123456789", False, ,
True)
* Else
* * If InStr(1, "0123456789", Right$(strRangeName, 1), vbBinaryCompare) 0
Then
* * * strRangeName = strRangeName & strAddIfTrailingNumerics
* * End If
* End If

* '5. replace spaces with "_" or other specified character
* '-------------------------------------------------------
* strRangeName = Replace(strRangeName, " ", strSpaceReplace, 1, -1,
vbBinaryCompare)

* MakeValidRangeName = strRangeName

End Function

Function ClearCharsFromString(strString As String, _
* * * * * * * * * * * * * * * strChars As String, _
* * * * * * * * * * * * * * * Optional bAll As Boolean = True, _
* * * * * * * * * * * * * * * Optional bLeading As Boolean, _
* * * * * * * * * * * * * * * Optional bTrailing As Boolean) As String

* Dim i As Long
* Dim strChar As String

* ClearCharsFromString = strString

* If bAll Then
* * For i = 1 To Len(strChars)
* * * strChar = Mid$(strChars, i, 1)
* * * If InStr(1, strString, strChar) 0 Then
* * * * ClearCharsFromString = Replace(ClearCharsFromString, _
* * * * * * * * * * * * * * * * * * * *strChar, _
* * * * * * * * * * * * * * * * * * * *vbNullString, _
* * * * * * * * * * * * * * * * * * * *1, -1, vbBinaryCompare)
* * * End If
* * Next i
* Else
* * If bLeading Then
* * * Do While InStr(1, strChars, Left$(ClearCharsFromString, 1), _
* * * * * * * * * * *vbBinaryCompare) 0
* * * * ClearCharsFromString = Right$(ClearCharsFromString, _
* * * * * * * * * * * * * * * * * * * Len(ClearCharsFromString) - 1)
* * * Loop
* * End If
* * If bTrailing Then
* * * Do While InStr(1, strChars, Right$(ClearCharsFromString, 1), _
* * * * * * * * * * *vbBinaryCompare) 0
* * * * ClearCharsFromString = Left$(ClearCharsFromString, _
* * * * * * * * * * * * * * * * * * *Len(ClearCharsFromString) - 1)
* * * Loop
* * End If
* End If

End Function

RBS

wrote in message

...



Hi!
I'm reading in a CSV file. *I want to use one of the fields as the
name of the WorkSheet. *The file is from different clients and the
field is free text. *I've come across names with ** or \. *Do I have
to use the SUBSTITUTE function 6 times or is there a replaceText
function?
Thanks,
Mechi- Hide quoted text -


- Show quoted text -


Since the program takes over an hour, I don't want to insert more
function calls - your code, though is very helpful and I'll save it
for use at other times - Thanks!

I inserted 8 simple lines:
clientName = Trim(clientName)
clientName = Replace(clientName, "*", "#")
clientName = Replace(clientName, "\", "|")
clientName = Replace(clientName, "/", "|")
clientName = Replace(clientName, "?", "!")
clientName = Replace(clientName, "[", "{")
clientName = Replace(clientName, "]", "}")
clientName = Left(clientName, 27) ' leave room for #


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
taking certian data from worksheet one to worksheet 2 [email protected] Excel Worksheet Functions 1 September 12th 06 09:01 AM
Taking data from another worksheet and converting Annerobbo Excel Worksheet Functions 2 November 9th 05 01:41 PM
Taking range as input from the User jn_mohit Excel Programming 1 July 8th 05 04:26 PM
Taking content from worksheet,loc it in another, and if exists. copy it to a third... grntdav Excel Programming 0 July 7th 04 02:25 PM
How to sum value from master worksheet taking value from other close worksheet nuha1578 Excel Programming 1 November 3rd 03 02:58 PM


All times are GMT +1. The time now is 11:24 AM.

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

About Us

"It's about Microsoft Excel"