Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 16th, 2005, 03:13 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 168
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Danny,
Try this one
Function ReplaceChars(Word As String, Oldstring As String, NewString As String) As String
Dim i As Integer
Dim OLength As Integer
Dim NLength As Integer
OLength = Len(Oldstring)
NLength = Len(NewString)

For i = 1 To Len(Word)
    If UCase(Mid(Word, i, OLength)) = UCase(Oldstring) Then
        ReplaceChars = Mid(Word, 1, i - 1) & NewString & Mid(Word, i + OLength)
        Exit For
    End If
Next
End Function

You can call a function like this example shows

Sub CallFunction()
    ActiveCell = ReplaceChars("Old Word", "old", "new")
End Sub


-vemaju

 
Old November 21st, 2005, 07:52 PM
Authorized User
 
Join Date: Nov 2005
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Vemaju,
I tried using your function somehow it does not work, it just replaced any first cell like : first cell select from "Bruce " to .... the first cell "Bruce" replaced to new. As my question it just replace any cell(on that column) has a character.
Please take a look.
Thanks
D

D
 
Old November 22nd, 2005, 02:04 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 168
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

Try to do it this way

For i = 1 To Len(Word)
    If UCase(Mid(Word, i, OLength)) = UCase(Oldstring) Then
        ReplaceChars = Mid(Word, 1, i - 1) & NewString & Mid(Word, i + OLength)
        Exit For
    End If
Next
If Len(ReplaceChars) = 0 Then
    ReplaceChars = Word
End If
End Function

Sub CallFunction()
Dim Cell As Range
For Each Cell In Selection
    Cell.Value = ReplaceChars(Cell.Value, "old", "new")
Next
End Sub
-vemaju

 
Old December 5th, 2005, 07:44 PM
Authorized User
 
Join Date: Nov 2005
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Vemaju,
The Macro you'd writen did not work, I tried couple times, it did not place the Range has one Character to New Character: "Tr".
Please take a look.
Thanks
D

D
 
Old December 6th, 2005, 04:30 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 168
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

I don´t understand what you mean?
Some examples.

-vemaju
 
Old December 6th, 2005, 12:25 PM
Authorized User
 
Join Date: Nov 2005
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
I mean in the column some range has one character, I would to remove any range in that column has one character, replace to "Tr". The function you have written, it did not work.
I hope you understand me.
Thanks
D

D
 
Old December 6th, 2005, 04:15 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 168
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

This code works if one cell has one or more characters

Function ReplaceChars(Word As String, Oldstring As String, NewString As String) As String
Dim i As Integer
Dim OLength As Integer
Dim NLength As Integer
OLength = Len(Oldstring)
NLength = Len(NewString)

For i = 1 To Len(Word)
    If UCase(Mid(Word, i, OLength)) = UCase(Oldstring) Then
        ReplaceChars = Mid(Word, 1, i - 1) & NewString & Mid(Word, i + OLength)
        Exit For
    End If
Next
If Len(ReplaceChars) = 0 Then
    ReplaceChars = Word
End If

End Function

'You can call a function like this example shows

Sub CallFunction()
Dim Cell As Range
For Each cell In Selection
    Cell.Value = ReplaceChars(Cell.Value, "e", "tr")
Next
End Sub

-vemaju
 
Old December 7th, 2005, 01:01 PM
Authorized User
 
Join Date: Nov 2005
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
Your function you wrote it works good, do you have any smart function to automatic to remove any range has one character: "A, b, c d.....", instead to "Tr". Now you function does work for replace the character you one to remove as "e" to "Tr".
Thanks ahead
D

D





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 10 Cannot Delete Character lucius BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 3 February 5th, 2008 12:45 AM
Cutting a character from a string arnabghosh Classic ASP Basics 2 August 18th, 2005 07:44 AM
Replacing a character from string itHighway Classic ASP Basics 5 March 14th, 2005 11:15 PM
Search for Character in String Kaynor09 Excel VBA 2 April 24th, 2004 11:13 AM
How do I get rid of the last character in a string Lucy Classic ASP Professional 3 September 30th, 2003 05:33 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.