range.value in loop speed issue

bjjc51192

New Member
Joined
Apr 5, 2017
Messages
9
So I created a rate calculator that allows the user to enter answers for questions pertaining to the rater ("r_user_interface").
These answers are linked to a table that has various calculations that gives the final result in the cell called ("v_premium")
The problem is, I now want to set default choices in order to compare the rates through all the zip codes, so what I did was create a transposed version of "r_user_interface" called "r_default_choices".
I then created a table which contains all 1106 zip codes as well as headers which pertain to the questions being asked in the user interface.
The macro then copies "r_default_choices" to the range of the table that contains the zip codes.
I now loop through every row in the table with the zip codes and default choices, and paste it in "r_user_interface" to get its respective "v_premium" value and paste it at the end of the row.

It takes around 45 seconds to complete the process, any thoughts on how I can speed things up?

<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">Sub cop_paste_values()
Dim rng As Range
Dim user_rng As Range
Dim row As Range


On Error Resume Next
Application.screenupdating=False
Worksheets
("Batch rater").Range("r_default_choices").Copy
Worksheets
("Batch rater").Range("t_default_choices").PasteSpecial Paste:=xlPasteValues
Set user_rng = Worksheets("User Interface").Range("r_user_interface")
Set rng = Range("t_default_choices_with_zip")

i
= 23

While i <= 1106
For Each row In rng.Rows
row
.Copy
user_rng
.PasteSpecial Paste:=xlPasteValues, Transpose:=True
Worksheets
("Batch rater").cells(i, 43).Value = Worksheets("Rater").Range("v_premium").Value
i
= i + 1
Next
Wend
application.screenupdating=True
Application
.CutCopyMode = False
End Sub</code>
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
You can describe or put exactly what range of cells you have in each Name Range.
 
Upvote 0
you can speed it up by a factor of about 1000 by using variant arrays.
You haven't specified what you are copying but here is an example of how to copy A1 to D19 on sheet 1 to A1 to S4 on sheet2

Code:
Sub test2()
With Worksheets("sheet1")
inarr = Range(.Cells(1, 1), .Cells(19, 4))
End With
With Worksheets("sheet2")
outarr = Range(.Cells(1, 1), .Cells(4, 19))
For i = 1 To 19
 For j = 1 To 4
  outarr(j, i) = inarr(i, j)
 Next j
Next i


Range(.Cells(1, 1), .Cells(4, 19)) = outarr


End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,264
Members
452,627
Latest member
KitkatToby

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top