VBA - Is It Possible to Make Code Run Faster With Array - Tell Me How

Myproblem

Board Regular
Joined
May 24, 2010
Messages
198
I have vba code down, which works fine, but I need to run more faster.
I know that some of the ways to make code to run faster make some line with Array.
I was wondering how to write line in red te be written in Array.
any idea, how?
thx

Sub CopyWithHlookup()<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
Application.ScreenUpdating = False<o:p></o:p>
Application.Calculation = xlCalculationManual<o:p></o:p>
Range("C5") = "=HLookup(B$1, MyData, $AD5, False)"<o:p></o:p>
<o:p> </o:p>
Range("C5").Copy<o:p></o:p>
<o:p> </o:p>
Range("C6:C25").PasteSpecial Paste:=xlPasteFormulas<o:p></o:p>
<o:p> </o:p>
Range("C5:C25").Copy<o:p></o:p>
Range("E5:E25").PasteSpecial Paste:=xlPasteFormulas<o:p></o:p>
Range("G5:G25").PasteSpecial Paste:=xlPasteFormulas<o:p></o:p>
Range("I5:I25").PasteSpecial Paste:=xlPasteFormulas<o:p></o:p>
Range("K5:K25").PasteSpecial Paste:=xlPasteFormulas<o:p></o:p>
Range("M5:M25").PasteSpecial Paste:=xlPasteFormulas<o:p></o:p>
Range("O5:O25").PasteSpecial Paste:=xlPasteFormulas<o:p></o:p>
Range("Q5:Q25").PasteSpecial Paste:=xlPasteFormulas<o:p></o:p>
Range("S5:S25").PasteSpecial Paste:=xlPasteFormulas<o:p></o:p>
Application.Calculation = xlCalculationAutomatic<o:p></o:p>
<o:p> </o:p>
Range("C5:C25").Copy<o:p></o:p>
Range("C5:C25").PasteSpecial Paste:=xlPasteValues<o:p></o:p>
<o:p> </o:p>
Range("E5:E25").Copy<o:p></o:p>
Range("E5:E25").PasteSpecial Paste:=xlPasteValues<o:p></o:p>
<o:p> </o:p>
Range("G5:G25").Copy<o:p></o:p>
Range("G5:G25").PasteSpecial Paste:=xlPasteValues<o:p></o:p>
<o:p> </o:p>
Range("I5:I25").Copy<o:p></o:p>
Range("I5:I25").PasteSpecial Paste:=xlPasteValues<o:p></o:p>
<o:p> </o:p>
Range("K5:K25").Copy<o:p></o:p>
Range("K5:K25").PasteSpecial Paste:=xlPasteValues<o:p></o:p>
<o:p> </o:p>
Range("M5:M25").Copy<o:p></o:p>
Range("M5:M25").PasteSpecial Paste:=xlPasteValues<o:p></o:p>
<o:p> </o:p>
Range("O5:O25").Copy<o:p></o:p>
Range("O5:O25").PasteSpecial Paste:=xlPasteValues<o:p></o:p>
<o:p> </o:p>
Range("Q5:Q25").Copy<o:p></o:p>
Range("Q5:Q25").PasteSpecial Paste:=xlPasteValues<o:p></o:p>
<o:p> </o:p>
Range("S5:S25").Copy<o:p></o:p>
Range("S5:S25").PasteSpecial Paste:=xlPasteValues<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
I'm not sure the array makes thei faster, but it certainly makes it shorter:
Code:
Option Explicit

Sub CopyWithHlookup()
Dim Arr As Variant, a As Variant

Application.ScreenUpdating = False
Arr = Array("C5:C25", "E5:E25", "G5:G25", "I5:I25", "K5:K25", _
            "M5:M25", "O5:O25", "Q5:Q25", "S5:S25")

    For Each a In Arr
        With Range(a)
            .FormulaR1C1 = "=HLookup(R1C[-1], MyData, RC30, False)"
            .Value = .Value
        End With
    Next a
    
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,552
Messages
6,179,484
Members
452,917
Latest member
MrsMSalt

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