Solved

Replace function while using EXCELVBA

Posted on 2003-08-20
10
3,411 Views
Last Modified: 2010-05-01
Hi,
 I am trying to use a replace function to replace a field's "," character with spaces. It gives me a compilation error saying Replace function not defined. is there any specific reference that I need to add for using replace functions?. I am using TRIM without any trouble. Can someone suggest any better alternative

Thanks
0
Comment
Question by:jibinpt
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 9191170
Try specifying the VBA library as well, e.g.,

VBA.Replace(...)
0
 
LVL 3

Expert Comment

by:PaultheGreat
ID: 9191180
try this out:

ThisWorkbook.Worksheets(1).Range("A1:J10").Replace ",", " ", xlPart, xlByRows

hope that helps!
-PtG
0
 

Author Comment

by:jibinpt
ID: 9191243
This is my code
ReplacedDesc = Replace(lsProcessSheet.Cells(lrow, glDescCol), ",", " ")..  VBA.Replace is not working
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 28

Expert Comment

by:AzraSound
ID: 9191280
When you type in VBA and then hit the dot (.) does the list of available function calls come up?  Is Replace listed as one of them?
0
 
LVL 14

Expert Comment

by:Tommy Kinard
ID: 9191348
You need to add .Value
ReplacedDesc = Replace(lsProcessSheet.Cells(lrow, glDescCol).Value, ",", " ")

HTH
dragontooth

0
 

Author Comment

by:jibinpt
ID: 9191383
When I type in VBA the dot (.) does come up but Replace is not one among them. RESET  is the only one which comes up with RE.
0
 
LVL 3

Accepted Solution

by:
PaultheGreat earned 50 total points
ID: 9191443
Replace(...) by itself isn't a valid function in VBA for Office 97... below is an excerpt from the Microsoft Excel Visual Basic help file:
------------------------------------

Syntax

expression.Replace(What, Replacement, LookAt, SearchOrder, MatchCase, MatchByte)

expression   Required. An expression that returns a Range object.

------------------------------------
Thus, "expression" is required, and generally you'll use this function as in my previous post. If you want to just replace values in a normal string without using Ranges and whatnot, you'll have to write your own little Replace function, as below:

Public Function replaceStr(ByVal str1 As String, ByVal repThis As String, ByVal withThis As String) As String
    While Left(str1, Len(repThis)) = repThis
        str1 = withThis & Mid(str1, Len(repThis) + 1)
    Wend
    While InStr(2, str1, repThis) > 0
        i = InStr(2, str1, repThis)
        str1 = Left(str1, i - 1) & withThis & Mid(str1, i + Len(repThis))
    Wend
    replaceStr = str1
End Function

hope that helps!
-PtG
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 9191454
Oh, is this VBA 97?  Replace didnt come around until VB6, so I guess that would mean VBA 2000?
0
 
LVL 14

Expert Comment

by:Tommy Kinard
ID: 9191463
Are you getting an error on the replace line or is it just not working?

dragontooth

0
 
LVL 3

Expert Comment

by:PaultheGreat
ID: 9191562
thanks for the points and the A! time to go home now... happy programming, people!

Regards,
PtG
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question