Solved

Replace function while using EXCELVBA

Posted on 2003-08-20
10
3,421 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

695 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