Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 *** |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 *** . |
#6
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 *** |
#7
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 *** . |
#8
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 *** . |
#9
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 *** . |
#10
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 *** . |
#11
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 *** |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Can you have a value created by a function replace the function? | Excel Worksheet Functions | |||
Replace Function | Excel Discussion (Misc queries) | |||
Function that makes excel work faster | Excel Programming | |||
Can faster CPU+larger/faster RAM significantly speed up recalulati | Excel Discussion (Misc queries) | |||
What's faster -- array formula or UD function? | Excel Programming |