Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA Replace command in Excel 1

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
How come the replace command doesn't work in VBA for Excel 97?
 
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:
Code:
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)
    Loop
    ReplaceChars = sStr
End Function
 
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?
 
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
 
This is what I already had:

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

???replacing???

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).
 
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.

DimensionalSolutions@core.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top