Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,101
Default Is there a faster way to loop thru cells

Where lastRow maybe +/- 25000
For i = 5 To lastRow
xlApp.Cells(i, "G").value = _
(xlApp.Cells(i, "E").value - xlApp.Cells(i, "D").value) _
* xlApp.Cells(i, "F").value
Next
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default Is there a faster way to loop thru cells

how about a formula?

With ws
.Range("G5:G" & lastrow).Formula = "=(" & .Range("E5").Address(0, 0) & _
"-" & .Range("D5").Address(0, 0) & ")*" & .Range("F5").Address(0, 0)
End With
End Sub

--


Gary


"Mike" wrote in message
...
Where lastRow maybe +/- 25000
For i = 5 To lastRow
xlApp.Cells(i, "G").value = _
(xlApp.Cells(i, "E").value - xlApp.Cells(i, "D").value) _
* xlApp.Cells(i, "F").value
Next



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default Is there a faster way to loop thru cells

here's the code i forgot to post

Sub test()
Dim lastrow As Long
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
lastrow = ws.Cells(Rows.Count, "D").End(xlUp).Row
With ws
.Range("G5:G" & lastrow).Formula = "=(" & .Range("E5").Address(0, 0) & _
"-" & .Range("D5").Address(0, 0) & ")*" & .Range("F5").Address(0, 0)
End With
End Sub


--


Gary


"Mike" wrote in message
...
Where lastRow maybe +/- 25000
For i = 5 To lastRow
xlApp.Cells(i, "G").value = _
(xlApp.Cells(i, "E").value - xlApp.Cells(i, "D").value) _
* xlApp.Cells(i, "F").value
Next



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,101
Default Is there a faster way to loop thru cells

Thanks Gary
While I was waiting i was trying this which was faster then the loop i posted
But for speed how does this compare to what you suggest
Dim rng As Excel.Range = xlApp.Range("G5")
xlApp.Range("G5").Formula = "=(E5-D5)*F5"
rng.AutoFill(xlApp.Range("G5:G" & lastRow),
Excel.XlAutoFillType.xlFillCopy)

"Gary Keramidas" wrote:

here's the code i forgot to post

Sub test()
Dim lastrow As Long
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
lastrow = ws.Cells(Rows.Count, "D").End(xlUp).Row
With ws
.Range("G5:G" & lastrow).Formula = "=(" & .Range("E5").Address(0, 0) & _
"-" & .Range("D5").Address(0, 0) & ")*" & .Range("F5").Address(0, 0)
End With
End Sub


--


Gary


"Mike" wrote in message
...
Where lastRow maybe +/- 25000
For i = 5 To lastRow
xlApp.Cells(i, "G").value = _
(xlApp.Cells(i, "E").value - xlApp.Cells(i, "D").value) _
* xlApp.Cells(i, "F").value
Next




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default Is there a faster way to loop thru cells

if i had to guess, i'd say mine was faster. fraction of a second? but i'm not
sure. i've used both.

--


Gary


"Mike" wrote in message
...
Thanks Gary
While I was waiting i was trying this which was faster then the loop i posted
But for speed how does this compare to what you suggest
Dim rng As Excel.Range = xlApp.Range("G5")
xlApp.Range("G5").Formula = "=(E5-D5)*F5"
rng.AutoFill(xlApp.Range("G5:G" & lastRow),
Excel.XlAutoFillType.xlFillCopy)

"Gary Keramidas" wrote:

here's the code i forgot to post

Sub test()
Dim lastrow As Long
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
lastrow = ws.Cells(Rows.Count, "D").End(xlUp).Row
With ws
.Range("G5:G" & lastrow).Formula = "=(" & .Range("E5").Address(0, 0)
& _
"-" & .Range("D5").Address(0, 0) & ")*" & .Range("F5").Address(0,
0)
End With
End Sub


--


Gary


"Mike" wrote in message
...
Where lastRow maybe +/- 25000
For i = 5 To lastRow
xlApp.Cells(i, "G").value = _
(xlApp.Cells(i, "E").value - xlApp.Cells(i, "D").value) _
* xlApp.Cells(i, "F").value
Next








  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,101
Default Is there a faster way to loop thru cells

Thanks Gary

"Gary Keramidas" wrote:

if i had to guess, i'd say mine was faster. fraction of a second? but i'm not
sure. i've used both.

--


Gary


"Mike" wrote in message
...
Thanks Gary
While I was waiting i was trying this which was faster then the loop i posted
But for speed how does this compare to what you suggest
Dim rng As Excel.Range = xlApp.Range("G5")
xlApp.Range("G5").Formula = "=(E5-D5)*F5"
rng.AutoFill(xlApp.Range("G5:G" & lastRow),
Excel.XlAutoFillType.xlFillCopy)

"Gary Keramidas" wrote:

here's the code i forgot to post

Sub test()
Dim lastrow As Long
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
lastrow = ws.Cells(Rows.Count, "D").End(xlUp).Row
With ws
.Range("G5:G" & lastrow).Formula = "=(" & .Range("E5").Address(0, 0)
& _
"-" & .Range("D5").Address(0, 0) & ")*" & .Range("F5").Address(0,
0)
End With
End Sub


--


Gary


"Mike" wrote in message
...
Where lastRow maybe +/- 25000
For i = 5 To lastRow
xlApp.Cells(i, "G").value = _
(xlApp.Cells(i, "E").value - xlApp.Cells(i, "D").value) _
* xlApp.Cells(i, "F").value
Next






  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default Is there a faster way to loop thru cells

some mvp may be able to give you a better answer.

--


Gary


"Mike" wrote in message
...
Thanks Gary

"Gary Keramidas" wrote:

if i had to guess, i'd say mine was faster. fraction of a second? but i'm not
sure. i've used both.

--


Gary


"Mike" wrote in message
...
Thanks Gary
While I was waiting i was trying this which was faster then the loop i
posted
But for speed how does this compare to what you suggest
Dim rng As Excel.Range = xlApp.Range("G5")
xlApp.Range("G5").Formula = "=(E5-D5)*F5"
rng.AutoFill(xlApp.Range("G5:G" & lastRow),
Excel.XlAutoFillType.xlFillCopy)

"Gary Keramidas" wrote:

here's the code i forgot to post

Sub test()
Dim lastrow As Long
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
lastrow = ws.Cells(Rows.Count, "D").End(xlUp).Row
With ws
.Range("G5:G" & lastrow).Formula = "=(" & .Range("E5").Address(0,
0)
& _
"-" & .Range("D5").Address(0, 0) & ")*" &
.Range("F5").Address(0,
0)
End With
End Sub


--


Gary


"Mike" wrote in message
...
Where lastRow maybe +/- 25000
For i = 5 To lastRow
xlApp.Cells(i, "G").value = _
(xlApp.Cells(i, "E").value - xlApp.Cells(i, "D").value) _
* xlApp.Cells(i, "F").value
Next








  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default Is there a faster way to loop thru cells

On Apr 10, 10:29*am, Mike wrote:
Where lastRow maybe +/- 25000
* * * * For i = 5 To lastRow
* * * * * * xlApp.Cells(i, "G").value = _
* * * * * * (xlApp.Cells(i, "E").value - xlApp.Cells(i, "D").value) _
* * * * * * * xlApp.Cells(i, "F").value
* * * * Next



It's quicker to read the data into an array, do all the maths in VBA,
and write it all back in one operation.

For instance

Sub writeloop2()
Dim lastrow As Long, i As Long, xlApp As Variant

xlApp = Range("d1:g25000").Value

lastrow = UBound(xlApp) - LBound(xlApp) + 1
For i = 5 To lastrow
xlApp(i, 4) = _
(xlApp(i, 2) - xlApp(i, 1)) _
* xlApp(i, 3)
Next i
Range("d1:g25000").Value = xlApp
End Sub


In XL2007 this is virtually instantaneous for 25000 lines, compared
with about 15 seconds seconds reading and writing line by line.
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
Technically, can anyone explain to me why Cells.Find() is so much faster than a loop? Will[_13_] Excel Programming 3 November 6th 07 07:34 PM
For Next loop faster with counter after Next? RB Smissaert Excel Programming 22 August 28th 06 05:12 AM
Faster way to loop through two ranges YH Excel Programming 5 August 26th 06 05:17 PM
Is there a faster loop than this Andibevan Excel Programming 4 August 25th 06 03:27 PM
Faster For-Next Loop? [email protected] Excel Programming 3 January 7th 05 09:08 PM


All times are GMT +1. The time now is 06:45 AM.

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

About Us

"It's about Microsoft Excel"