vba help - formula fill down

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
976
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

I am using below code to fill down existing formula,
its very lenghty code can we shorten below code, by using any alternate Way


VBA Code:
With ws_Master

    With .Range(.Cells(2, 4), .Cells(lr_new, 4))
        .FillDown
    End With

    With .Range(.Cells(2, 5.Cells(lr_new, 5))
        .FillDown
    End With

    
    With .Range(.Cells(2, 7), .Cells(lr_new, 7))
        .FillDown
    End With

    
    With .Range(.Cells(2, 9), .Cells(lr_new, 9))
        .FillDown
    End With


    With .Range(.Cells(2, 11), .Cells(lr_new, 11))
        .FillDown
    End With

    With .Range(.Cells(2, 14), .Cells(lr_new, 14))
        .FillDown
    End With

    With .Range(.Cells(2, 15), .Cells(lr_new, 15))
        .FillDown
    End With

    With .Range(.Cells(2, 18), .Cells(lr_new, 18))
        .FillDown
    End With


    With .Range(.Cells(2, 20), .Cells(lr_new, 20))
        .FillDown
    End With

end with


Thanks
mg
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
How about
VBA Code:
With ws_Master
   .Range("D2:E" & lr_new).FillDown
   .Range("G2:G" & lr_new).FillDown
   .Range("I2:I" & lr_new).FillDown
   .Range("K2:K" & lr_new).FillDown
   .Range("N2:O" & lr_new).FillDown
   .Range("R2:R" & lr_new).FillDown
   .Range("T2:T" & lr_new).FillDown
End With
 
Upvote 0
Hi Fluff,

Thanks for your help, this also help in reducing some code length.
is it possible to use loop here, as I have to work on 15 Columns.



Thanks
mg
 
Upvote 0
How about
VBA Code:
Sub Mallesh()
   Dim Ary As Variant
   Dim i As Long, lr_new As Long
   
   Ary = Array("D2:E", "G2:G", "I2:I", "K2:K", "N2:O")
   With ws_master
      For i = 0 To UBound(Ary)
         .Range(Ary(i) & lr_new).FillDown
      Next i
   End With
   
End Sub
 
Upvote 0
Hi Fluff,

Superb ! Thank you so much for you help, (y)
I have used match function to find header, it may change,

Nice code it worked in both situation ! Thanks


VBA Code:
Arys = Array(2, 4, 6 etc)
   
   With ws_Master
      For i = 0 To UBound(Arys)
        .Range(.Cells(2, Arys(i)), .Cells(lr_new, Arys(i))).FillDown
      Next i
   End With

Thanks
mg
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,891
Messages
6,122,105
Members
449,066
Latest member
Andyg666

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