Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 144
Default Loop works correctly but takes extremely long to execute

Hi all,

Below is a sub I created to sum up values from a given column similar to
what a Pivot Table would do (from my limited experience, it seems that
refreshing pivot tables using vba causes the code to hang up or take very
long to execute).

The issue is that it simply takes forever for the code to execute -
literally I can see it summing up values in each cell as it executes - so
slow that I can see each value in the cell as it changes. I have written
similar code before that executes much much faster...I was thinking that
maybe I just needed to use the vba code cleaner, but even after using that,
the code executes at the same speed.

I'm wondering if it does make sense to you that it would take extremely long
for this code to execute (there are a total of 280 rows of data in range2 and
2071 records in range3).

Sub bringit()

'Calculate and populate commissions by unique Member ID
With Sheets("Open Transactions by Member ID")
If Len(.Range("A3")) < 0 Then
Set range2 = .Range("A2", .Range("A2").End(xlDown))
Else
End If
End With
With Sheets("Transaction Summary")
If Len(.Range("A3")) < 0 Then
Set range3 = .Range("A2", .Range("A2").End(xlDown))
Else
End If
End With


With Sheets("Open Transactions by Member ID")
If Len(.Range("A2")) < 0 Then
For Each B In range2
tottran = 0
opentran = 0
openeligtran = 0
For Each C In range3
If B = C Then
tottran = tottran + C.Offset(0, 9)
If C.Offset(0, 12).Value = "Open" Then
opentran = opentran + C.Offset(0, 9)
Else
If C.Offset(0, 12).Value = "Payment Issued" Then
openeligtran = openeligtran + C.Offset(0, 9)
Else
End If
End If
Else
B.Offset(0, 1).Value = tottran
B.Offset(0, 3).Value = opentran
B.Offset(0, 4).Value = openeligtran
B.Offset(0, 2).Value = tottran - opentran
End If
Next
Next
Else
End If
End With


End Sub

--
Robert
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 140
Default Loop works correctly but takes extremely long to execute

1 thaing that will help speed this up
Turn off ScreenUpdating at the start of the procedure
And turn it back on again at the end.

Syntax - Application.ScreeUpdating = False 'Turn Off
Application.ScreeUpdating = True 'Turn On

Might only be anominal time saving - but it is a time saving

"robs3131" wrote:

Hi all,

Below is a sub I created to sum up values from a given column similar to
what a Pivot Table would do (from my limited experience, it seems that
refreshing pivot tables using vba causes the code to hang up or take very
long to execute).

The issue is that it simply takes forever for the code to execute -
literally I can see it summing up values in each cell as it executes - so
slow that I can see each value in the cell as it changes. I have written
similar code before that executes much much faster...I was thinking that
maybe I just needed to use the vba code cleaner, but even after using that,
the code executes at the same speed.

I'm wondering if it does make sense to you that it would take extremely long
for this code to execute (there are a total of 280 rows of data in range2 and
2071 records in range3).

Sub bringit()

'Calculate and populate commissions by unique Member ID
With Sheets("Open Transactions by Member ID")
If Len(.Range("A3")) < 0 Then
Set range2 = .Range("A2", .Range("A2").End(xlDown))
Else
End If
End With
With Sheets("Transaction Summary")
If Len(.Range("A3")) < 0 Then
Set range3 = .Range("A2", .Range("A2").End(xlDown))
Else
End If
End With


With Sheets("Open Transactions by Member ID")
If Len(.Range("A2")) < 0 Then
For Each B In range2
tottran = 0
opentran = 0
openeligtran = 0
For Each C In range3
If B = C Then
tottran = tottran + C.Offset(0, 9)
If C.Offset(0, 12).Value = "Open" Then
opentran = opentran + C.Offset(0, 9)
Else
If C.Offset(0, 12).Value = "Payment Issued" Then
openeligtran = openeligtran + C.Offset(0, 9)
Else
End If
End If
Else
B.Offset(0, 1).Value = tottran
B.Offset(0, 3).Value = opentran
B.Offset(0, 4).Value = openeligtran
B.Offset(0, 2).Value = tottran - opentran
End If
Next
Next
Else
End If
End With


End Sub

--
Robert

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Loop works correctly but takes extremely long to execute

your making 280 x 2071 = 579880 loops.

You could easily do this formulas in 'Open Transactions by Member ID'
B2:
=countif('Transaction Summary'!$A$2:$A$3000,A2)
D2:
=Sumproduct(--(A2='Transaction Summary'!$A$2:$A$3000),--('Transaction
Summary'!$M$2:$M$3000="Open"),'Transaction Summary'!$J$2:$J$3000)
E2:
=Sumproduct(--(A2='Transaction Summary'!$A$2:$A$3000),--('Transaction
Summary'!$M$2:$M$3000="Payment Issued"),'Transaction Summary'!$J$2:$J$3000)
C2: =B2-D2
=

and so forth.

If you don't want to put the formulas in the worksheet directly, at least
use them in your code.

--
Regards,
Tom Ogilvy




"robs3131" wrote:

Hi all,

Below is a sub I created to sum up values from a given column similar to
what a Pivot Table would do (from my limited experience, it seems that
refreshing pivot tables using vba causes the code to hang up or take very
long to execute).

The issue is that it simply takes forever for the code to execute -
literally I can see it summing up values in each cell as it executes - so
slow that I can see each value in the cell as it changes. I have written
similar code before that executes much much faster...I was thinking that
maybe I just needed to use the vba code cleaner, but even after using that,
the code executes at the same speed.

I'm wondering if it does make sense to you that it would take extremely long
for this code to execute (there are a total of 280 rows of data in range2 and
2071 records in range3).

Sub bringit()

'Calculate and populate commissions by unique Member ID
With Sheets("Open Transactions by Member ID")
If Len(.Range("A3")) < 0 Then
Set range2 = .Range("A2", .Range("A2").End(xlDown))
Else
End If
End With
With Sheets("Transaction Summary")
If Len(.Range("A3")) < 0 Then
Set range3 = .Range("A2", .Range("A2").End(xlDown))
Else
End If
End With


With Sheets("Open Transactions by Member ID")
If Len(.Range("A2")) < 0 Then
For Each B In range2
tottran = 0
opentran = 0
openeligtran = 0
For Each C In range3
If B = C Then
tottran = tottran + C.Offset(0, 9)
If C.Offset(0, 12).Value = "Open" Then
opentran = opentran + C.Offset(0, 9)
Else
If C.Offset(0, 12).Value = "Payment Issued" Then
openeligtran = openeligtran + C.Offset(0, 9)
Else
End If
End If
Else
B.Offset(0, 1).Value = tottran
B.Offset(0, 3).Value = opentran
B.Offset(0, 4).Value = openeligtran
B.Offset(0, 2).Value = tottran - opentran
End If
Next
Next
Else
End If
End With


End Sub

--
Robert

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 144
Default Loop works correctly but takes extremely long to execute

Thanks Tom and Steve. I was planning on turning off the screen update with
my final code but I know that even with this it wouldn't speed it up.

Tom - I will go ahead and use formulas. It was always my thought that it is
faster to use code, even with intensive loops, than it is to use things such
as vlookups or pivot tables or other formulas in code as it sems that Excel
slows down significantly intermittingly when running such code. IE - it may
run ok today, but tomorrow it will get hung up and show something such as
"Calculating" or "Sort" or "Cells" at the bottom of Excel with bars showing
where Excel is at in that process. Do either of you ever run into this
intermittent issue? Just curious. Thanks.
--
Robert


"Tom Ogilvy" wrote:

your making 280 x 2071 = 579880 loops.

You could easily do this formulas in 'Open Transactions by Member ID'
B2:
=countif('Transaction Summary'!$A$2:$A$3000,A2)
D2:
=Sumproduct(--(A2='Transaction Summary'!$A$2:$A$3000),--('Transaction
Summary'!$M$2:$M$3000="Open"),'Transaction Summary'!$J$2:$J$3000)
E2:
=Sumproduct(--(A2='Transaction Summary'!$A$2:$A$3000),--('Transaction
Summary'!$M$2:$M$3000="Payment Issued"),'Transaction Summary'!$J$2:$J$3000)
C2: =B2-D2
=

and so forth.

If you don't want to put the formulas in the worksheet directly, at least
use them in your code.

--
Regards,
Tom Ogilvy




"robs3131" wrote:

Hi all,

Below is a sub I created to sum up values from a given column similar to
what a Pivot Table would do (from my limited experience, it seems that
refreshing pivot tables using vba causes the code to hang up or take very
long to execute).

The issue is that it simply takes forever for the code to execute -
literally I can see it summing up values in each cell as it executes - so
slow that I can see each value in the cell as it changes. I have written
similar code before that executes much much faster...I was thinking that
maybe I just needed to use the vba code cleaner, but even after using that,
the code executes at the same speed.

I'm wondering if it does make sense to you that it would take extremely long
for this code to execute (there are a total of 280 rows of data in range2 and
2071 records in range3).

Sub bringit()

'Calculate and populate commissions by unique Member ID
With Sheets("Open Transactions by Member ID")
If Len(.Range("A3")) < 0 Then
Set range2 = .Range("A2", .Range("A2").End(xlDown))
Else
End If
End With
With Sheets("Transaction Summary")
If Len(.Range("A3")) < 0 Then
Set range3 = .Range("A2", .Range("A2").End(xlDown))
Else
End If
End With


With Sheets("Open Transactions by Member ID")
If Len(.Range("A2")) < 0 Then
For Each B In range2
tottran = 0
opentran = 0
openeligtran = 0
For Each C In range3
If B = C Then
tottran = tottran + C.Offset(0, 9)
If C.Offset(0, 12).Value = "Open" Then
opentran = opentran + C.Offset(0, 9)
Else
If C.Offset(0, 12).Value = "Payment Issued" Then
openeligtran = openeligtran + C.Offset(0, 9)
Else
End If
End If
Else
B.Offset(0, 1).Value = tottran
B.Offset(0, 3).Value = opentran
B.Offset(0, 4).Value = openeligtran
B.Offset(0, 2).Value = tottran - opentran
End If
Next
Next
Else
End If
End With


End Sub

--
Robert

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,440
Default Loop works correctly but takes extremely long to execute

Hi Robert,

Worksheet formulas are generally very fast, certainly compared to code doing the same thing.
For lots of info about Excel performance, visit Charles Williams' site:

www.decisionmodels.com

--
Kind regards,

Niek Otten
Microsoft MVP - Excel


"robs3131" wrote in message ...
| Thanks Tom and Steve. I was planning on turning off the screen update with
| my final code but I know that even with this it wouldn't speed it up.
|
| Tom - I will go ahead and use formulas. It was always my thought that it is
| faster to use code, even with intensive loops, than it is to use things such
| as vlookups or pivot tables or other formulas in code as it sems that Excel
| slows down significantly intermittingly when running such code. IE - it may
| run ok today, but tomorrow it will get hung up and show something such as
| "Calculating" or "Sort" or "Cells" at the bottom of Excel with bars showing
| where Excel is at in that process. Do either of you ever run into this
| intermittent issue? Just curious. Thanks.
| --
| Robert
|
|
| "Tom Ogilvy" wrote:
|
| your making 280 x 2071 = 579880 loops.
|
| You could easily do this formulas in 'Open Transactions by Member ID'
| B2:
| =countif('Transaction Summary'!$A$2:$A$3000,A2)
| D2:
| =Sumproduct(--(A2='Transaction Summary'!$A$2:$A$3000),--('Transaction
| Summary'!$M$2:$M$3000="Open"),'Transaction Summary'!$J$2:$J$3000)
| E2:
| =Sumproduct(--(A2='Transaction Summary'!$A$2:$A$3000),--('Transaction
| Summary'!$M$2:$M$3000="Payment Issued"),'Transaction Summary'!$J$2:$J$3000)
| C2: =B2-D2
| =
|
| and so forth.
|
| If you don't want to put the formulas in the worksheet directly, at least
| use them in your code.
|
| --
| Regards,
| Tom Ogilvy
|
|
|
|
| "robs3131" wrote:
|
| Hi all,
|
| Below is a sub I created to sum up values from a given column similar to
| what a Pivot Table would do (from my limited experience, it seems that
| refreshing pivot tables using vba causes the code to hang up or take very
| long to execute).
|
| The issue is that it simply takes forever for the code to execute -
| literally I can see it summing up values in each cell as it executes - so
| slow that I can see each value in the cell as it changes. I have written
| similar code before that executes much much faster...I was thinking that
| maybe I just needed to use the vba code cleaner, but even after using that,
| the code executes at the same speed.
|
| I'm wondering if it does make sense to you that it would take extremely long
| for this code to execute (there are a total of 280 rows of data in range2 and
| 2071 records in range3).
|
| Sub bringit()
|
| 'Calculate and populate commissions by unique Member ID
| With Sheets("Open Transactions by Member ID")
| If Len(.Range("A3")) < 0 Then
| Set range2 = .Range("A2", .Range("A2").End(xlDown))
| Else
| End If
| End With
| With Sheets("Transaction Summary")
| If Len(.Range("A3")) < 0 Then
| Set range3 = .Range("A2", .Range("A2").End(xlDown))
| Else
| End If
| End With
|
|
| With Sheets("Open Transactions by Member ID")
| If Len(.Range("A2")) < 0 Then
| For Each B In range2
| tottran = 0
| opentran = 0
| openeligtran = 0
| For Each C In range3
| If B = C Then
| tottran = tottran + C.Offset(0, 9)
| If C.Offset(0, 12).Value = "Open" Then
| opentran = opentran + C.Offset(0, 9)
| Else
| If C.Offset(0, 12).Value = "Payment Issued" Then
| openeligtran = openeligtran + C.Offset(0, 9)
| Else
| End If
| End If
| Else
| B.Offset(0, 1).Value = tottran
| B.Offset(0, 3).Value = opentran
| B.Offset(0, 4).Value = openeligtran
| B.Offset(0, 2).Value = tottran - opentran
| End If
| Next
| Next
| Else
| End If
| End With
|
|
| End Sub
|
| --
| Robert




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Loop works correctly but takes extremely long to execute

I don't think it will be an issue with this workbook, but
the compromise is to have your code put in the formulas, then replace the
formulas with the values they return

With Range("B2:B281")
.Formula = "=countif('Transaction Summary'!$A$2:$A$3000,A2)"
.Formula = .Value
end With

as an example.

--
Regards,
Tom Ogilvy


"robs3131" wrote:

Thanks Tom and Steve. I was planning on turning off the screen update with
my final code but I know that even with this it wouldn't speed it up.

Tom - I will go ahead and use formulas. It was always my thought that it is
faster to use code, even with intensive loops, than it is to use things such
as vlookups or pivot tables or other formulas in code as it sems that Excel
slows down significantly intermittingly when running such code. IE - it may
run ok today, but tomorrow it will get hung up and show something such as
"Calculating" or "Sort" or "Cells" at the bottom of Excel with bars showing
where Excel is at in that process. Do either of you ever run into this
intermittent issue? Just curious. Thanks.
--
Robert


"Tom Ogilvy" wrote:

your making 280 x 2071 = 579880 loops.

You could easily do this formulas in 'Open Transactions by Member ID'
B2:
=countif('Transaction Summary'!$A$2:$A$3000,A2)
D2:
=Sumproduct(--(A2='Transaction Summary'!$A$2:$A$3000),--('Transaction
Summary'!$M$2:$M$3000="Open"),'Transaction Summary'!$J$2:$J$3000)
E2:
=Sumproduct(--(A2='Transaction Summary'!$A$2:$A$3000),--('Transaction
Summary'!$M$2:$M$3000="Payment Issued"),'Transaction Summary'!$J$2:$J$3000)
C2: =B2-D2
=

and so forth.

If you don't want to put the formulas in the worksheet directly, at least
use them in your code.

--
Regards,
Tom Ogilvy




"robs3131" wrote:

Hi all,

Below is a sub I created to sum up values from a given column similar to
what a Pivot Table would do (from my limited experience, it seems that
refreshing pivot tables using vba causes the code to hang up or take very
long to execute).

The issue is that it simply takes forever for the code to execute -
literally I can see it summing up values in each cell as it executes - so
slow that I can see each value in the cell as it changes. I have written
similar code before that executes much much faster...I was thinking that
maybe I just needed to use the vba code cleaner, but even after using that,
the code executes at the same speed.

I'm wondering if it does make sense to you that it would take extremely long
for this code to execute (there are a total of 280 rows of data in range2 and
2071 records in range3).

Sub bringit()

'Calculate and populate commissions by unique Member ID
With Sheets("Open Transactions by Member ID")
If Len(.Range("A3")) < 0 Then
Set range2 = .Range("A2", .Range("A2").End(xlDown))
Else
End If
End With
With Sheets("Transaction Summary")
If Len(.Range("A3")) < 0 Then
Set range3 = .Range("A2", .Range("A2").End(xlDown))
Else
End If
End With


With Sheets("Open Transactions by Member ID")
If Len(.Range("A2")) < 0 Then
For Each B In range2
tottran = 0
opentran = 0
openeligtran = 0
For Each C In range3
If B = C Then
tottran = tottran + C.Offset(0, 9)
If C.Offset(0, 12).Value = "Open" Then
opentran = opentran + C.Offset(0, 9)
Else
If C.Offset(0, 12).Value = "Payment Issued" Then
openeligtran = openeligtran + C.Offset(0, 9)
Else
End If
End If
Else
B.Offset(0, 1).Value = tottran
B.Offset(0, 3).Value = opentran
B.Offset(0, 4).Value = openeligtran
B.Offset(0, 2).Value = tottran - opentran
End If
Next
Next
Else
End If
End With


End Sub

--
Robert

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 144
Default Loop works correctly but takes extremely long to execute

Thanks Tom and Nick!! This helps.

--
Robert


"Tom Ogilvy" wrote:

I don't think it will be an issue with this workbook, but
the compromise is to have your code put in the formulas, then replace the
formulas with the values they return

With Range("B2:B281")
.Formula = "=countif('Transaction Summary'!$A$2:$A$3000,A2)"
.Formula = .Value
end With

as an example.

--
Regards,
Tom Ogilvy


"robs3131" wrote:

Thanks Tom and Steve. I was planning on turning off the screen update with
my final code but I know that even with this it wouldn't speed it up.

Tom - I will go ahead and use formulas. It was always my thought that it is
faster to use code, even with intensive loops, than it is to use things such
as vlookups or pivot tables or other formulas in code as it sems that Excel
slows down significantly intermittingly when running such code. IE - it may
run ok today, but tomorrow it will get hung up and show something such as
"Calculating" or "Sort" or "Cells" at the bottom of Excel with bars showing
where Excel is at in that process. Do either of you ever run into this
intermittent issue? Just curious. Thanks.
--
Robert


"Tom Ogilvy" wrote:

your making 280 x 2071 = 579880 loops.

You could easily do this formulas in 'Open Transactions by Member ID'
B2:
=countif('Transaction Summary'!$A$2:$A$3000,A2)
D2:
=Sumproduct(--(A2='Transaction Summary'!$A$2:$A$3000),--('Transaction
Summary'!$M$2:$M$3000="Open"),'Transaction Summary'!$J$2:$J$3000)
E2:
=Sumproduct(--(A2='Transaction Summary'!$A$2:$A$3000),--('Transaction
Summary'!$M$2:$M$3000="Payment Issued"),'Transaction Summary'!$J$2:$J$3000)
C2: =B2-D2
=

and so forth.

If you don't want to put the formulas in the worksheet directly, at least
use them in your code.

--
Regards,
Tom Ogilvy




"robs3131" wrote:

Hi all,

Below is a sub I created to sum up values from a given column similar to
what a Pivot Table would do (from my limited experience, it seems that
refreshing pivot tables using vba causes the code to hang up or take very
long to execute).

The issue is that it simply takes forever for the code to execute -
literally I can see it summing up values in each cell as it executes - so
slow that I can see each value in the cell as it changes. I have written
similar code before that executes much much faster...I was thinking that
maybe I just needed to use the vba code cleaner, but even after using that,
the code executes at the same speed.

I'm wondering if it does make sense to you that it would take extremely long
for this code to execute (there are a total of 280 rows of data in range2 and
2071 records in range3).

Sub bringit()

'Calculate and populate commissions by unique Member ID
With Sheets("Open Transactions by Member ID")
If Len(.Range("A3")) < 0 Then
Set range2 = .Range("A2", .Range("A2").End(xlDown))
Else
End If
End With
With Sheets("Transaction Summary")
If Len(.Range("A3")) < 0 Then
Set range3 = .Range("A2", .Range("A2").End(xlDown))
Else
End If
End With


With Sheets("Open Transactions by Member ID")
If Len(.Range("A2")) < 0 Then
For Each B In range2
tottran = 0
opentran = 0
openeligtran = 0
For Each C In range3
If B = C Then
tottran = tottran + C.Offset(0, 9)
If C.Offset(0, 12).Value = "Open" Then
opentran = opentran + C.Offset(0, 9)
Else
If C.Offset(0, 12).Value = "Payment Issued" Then
openeligtran = openeligtran + C.Offset(0, 9)
Else
End If
End If
Else
B.Offset(0, 1).Value = tottran
B.Offset(0, 3).Value = opentran
B.Offset(0, 4).Value = openeligtran
B.Offset(0, 2).Value = tottran - opentran
End If
Next
Next
Else
End If
End With


End Sub

--
Robert

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
deleting takes too long Howiek1 Excel Worksheet Functions 2 January 17th 11 12:58 PM
VLookup takes too long DTTODGG Excel Worksheet Functions 4 March 20th 08 09:57 PM
Clearing cells takes long, long time unclemuffin Excel Discussion (Misc queries) 9 August 17th 07 02:22 AM
Extremely long SQL Array Diana[_7_] Excel Programming 4 April 19th 07 06:36 PM
PasteSpecial takes too long Ramil[_2_] Excel Programming 0 January 19th 04 05:01 PM


All times are GMT +1. The time now is 05:57 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"