![]() |
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 *** |
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 *** |
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 *** |
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 *** |
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 *** . |
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 *** |
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 *** . |
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 *** . |
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 *** . |
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 *** . |
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 *** |
All times are GMT +1. The time now is 09:45 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com