I want to speed(clean) up my code with loops

dcoker

New Member
Joined
Dec 13, 2018
Messages
36
I have 16 different blocks of code that have the same structure as below. Is there a way to speed this up and make it cleaner with loops? Thanks :)

ActiveSheet.Range("D8") = reg1
ActiveSheet.Range("D9") = reg2
ActiveSheet.Range("D10") = reg3
ActiveSheet.Range("D11") = reg4
ActiveSheet.Range("D12") = reg5
ActiveSheet.Range("D13") = reg6
ActiveSheet.Range("D14") = reg7
ActiveSheet.Range("D15") = reg8
ActiveSheet.Range("D16") = reg9
ActiveSheet.Range("D17") = reg10
ActiveSheet.Range("D18") = reg11
ActiveSheet.Range("D19") = reg12
ActiveSheet.Range("D20") = reg13
ActiveSheet.Range("D21") = reg14
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
What are reg1, reg2 etc.?

By they way, using a loop wouldn't necessarily speed this code up - you would still be doing the same thing the same no of times.

What might speed things up would be to use an array.
Code:
ActiveSheet.Range("D8:D21") = Application.Transpose(Array(reg1, reg2, reg3, reg4, reg5, reg6, reg7, reg8, reg9, reg10, reg11, reg12, reg13, reg14))

Whether that's practical kind of depends what reg1, reg2 etc. are.
 
Upvote 0
That works great!! Thank you!!!!!!

Is there any way to do the same for this?? Thanks!

wcom1 = ActiveCell.Offset(2, 15).Text
wcom2 = ActiveCell.Offset(3, 15).Text
wcom3 = ActiveCell.Offset(4, 15).Text
wcom4 = ActiveCell.Offset(5, 15).Text
wcom5 = ActiveCell.Offset(6, 15).Text
wcom6 = ActiveCell.Offset(7, 15).Text
wcom7 = ActiveCell.Offset(8, 15).Text
wcom8 = ActiveCell.Offset(9, 15).Text
wcom9 = ActiveCell.Offset(10, 15).Text
wcom10 = ActiveCell.Offset(11, 15).Text
wcom11 = ActiveCell.Offset(12, 15).Text
wcom12 = ActiveCell.Offset(13, 15).Text
wcom13 = ActiveCell.Offset(14, 15).Text
wcom14 = ActiveCell.Offset(15, 15).Text
 
Upvote 0
That works great!! Thank you!!!!!!

Is there any way to do the same for this?? Thanks!

wcom1 = ActiveCell.Offset(2, 15).Text
wcom2 = ActiveCell.Offset(3, 15).Text
wcom3 = ActiveCell.Offset(4, 15).Text
wcom4 = ActiveCell.Offset(5, 15).Text
wcom5 = ActiveCell.Offset(6, 15).Text
wcom6 = ActiveCell.Offset(7, 15).Text
wcom7 = ActiveCell.Offset(8, 15).Text
wcom8 = ActiveCell.Offset(9, 15).Text
wcom9 = ActiveCell.Offset(10, 15).Text
wcom10 = ActiveCell.Offset(11, 15).Text
wcom11 = ActiveCell.Offset(12, 15).Text
wcom12 = ActiveCell.Offset(13, 15).Text
wcom13 = ActiveCell.Offset(14, 15).Text
wcom14 = ActiveCell.Offset(15, 15).Text

If wcom1, wcom2, etc. are simple string variables, then the answer is no. However, if that is the case, you should look into how to use arrays as that would allow you to simplify the code quite easily.
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,691
Members
448,978
Latest member
rrauni

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