A Microsoft Excel forum. ExcelBanter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » ExcelBanter forum » Excel Newsgroups » Excel Programming
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

Limits of Substitute and Replace functions



 
 
Thread Tools Display Modes
  #1  
Old June 20th 12, 04:26 AM
deutz deutz is offline
Junior Member
 
First recorded activity by ExcelBanter: May 2012
Posts: 13
Default Limits of Substitute and Replace functions

Hi and thanks in advance,

I am using Excel 2003

I have a sheet that has in column A some long strings and I want a quick way, preferably without looping, to replace some characters with another character.

The problem I have run into is that when I use Replace in VBA it is only applied to cells with no more than 1024 characters and so I tried using Substitute but that crashes if there is more than 1271 characters in a cell so I don't know how to process these long strings quickly.

I tried the following and hit the limits mentioned above:

Code:
Range("A:A").Select
Selection.Replace What:=strOld, Replacement:=strNew, LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Code:
With Range("A1", Range("A" & Rows.Count).End(xlUp))
    .Value = Application.Substitute(.Cells, strOld, strNew)
End With
Ads
  #2  
Old June 20th 12, 09:08 AM posted to microsoft.public.excel.programming
Auric__
external usenet poster
 
Posts: 304
Default Limits of Substitute and Replace functions

deutz wrote:

> I am using Excel 2003
>
> I have a sheet that has in column A some long strings and I want a quick
> way, preferably without looping, to replace some characters with another
> character.
>
> The problem I have run into is that when I use Replace in VBA it is only
> applied to cells with no more than 1024 characters and so I tried using
> Substitute but that crashes if there is more than 1271 characters in a
> cell so I don't know how to process these long strings quickly.
>
> I tried the following and hit the limits mentioned above:


If the Replace function is in 2003, you can do this:
Selection.Value = Replace(Selection.Value, strOld, strNew)

In 2007 it works for me with a 32,768-character string. (I didn't test it
further than that.)

If the Replace function is *not* in 2003, you can use Split and Join:
Selection.Value = Join(Split(Selection.Value, strOld), strNew)

Also works for me with the same 32,768-character string. I *believe* it
should work as far back as Excel 2000.

--
- Good lord!
- He's not in.
  #3  
Old June 21st 12, 02:59 AM
deutz deutz is offline
Junior Member
 
First recorded activity by ExcelBanter: May 2012
Posts: 13
Default

Quote:
Originally Posted by Auric__ View Post
deutz wrote:

> I am using Excel 2003
>
> I have a sheet that has in column A some long strings and I want a quick
> way, preferably without looping, to replace some characters with another
> character.
>
> The problem I have run into is that when I use Replace in VBA it is only
> applied to cells with no more than 1024 characters and so I tried using
> Substitute but that crashes if there is more than 1271 characters in a
> cell so I don't know how to process these long strings quickly.
>
> I tried the following and hit the limits mentioned above:


If the Replace function is in 2003, you can do this:
Selection.Value = Replace(Selection.Value, strOld, strNew)

In 2007 it works for me with a 32,768-character string. (I didn't test it
further than that.)

If the Replace function is *not* in 2003, you can use Split and Join:
Selection.Value = Join(Split(Selection.Value, strOld), strNew)

Also works for me with the same 32,768-character string. I *believe* it
should work as far back as Excel 2000.

--
- Good lord!
- He's not in.
Thanks for your suggestions Auric,

I substituted strOld and strNew with my strings and added code line
Range("A:A").Select just before your line of code

I then tried both methods but got a Type Mismatch error each time.
  #4  
Old June 21st 12, 08:55 AM posted to microsoft.public.excel.programming
Auric__
external usenet poster
 
Posts: 304
Default Limits of Substitute and Replace functions

deutz wrote:

> Auric__;1602931 Wrote:
>> deutz wrote:
>>
>> > I am using Excel 2003
>> >
>> > I have a sheet that has in column A some long strings and I want a
>> > quick way, preferably without looping, to replace some characters
>> > with another character.
>> >
>> > The problem I have run into is that when I use Replace in VBA it is
>> > only applied to cells with no more than 1024 characters and so I
>> > tried using Substitute but that crashes if there is more than 1271
>> > characters in a cell so I don't know how to process these long
>> > strings quickly.
>> >
>> > I tried the following and hit the limits mentioned above:-

>>
>> If the Replace function is in 2003, you can do this:
>> Selection.Value = Replace(Selection.Value, strOld, strNew)
>>
>> In 2007 it works for me with a 32,768-character string. (I didn't test
>> it further than that.)
>>
>> If the Replace function is *not* in 2003, you can use Split and Join:
>> Selection.Value = Join(Split(Selection.Value, strOld), strNew)
>>
>> Also works for me with the same 32,768-character string. I *believe* it
>> should work as far back as Excel 2000.

>
> Thanks for your suggestions Auric,
>
> I substituted strOld and strNew with my strings and added code line
> Range("A:A").Select just before your line of code
>
> I then tried both methods but got a Type Mismatch error each time.


That's because they both expect a single string, i.e. a single cell. Try this
instead:
Dim cell As Range
For Each cell In Selection
cell.Value = Replace(cell.Value, strOld, strNew)
Next

--
Gene pool's running low on chlorine again.
  #5  
Old June 22nd 12, 12:43 AM
deutz deutz is offline
Junior Member
 
First recorded activity by ExcelBanter: May 2012
Posts: 13
Default

Quote:
Originally Posted by Auric__ View Post
deutz wrote:

> Auric__;1602931 Wrote:
>> deutz wrote:
>>
>> > I am using Excel 2003
>> >
>> > I have a sheet that has in column A some long strings and I want a
>> > quick way, preferably without looping, to replace some characters
>> > with another character.
>> >
>> > The problem I have run into is that when I use Replace in VBA it is
>> > only applied to cells with no more than 1024 characters and so I
>> > tried using Substitute but that crashes if there is more than 1271
>> > characters in a cell so I don't know how to process these long
>> > strings quickly.
>> >
>> > I tried the following and hit the limits mentioned above:-

>>
>> If the Replace function is in 2003, you can do this:
>> Selection.Value = Replace(Selection.Value, strOld, strNew)
>>
>> In 2007 it works for me with a 32,768-character string. (I didn't test
>> it further than that.)
>>
>> If the Replace function is *not* in 2003, you can use Split and Join:
>> Selection.Value = Join(Split(Selection.Value, strOld), strNew)
>>
>> Also works for me with the same 32,768-character string. I *believe* it
>> should work as far back as Excel 2000.

>
> Thanks for your suggestions Auric,
>
> I substituted strOld and strNew with my strings and added code line
> Range("A:A").Select just before your line of code
>
> I then tried both methods but got a Type Mismatch error each time.


That's because they both expect a single string, i.e. a single cell. Try this
instead:
Dim cell As Range
For Each cell In Selection
cell.Value = Replace(cell.Value, strOld, strNew)
Next

--
Gene pool's running low on chlorine again.
Thanks, I have similar code that loops thru each cell and works ok but thought there may be a quicker way to do it without looping, in one fell swoop.
  #6  
Old June 22nd 12, 03:39 AM posted to microsoft.public.excel.programming
GS[_2_]
external usenet poster
 
Posts: 2,245
Default Limits of Substitute and Replace functions

> Thanks, I have similar code that loops thru each cell and works ok but
> thought there may be a quicker way to do it without looping, in one fell
> swoop.


It will always be slower to act directly on the cells. It will be
orders of magnitude faster to 'dump' the range into an array, loop the
array (in memory), then 'dump' the array back into the range.

Example:
Sub ReplaceData()
Dim vData As Variant, i As Long
vData = Selection
For i = LBound(vData) To UBound(vData) 'assumes 1 column selected
vData(i, 1) = Replace(vData(i, 1), strOld, strNew)
Next 'i
Selection = vData
End Sub

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace and Substitute Functions HLS[_2_] Excel Programming 0 September 21st 06 04:37 PM
wildcards with substitute of replace Jay Fincannon Excel Programming 5 September 16th 06 07:24 PM
Replace/substitute Viktor Ygdorff Excel Programming 0 July 17th 06 02:35 PM
Substitute/Replace Viktor Ygdorff Charts and Charting in Excel 0 July 17th 06 12:37 PM
Looking for a site with functions that substitute the ATP functions Franz Verga Excel Worksheet Functions 3 June 24th 06 04:30 AM


All times are GMT +1. The time now is 03:12 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Copyright ©2004-2013 ExcelBanter.
The comments are property of their posters.