Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

VBA Replace command in Excel

VBA Replace command in Excel

VBA Replace command in Excel

How come the replace command doesn't work in VBA for Excel 97?

RE: VBA Replace command in Excel

That is a VB command and is not available in VBA. You will need to do it using a couple more lines of code.
Try using a function like this:

Function ReplaceChars(sStr As String) As String
    Dim sSearchChar As String
    Dim sReplaceChar As String
    Dim sTmp As String
    Dim iLoc As Integer
    sSearchChar = ","
    sReplaceChar = "-"
    iLoc = InStr(1, sStr, sSearchChar)
    Do While iLoc > 0
        sTmp = Left(sStr, iLoc - 1) & sReplaceChar & Right(sStr, Len(sStr) - iLoc)
        sStr = sTmp
        iLoc = InStr(1, sStr, sSearchChar)
    ReplaceChars = sStr
End Function

RE: VBA Replace command in Excel

Aww, that code is so complicated. I don't understand its structure
I need to do some automatical replacing in a file on my harddrive. The words "xxyyzz_24" need to be replaced by the words "0level", and the words "xxyyzz_48" need to be replaced by the word "1level". I should probably get the normal visual basic for this. But maybe someone can give me the VBA code to do that?

RE: VBA Replace command in Excel

What does the contents of the file look like? Have you written any code to read the contents of the file? If so, please post it so we can better help you.

If you are just replacing values in cells on a worksheet? If so, you can use wildcards in the Edit > Replace command.

Replace: ??????_24 with 0level
Replace: ??????_48 with 1level

RE: VBA Replace command in Excel

This is what I already had:

FileCount = 1
Open "c:\projects\shared\output5.dat" For Binary As #1
G$ = String$(LOF(1), 0)
Get #1, 1, G$
Close #1


Kill "c:\projects\shared\output5.dat"
Open "c:\projects\shared\output5.dat" For Binary As #1
Put #1, 1, G$
Close #1

Between that code I wanted to put the replace commands. But they didn't work.
That "output5.dat" is a file generated by another program. It has nothing to do with Excel, but since I allways have Excel opened for other things, I thought I could as well use the VBA option to edit that file automatically (like I said, I don't have the normal visual basic).

RE: VBA Replace command in Excel

Could you send me a copy of the output5.dat file? It would be easier for me to debug with a copy of the actual file.


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close