Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Replace Space charcter in a string variable

For x = 1 To Len(strVar)
If Mid(strVar, x, 1) = " " Then
Mid(strVar, x, 1) = "_"
End If
Next x

I haven't tested this, but it works in theory. Let me know if you hav
any problems - Piku

--
Message posted from http://www.ExcelForum.com

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Replace Space charcter in a string variable

Okay... I just tested it and it does work fine! :-) - Piku

--
Message posted from http://www.ExcelForum.com

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,885
Default Replace Space charcter in a string variable

Hi Pikus
why not use Replace :-)

--
Regards
Frank Kabel
Frankfurt, Germany


For x = 1 To Len(strVar)
If Mid(strVar, x, 1) = " " Then
Mid(strVar, x, 1) = "_"
End If
Next x

I haven't tested this, but it works in theory. Let me know if you
have any problems - Pikus


---
Message posted from http://www.ExcelForum.com/

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Replace Space charcter in a string variable

He specifically said he was working with a string variable, not info i
a cell. Now the info may actually be in a cell, but without knowing
didn't want to assume. That being said, Replace only works on Rang
objects right? So it wouldn't work with a string... Please tell me i
I'm wrong about that. - Piku

--
Message posted from http://www.ExcelForum.com

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Replace Space charcter in a string variable

In VBA6 (Excel 2000 and later) there is a Replace function that
replaces text within character strings. E.g.,

Dim S As String
S = "abcdefg"
S = Replace(S, "abc", "xyz")
Debug.Print S


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"pikus " wrote in message
...
He specifically said he was working with a string variable, not

info in
a cell. Now the info may actually be in a cell, but without

knowing I
didn't want to assume. That being said, Replace only works on

Range
objects right? So it wouldn't work with a string... Please

tell me if
I'm wrong about that. - Pikus


---
Message posted from http://www.ExcelForum.com/





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Replace Space charcter in a string variable

Thanks Chip! Is there anything you don't know? - Piku

--
Message posted from http://www.ExcelForum.com

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Replace Space charcter in a string variable

Here is the code I used
-----------------------------------------

On Error Resume Nex

FileSaveName = Application.GetSaveAsFilename("K:\Excel_Keyword\",
fileFilter:="Text Files (*.txt), *.txt"

space_position = Application.WorksheetFunction.Find(" ", FileSaveName

If Err = 0 Then MsgBox "Spaces have been found in your file name," & vbCrLf &
"they will be replaced with underscores

Do While Err =
FileSaveName = Application.WorksheetFunction.Replace
(Arg1:=FileSaveName, Arg2:=space_position,
Arg3:=1, Arg4:="_"
space_position = Application.WorksheetFunction.Find(" ", FileSaveName
Loo

Err.Clea

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Replace Space charcter in a string variable

Why so verbose and slow?

FileSaveName = Application.GetSaveAsFilename("K:\Excel_Keyword\", _
fileFilter:="Text Files (*.txt), *.txt")

FileSaveName = Application.Substitute(FileSaveName," ","_")

There is no need to test - because if there are no spaces, nothing will
happen, and if there are, they will be replaced.

You can also use the vba replace function if only working in xl2000 or
higher (or anyone who will use your code).

if you want a warning, so you need to check then


FileSaveName = Application.GetSaveAsFilename("K:\Excel_Keyword\", _
fileFilter:="Text Files (*.txt), *.txt")
if instr(1,FileSaveName," ",vbTextCompare) < 0 then
msgbox "warning . . . blah blah"
FileSaveName = Application.Substitute(FileSaveName," ","_")
end if


--
Regards,
Tom Ogilvy


"Simon Shaw" wrote in message
...
Here is the code I used:
------------------------------------------

On Error Resume Next

FileSaveName = Application.GetSaveAsFilename("K:\Excel_Keyword\", _
fileFilter:="Text Files (*.txt), *.txt")

space_position = Application.WorksheetFunction.Find(" ", FileSaveName)

If Err = 0 Then MsgBox "Spaces have been found in your file name," &

vbCrLf & _
"they will be replaced with underscores"

Do While Err = 0
FileSaveName = Application.WorksheetFunction.Replace _
(Arg1:=FileSaveName, Arg2:=space_position, _
Arg3:=1, Arg4:="_")
space_position = Application.WorksheetFunction.Find(" ",

FileSaveName)
Loop

Err.Clear



  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Replace Space charcter in a string variable

How would I know?

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"pikus " wrote in message
...
Thanks Chip! Is there anything you don't know? - Pikus


---
Message posted from http://www.ExcelForum.com/



  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,885
Default Replace Space charcter in a string variable

Hi Pikus
Replace works also on string variables (see VBA help)

--
Regards
Frank Kabel
Frankfurt, Germany


He specifically said he was working with a string variable, not info
in a cell. Now the info may actually be in a cell, but without
knowing I didn't want to assume. That being said, Replace only works
on Range objects right? So it wouldn't work with a string... Please
tell me if I'm wrong about that. - Pikus


---
Message posted from http://www.ExcelForum.com/




  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Replace Space charcter in a string variable

Just excalimimg cuz I feel like SUCH an amature next to some of th
people that post here. I read a book about VBA like six months ago an
I really fell in love with programing and I'm soaking up information a
what feels like light speed, but the experience you guys bring i
awesome... Anyways, thanks again for the pointers. Talk to y'al
later. - Piku

--
Message posted from http://www.ExcelForum.com

  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Replace Space charcter in a string variable

Only because I didn't know about using substitute

thanks
  #13   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Replace Space charcter in a string variable

Does the vba replace function exist in XL 97

I thought i used it there B4 but now having trouble.
Do I have to add references or something to the workbook to get th
function to work?

Thanks AL

--
Message posted from http://www.ExcelForum.com

  #14   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Replace Space charcter in a string variable

The replace function was added in VBA6. xl2000 and later use VBA6. XL97
uses VBA5 which does not have a replace function.

It can not be added by changing references. However, you could write your
own.
--
Regards,
Tom Ogilvy

"ajlinnane " wrote in message
...
Does the vba replace function exist in XL 97

I thought i used it there B4 but now having trouble.
Do I have to add references or something to the workbook to get the
function to work?

Thanks AL.


---
Message posted from http://www.ExcelForum.com/



  #15   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Replace Space charcter in a string variable

Just to add
in all versions you can use the substitute worksheet function

sStr = Application.substitute(sSt," ","")

You could possibly use conditional compilation since this would be slower.

--
Regards,
Tom Ogilvy

"Tom Ogilvy" wrote in message
...
The replace function was added in VBA6. xl2000 and later use VBA6. XL97
uses VBA5 which does not have a replace function.

It can not be added by changing references. However, you could write your
own.
--
Regards,
Tom Ogilvy

"ajlinnane " wrote in message
...
Does the vba replace function exist in XL 97

I thought i used it there B4 but now having trouble.
Do I have to add references or something to the workbook to get the
function to work?

Thanks AL.


---
Message posted from http://www.ExcelForum.com/





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
Doing a replace with a wildcard charcter John Excel Discussion (Misc queries) 2 October 2nd 09 02:49 PM
Finding a charcter in a text string (take2) Bulent About this forum 0 October 30th 07 09:36 AM
Finding a charcter in a text string caldog Excel Worksheet Functions 3 October 7th 07 04:37 AM
Replace Space charcter in a string variable Frank Kabel Excel Programming 0 May 18th 04 05:45 PM
Macro to delete last charcter in a text string Brian Excel Programming 2 July 24th 03 03:43 AM


All times are GMT +1. The time now is 05:26 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"