Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 154
Default FASTER USE OF THE REPLACE() FUNCTION !!!!!

Hi -

I am using the Replace() function for a very large range. It works okay
but wait time is quite long. Is there a more elegant approach to the
below?

For Each rng In Range("B3:B65500")
If rng.Value < "" Then
rng.Value = Replace(rng.Value, "-", ",")
End If
Next rng


Any help would be appreciated! Thanks

Jay

*** Sent via Developersdex http://www.developersdex.com ***
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default FASTER USE OF THE REPLACE() FUNCTION !!!!!

Try it this way...

Range("B3:B65500").Replace "-", ","

--
Rick (MVP - Excel)



"jay dean" wrote in message
...
Hi -

I am using the Replace() function for a very large range. It works okay
but wait time is quite long. Is there a more elegant approach to the
below?

For Each rng In Range("B3:B65500")
If rng.Value < "" Then
rng.Value = Replace(rng.Value, "-", ",")
End If
Next rng


Any help would be appreciated! Thanks

Jay

*** Sent via Developersdex http://www.developersdex.com ***


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default FASTER USE OF THE REPLACE() FUNCTION !!!!!

I just wanted to mention as a follow up that I'm using the Replace method of
a range rather than VB's Replace function... it will be much faster as you
don't have to loop the cells individually.

--
Rick (MVP - Excel)



"Rick Rothstein" wrote in message
...
Try it this way...

Range("B3:B65500").Replace "-", ","

--
Rick (MVP - Excel)



"jay dean" wrote in message
...
Hi -

I am using the Replace() function for a very large range. It works okay
but wait time is quite long. Is there a more elegant approach to the
below?

For Each rng In Range("B3:B65500")
If rng.Value < "" Then
rng.Value = Replace(rng.Value, "-", ",")
End If
Next rng


Any help would be appreciated! Thanks

Jay

*** Sent via Developersdex http://www.developersdex.com ***


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default FASTER USE OF THE REPLACE() FUNCTION !!!!!

Rick,

I'm baffled --- help.

Using your suggestion and with the code below i get a runtime of ~2.4 seconds.
Un-Comment the calculation and screenupdating lines to disable them and the
runtime goes UP to ~3.1 seconds. Why is that happening, am i making an
elementary error?

Sub BlankDays()
starttime = Timer
'Application.ScreenUpdating = False
'Application.Calculation = xlCalculationManual
Range("B3:B65500").Replace "-", ","
'Application.ScreenUpdating = True
'Application.Calculation = xlCalculationAutomatic
MsgBox "Runtime " & Timer - starttime
End Sub

--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.


"Rick Rothstein" wrote:

Try it this way...

Range("B3:B65500").Replace "-", ","

--
Rick (MVP - Excel)



"jay dean" wrote in message
...
Hi -

I am using the Replace() function for a very large range. It works okay
but wait time is quite long. Is there a more elegant approach to the
below?

For Each rng In Range("B3:B65500")
If rng.Value < "" Then
rng.Value = Replace(rng.Value, "-", ",")
End If
Next rng


Any help would be appreciated! Thanks

Jay

*** Sent via Developersdex http://www.developersdex.com ***


.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 154
Default FASTER USE OF THE REPLACE() FUNCTION !!!!!

Thanks, Rick, JLG, and Mike.
I adopted Rick's approach as it eliminates looping and is fastest!

Thanks, again
Jay

*** Sent via Developersdex http://www.developersdex.com ***


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,565
Default FASTER USE OF THE REPLACE() FUNCTION !!!!!

Would this not work:

For Each rng In Range("B3:B65500")
rng.Replace "-", ","
Next rng




"jay dean" wrote in message
...
Hi -

I am using the Replace() function for a very large range. It works okay
but wait time is quite long. Is there a more elegant approach to the
below?

For Each rng In Range("B3:B65500")
If rng.Value < "" Then
rng.Value = Replace(rng.Value, "-", ",")
End If
Next rng


Any help would be appreciated! Thanks

Jay

*** Sent via Developersdex http://www.developersdex.com ***



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default FASTER USE OF THE REPLACE() FUNCTION !!!!!

You don't have to loop the range... you can apply the range's Replace method
to the entire range all at once...

Range("B3:B65500").Replace "-", ","

--
Rick (MVP - Excel)



"JLGWhiz" wrote in message
...
Would this not work:

For Each rng In Range("B3:B65500")
rng.Replace "-", ","
Next rng




"jay dean" wrote in message
...
Hi -

I am using the Replace() function for a very large range. It works okay
but wait time is quite long. Is there a more elegant approach to the
below?

For Each rng In Range("B3:B65500")
If rng.Value < "" Then
rng.Value = Replace(rng.Value, "-", ",")
End If
Next rng


Any help would be appreciated! Thanks

Jay

*** Sent via Developersdex http://www.developersdex.com ***



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default FASTER USE OF THE REPLACE() FUNCTION !!!!!

Hi,

First let's make sure were not doing too many rows by making the range
dynamic by finding the last row. Then stop the screen updating and the
worksheet calculating. Doing this on my pc reduced the runtime from 11
seconds to less than 4

Sub BlankDays()
Dim LastRow As Long
LastRow = Cells(Cells.Rows.Count, "B").End(xlUp).Row
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
For Each Rng In Range("B3:B" & LastRow)
If Rng.Value < "" Then
Rng.Value = Replace(Rng.Value, "-", ",")
End If
Next Rng
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

End Sub
--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.


"jay dean" wrote:

Hi -

I am using the Replace() function for a very large range. It works okay
but wait time is quite long. Is there a more elegant approach to the
below?

For Each rng In Range("B3:B65500")
If rng.Value < "" Then
rng.Value = Replace(rng.Value, "-", ",")
End If
Next rng


Any help would be appreciated! Thanks

Jay

*** Sent via Developersdex http://www.developersdex.com ***
.

  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 342
Default FASTER USE OF THE REPLACE() FUNCTION !!!!!

Good answer. I don't see what the if statement accomplishes. I the cell is
empty no "-" would be found anyway.

Tom

"Mike H" wrote:

Hi,

First let's make sure were not doing too many rows by making the range
dynamic by finding the last row. Then stop the screen updating and the
worksheet calculating. Doing this on my pc reduced the runtime from 11
seconds to less than 4

Sub BlankDays()
Dim LastRow As Long
LastRow = Cells(Cells.Rows.Count, "B").End(xlUp).Row
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
For Each Rng In Range("B3:B" & LastRow)
If Rng.Value < "" Then
Rng.Value = Replace(Rng.Value, "-", ",")
End If
Next Rng
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

End Sub
--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.


"jay dean" wrote:

Hi -

I am using the Replace() function for a very large range. It works okay
but wait time is quite long. Is there a more elegant approach to the
below?

For Each rng In Range("B3:B65500")
If rng.Value < "" Then
rng.Value = Replace(rng.Value, "-", ",")
End If
Next rng


Any help would be appreciated! Thanks

Jay

*** Sent via Developersdex http://www.developersdex.com ***
.

  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default FASTER USE OF THE REPLACE() FUNCTION !!!!!

Yes,

Excellent point about the IF I never noticed that. I also meant to add that
with a worksheet full of formula the increase in performance will be dramatic.

I filled column c with a simple formula =d1/e1 and re-ran the test and got
4.2 seconds runtime with calculation/screenupdating disabled and 200 seconds
with them enabled.

Surprisingly, well to me it was, with the IF statment removed the run time
only went down to 3.9 seconds but the point about removing it is still valid

Sub BlankDays()
Dim LastRow As Long
LastRow = Cells(Cells.Rows.Count, "B").End(xlUp).Row
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
For Each Rng In Range("B3:B" & LastRow)

Rng.Value = Replace(Rng.Value, "-", ",")

Next Rng
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.


"tompl" wrote:

Good answer. I don't see what the if statement accomplishes. I the cell is
empty no "-" would be found anyway.

Tom

"Mike H" wrote:

Hi,

First let's make sure were not doing too many rows by making the range
dynamic by finding the last row. Then stop the screen updating and the
worksheet calculating. Doing this on my pc reduced the runtime from 11
seconds to less than 4

Sub BlankDays()
Dim LastRow As Long
LastRow = Cells(Cells.Rows.Count, "B").End(xlUp).Row
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
For Each Rng In Range("B3:B" & LastRow)
If Rng.Value < "" Then
Rng.Value = Replace(Rng.Value, "-", ",")
End If
Next Rng
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

End Sub
--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.


"jay dean" wrote:

Hi -

I am using the Replace() function for a very large range. It works okay
but wait time is quite long. Is there a more elegant approach to the
below?

For Each rng In Range("B3:B65500")
If rng.Value < "" Then
rng.Value = Replace(rng.Value, "-", ",")
End If
Next rng


Any help would be appreciated! Thanks

Jay

*** Sent via Developersdex http://www.developersdex.com ***
.



  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default FASTER USE OF THE REPLACE() FUNCTION !!!!!

First off, I would highly suggest the method I posted be used as it will be
much, much faster than looping the cells. However, with that said, if one
were going to use the posted loop, it should be more efficient to test the
cell and only apply the Replace function call if a dash is found... the If
test will execute much faster than the Replace function call will... Replace
is not one of VB's faster functions and, for cells with no dash in them, it
will still spend some time deciding that it has nothing to do.

--
Rick (MVP - Excel)



"tompl" wrote in message
...
Good answer. I don't see what the if statement accomplishes. I the cell
is
empty no "-" would be found anyway.

Tom

"Mike H" wrote:

Hi,

First let's make sure were not doing too many rows by making the range
dynamic by finding the last row. Then stop the screen updating and the
worksheet calculating. Doing this on my pc reduced the runtime from 11
seconds to less than 4

Sub BlankDays()
Dim LastRow As Long
LastRow = Cells(Cells.Rows.Count, "B").End(xlUp).Row
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
For Each Rng In Range("B3:B" & LastRow)
If Rng.Value < "" Then
Rng.Value = Replace(Rng.Value, "-", ",")
End If
Next Rng
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

End Sub
--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.


"jay dean" wrote:

Hi -

I am using the Replace() function for a very large range. It works okay
but wait time is quite long. Is there a more elegant approach to the
below?

For Each rng In Range("B3:B65500")
If rng.Value < "" Then
rng.Value = Replace(rng.Value, "-", ",")
End If
Next rng


Any help would be appreciated! Thanks

Jay

*** Sent via Developersdex http://www.developersdex.com ***
.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can you have a value created by a function replace the function? xak1222 Excel Worksheet Functions 3 September 10th 09 09:37 AM
Replace Function Autumn Dreams Excel Discussion (Misc queries) 3 June 4th 06 12:33 PM
Function that makes excel work faster [email protected] Excel Programming 5 April 25th 06 01:27 PM
Can faster CPU+larger/faster RAM significantly speed up recalulati jmk_li Excel Discussion (Misc queries) 2 September 28th 05 10:24 AM
What's faster -- array formula or UD function? Steve[_77_] Excel Programming 6 August 22nd 05 08:41 PM


All times are GMT +1. The time now is 04:31 PM.

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

About Us

"It's about Microsoft Excel"