Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Replace function while using EXCELVBA

Posted on 2003-08-20
10
Medium Priority
?
3,451 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 200 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…
Suggested Courses

824 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