Eliminate redundancy in Excel VBA Code

The code below obviously works, but I'm wondering if there is perhaps a more efficient way that it can be written?  Thanks!
Public Sub ApplyFillFormat()
Dim rngAutoFill1, rngAutoFill2, rngAutoFormat As Range
Set rngAutoFill1 = Range("C14:H14")
Set rngAutoFill2 = Range("K14:S14")
Set rngAutoFormat = Range("I14")

Range(rngAutoFill1, rngAutoFill1.End(xlDown)).FillDown
Range(rngAutoFill2, rngAutoFill2.End(xlDown)).FillDown

rngAutoFormat.Copy
Range(rngAutoFormat, rngAutoFormat.End(xlDown)).PasteSpecial (xlPasteFormats)

End Sub

Open in new window

KP_SoCalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Patrick MatthewsCommented:
KP_SoCal,

If you a large number of ranges to apply that to, then you might want to try looping through an array, but as it is your code is fine, with the exception of:

Dim rngAutoFill1, rngAutoFill2, rngAutoFormat As Range


When you do that, rngAutoFill1 and rngAutoFill2 are actually being declared as Variant, and not as Range.  Instead, use:

Dim rngAutoFill1 As Range, rngAutoFill2 As Range, rngAutoFormat As Range

Patrick
0
KP_SoCalAuthor Commented:
Patrick, thanks for the declaration tip!  Would you be able to give me an example of how an array would look if applied to my code?  Just curious in case I ever need it in the future.  Thanks so much! =)
0
sir plusSales ManagementCommented:
Hi
Your current code is fine for 2 areas abut for 10 it will get messy
to loop etc you need to use variant

I think this is what you are after

PS Best practice is to define 1 per line for readability, not sure I agree but thats what I do.


Public Sub ApplyFillFormat()
Dim rngAutoFill As Variant
Dim r As Variant
Dim rngAutoFormat As Range

Let rngAutoFill = Array("C14:H14", "K14:S14")
Set rngAutoFormat = Range("I14")
 

For r = LBound(rngAutoFill) To UBound(rngAutoFill)
    Range(Range(rngAutoFill(r)), Range(rngAutoFill(r)).End(xlDown)).FillDown
Next r
 
rngAutoFormat.Copy
Range(rngAutoFormat, rngAutoFormat.End(xlDown)).PasteSpecial (xlPasteFormats)

End Sub

Open in new window

0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

sir plusSales ManagementCommented:
Or even better

Public Sub ApplyFillFormat()
Dim rngAutoFill As Variant
Dim r As Variant
Dim rngAutoFormat As Range
 
Let rngAutoFill = Array("C14:H14", "K14:S14")
Set rngAutoFormat = Range("I14")
    
For Each r In rngAutoFill
    Range(Range(r), Range(r).End(xlDown)).FillDown
Next r

rngAutoFormat.Copy
Range(rngAutoFormat, rngAutoFormat.End(xlDown)).PasteSpecial (xlPasteFormats)
 
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KP_SoCalAuthor Commented:
Thanks guys for the feedback on this!  I'm going to split the points up.
0
sir plusSales ManagementCommented:
Thaks for the recognition
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.