Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default slow for loops ... better way using arrays or something?

I am (still) dealing with slow running code.

The following use "for" loops to populate cells. The first example needs
add headers to thrtee sheets based on what exists on another sheet. The
second example needs to add a formula to each row in column A that contains
values in column B.

Could someone suggest a better way to get these jobs done? Arrays?

******************
1.
For index1 = 1 To num_headers 'num headers is variable
header_val = Worksheets("sheet1").Cells(1, index1).Value
Worksheets("sheet2").Cells(1, index1).Value = header_val
Worksheets("sheet3").Cells(1, index1 + 1).Value = header_val
Worksheets("sheet4").Cells(1, index1 + 1).Value = header_val
Next

2.
For index1 = 1 To num_month_records 'variable
Worksheets("sheet1").Cells(index1 + 1, 1).FormulaR1C1 =
"=CONCATENATE(RC3,""_"",RC4)"
Next



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default slow for loops ... better way using arrays or something?

Dim header_val as Variant
header_val = Worksheets("sheet1").Cells(1,
1).Resize(1,num_headers).Value
Worksheets("sheet2").Cells(1, 1).Resize(1,num_headers).Value =
header_val
Worksheets("sheet3").Cells(1, 1).Resize(1,num_headers).Value =
header_val
Worksheets("sheet4").Cells(1, 1).Resize(1,num_headers).Value =
header_val




Worksheets("sheet1").Cells(2, 1).Resize( _
num_month_records,1).FormulaR1C1 =
"=CONCATENATE(RC3,""_"",RC4)"

--
Regards,
Tom Ogilvy

"wdeleo" wrote in message
...
I am (still) dealing with slow running code.

The following use "for" loops to populate cells. The first example needs
add headers to thrtee sheets based on what exists on another sheet. The
second example needs to add a formula to each row in column A that
contains
values in column B.

Could someone suggest a better way to get these jobs done? Arrays?

******************
1.
For index1 = 1 To num_headers 'num headers is variable
header_val = Worksheets("sheet1").Cells(1, index1).Value
Worksheets("sheet2").Cells(1, index1).Value = header_val
Worksheets("sheet3").Cells(1, index1 + 1).Value = header_val
Worksheets("sheet4").Cells(1, index1 + 1).Value = header_val
Next

2.
For index1 = 1 To num_month_records 'variable
Worksheets("sheet1").Cells(index1 + 1, 1).FormulaR1C1 =
"=CONCATENATE(RC3,""_"",RC4)"
Next





  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default slow for loops ... better way using arrays or something?

Well ... THAT's better. It was taking several minutes and now it takes about
30 seconds. Still unsatisfactory, but at least now I can run it and disect
it at a reasonable pace. I think there is still something that needs to be
fixed as well as the parts that could use improvement.

Thanks soooo much Tom (again)
Billy


"Tom Ogilvy" wrote:

Dim header_val as Variant
header_val = Worksheets("sheet1").Cells(1,
1).Resize(1,num_headers).Value
Worksheets("sheet2").Cells(1, 1).Resize(1,num_headers).Value =
header_val
Worksheets("sheet3").Cells(1, 1).Resize(1,num_headers).Value =
header_val
Worksheets("sheet4").Cells(1, 1).Resize(1,num_headers).Value =
header_val




Worksheets("sheet1").Cells(2, 1).Resize( _
num_month_records,1).FormulaR1C1 =
"=CONCATENATE(RC3,""_"",RC4)"

--
Regards,
Tom Ogilvy

"wdeleo" wrote in message
...
I am (still) dealing with slow running code.

The following use "for" loops to populate cells. The first example needs
add headers to thrtee sheets based on what exists on another sheet. The
second example needs to add a formula to each row in column A that
contains
values in column B.

Could someone suggest a better way to get these jobs done? Arrays?

******************
1.
For index1 = 1 To num_headers 'num headers is variable
header_val = Worksheets("sheet1").Cells(1, index1).Value
Worksheets("sheet2").Cells(1, index1).Value = header_val
Worksheets("sheet3").Cells(1, index1 + 1).Value = header_val
Worksheets("sheet4").Cells(1, index1 + 1).Value = header_val
Next

2.
For index1 = 1 To num_month_records 'variable
Worksheets("sheet1").Cells(index1 + 1, 1).FormulaR1C1 =
"=CONCATENATE(RC3,""_"",RC4)"
Next






  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default slow for loops ... better way using arrays or something?

Running this code on 60000 rows takes less than 1 second.
So the answer lies elsewhere with some other code or maybe .Calculation.

NickHK

"wdeleo" wrote in message
...
Well ... THAT's better. It was taking several minutes and now it takes

about
30 seconds. Still unsatisfactory, but at least now I can run it and

disect
it at a reasonable pace. I think there is still something that needs to

be
fixed as well as the parts that could use improvement.

Thanks soooo much Tom (again)
Billy


"Tom Ogilvy" wrote:

Dim header_val as Variant
header_val = Worksheets("sheet1").Cells(1,
1).Resize(1,num_headers).Value
Worksheets("sheet2").Cells(1, 1).Resize(1,num_headers).Value =
header_val
Worksheets("sheet3").Cells(1, 1).Resize(1,num_headers).Value =
header_val
Worksheets("sheet4").Cells(1, 1).Resize(1,num_headers).Value =
header_val




Worksheets("sheet1").Cells(2, 1).Resize( _
num_month_records,1).FormulaR1C1 =
"=CONCATENATE(RC3,""_"",RC4)"

--
Regards,
Tom Ogilvy

"wdeleo" wrote in message
...
I am (still) dealing with slow running code.

The following use "for" loops to populate cells. The first example

needs
add headers to thrtee sheets based on what exists on another sheet.

The
second example needs to add a formula to each row in column A that
contains
values in column B.

Could someone suggest a better way to get these jobs done? Arrays?

******************
1.
For index1 = 1 To num_headers 'num headers is variable
header_val = Worksheets("sheet1").Cells(1, index1).Value
Worksheets("sheet2").Cells(1, index1).Value = header_val
Worksheets("sheet3").Cells(1, index1 + 1).Value = header_val
Worksheets("sheet4").Cells(1, index1 + 1).Value = header_val
Next

2.
For index1 = 1 To num_month_records 'variable
Worksheets("sheet1").Cells(index1 + 1, 1).FormulaR1C1 =
"=CONCATENATE(RC3,""_"",RC4)"
Next








  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default slow for loops ... better way using arrays or something?

Just to expand, Nick is suggesting you might try

Application.Calculcation = xlManual

' current code

' optionally change it back to automatic
Application.Calculation = xlAutomatic

--
Regards,
Tom Ogilvy


"NickHK" wrote:

Running this code on 60000 rows takes less than 1 second.
So the answer lies elsewhere with some other code or maybe .Calculation.

NickHK

"wdeleo" wrote in message
...
Well ... THAT's better. It was taking several minutes and now it takes

about
30 seconds. Still unsatisfactory, but at least now I can run it and

disect
it at a reasonable pace. I think there is still something that needs to

be
fixed as well as the parts that could use improvement.

Thanks soooo much Tom (again)
Billy


"Tom Ogilvy" wrote:

Dim header_val as Variant
header_val = Worksheets("sheet1").Cells(1,
1).Resize(1,num_headers).Value
Worksheets("sheet2").Cells(1, 1).Resize(1,num_headers).Value =
header_val
Worksheets("sheet3").Cells(1, 1).Resize(1,num_headers).Value =
header_val
Worksheets("sheet4").Cells(1, 1).Resize(1,num_headers).Value =
header_val




Worksheets("sheet1").Cells(2, 1).Resize( _
num_month_records,1).FormulaR1C1 =
"=CONCATENATE(RC3,""_"",RC4)"

--
Regards,
Tom Ogilvy

"wdeleo" wrote in message
...
I am (still) dealing with slow running code.

The following use "for" loops to populate cells. The first example

needs
add headers to thrtee sheets based on what exists on another sheet.

The
second example needs to add a formula to each row in column A that
contains
values in column B.

Could someone suggest a better way to get these jobs done? Arrays?

******************
1.
For index1 = 1 To num_headers 'num headers is variable
header_val = Worksheets("sheet1").Cells(1, index1).Value
Worksheets("sheet2").Cells(1, index1).Value = header_val
Worksheets("sheet3").Cells(1, index1 + 1).Value = header_val
Worksheets("sheet4").Cells(1, index1 + 1).Value = header_val
Next

2.
For index1 = 1 To num_month_records 'variable
Worksheets("sheet1").Cells(index1 + 1, 1).FormulaR1C1 =
"=CONCATENATE(RC3,""_"",RC4)"
Next









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
VBA excel using arrays and loops [email protected] Excel Programming 1 July 7th 06 01:48 AM
Formula Arrays VERY SLOW in Excel 2002 Patrick Excel Worksheet Functions 2 January 27th 05 12:59 AM
Scope of the arrays in Loops itsmaheshp[_9_] Excel Programming 1 November 15th 04 12:51 PM
Arrays to replace very slow loops ? vbastarter Excel Programming 5 August 10th 04 07:15 PM
Excel 2000 Slow Loops scain2004 Excel Programming 7 April 4th 04 02:35 AM


All times are GMT +1. The time now is 05:50 PM.

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"