VBA More Efficient Fill Down

Littlemalky

Board Regular
Joined
Jan 14, 2011
Messages
223
Is there anyway to make this more efficient. This is the only way I could get this problem to work: I'm inserting a formula into a cell I can't call upon directly because it's dynamic, and then I'm selecting the cell, copying that cell, and telling it to paste down in the next 23 rows below it.
It seems redundant to me, but I can't figure out anything else.
Code:
Workbooks("" & Format(DateSerial(Year(Date), (Month(Date) - 1), Day(Date)), "yyyy.mm") & " summary.xlsx").Worksheets("Domestic").[A5].End(xlToRight).Offset(0, 1).Formula = _
        "=IFERROR(VLOOKUP(A:A,'[" & Format(DateSerial(Year(Date), (Month(Date) - 1), Day(Date)), "mm-yyyy") & " Domestic.xlsx]Summary'!$A:$D,4,0),0)"
    Workbooks("" & Format(DateSerial(Year(Date), (Month(Date) - 1), Day(Date)), "yyyy.mm") & " summary.xlsx").Worksheets("Domestic").[A5].End(xlToRight).Select
    Workbooks("" & Format(DateSerial(Year(Date), (Month(Date) - 1), Day(Date)), "yyyy.mm") & " summary.xlsx").Worksheets("Domestic").[A5].End(xlToRight).Copy _
        Destination:=Workbooks("" & Format(DateSerial(Year(Date), (Month(Date) - 1), Day(Date)), "yyyy.mm") & " summary.xlsx").Worksheets("Domestic").Range(Selection, Selection.Cells(23))
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
You can put the formula into all the cells in one line...

change
Offset(0, 1).Formula = "...
to
Offset(0, 1).Resize(23,1).Formula = "...

Then you don't need to copy/paste the formula to the rest of the rows.


Also, another simplification is to use the DateAdd Function...

This
DateSerial(Year(Date), (Month(Date) - 1), Day(Date))
Can be written as
DateAdd("m", -1, Date)


Hope that helps.
 
Upvote 0

Forum statistics

Threads
1,224,520
Messages
6,179,270
Members
452,902
Latest member
Knuddeluff

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