copy a formula using VBA

Colleen45

Active Member
Joined
Jul 22, 2007
Messages
495
I would like to copy the formula below using VBA
The formula is in K5, and I would like it copied down to K111
The only change for each line is that 1.xlsm, must change to 2.xlsm then to 3.xlsm etc until 107.xlsm

=SUMPRODUCT(COUNTIF(BR8,'C:\Collection Dev\Stock Register\2010-2011\[1.xlsm]Annual Expenditure'!$AI$15:$AL$1000))
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Try like this

Code:
Sub test()
Range("K5").AutoFill Destination:=Range("K5:K111")
End Sub
 
Upvote 0
Code:
=SUMPRODUCT(COUNTIF(BR8,'C:\Collection Dev\Stock Register\2010-2011\INDIRECT("[" & L5 & ".xlsm]Annual Expenditure'!$AI$15:$AL$1000")))

I USED THE INDIRECT COMMAND TO MAKE THE STRING AN ADDRESS. L5 TO L111 WILL HAVE 1 2 3 .....
 
Upvote 0
I think maybe the point is being missed, the formula must remain as it is, with only the workbook number increasing, ie 1.xlsm, 2.xlsm etc
 
Upvote 0
Try

Code:
Sub test()
Dim i As Long
For i = 5 To 111
    Range("K" & i).Formula = "=SUMPRODUCT(COUNTIF(BR8,'C:\Collection Dev\Stock Register\2010-2011\[" & i - 4 & ".xlsm]Annual Expenditure'!$AI$15:$AL$1000))"
Next i
End Sub
 
Upvote 0
Code:
=SUMPRODUCT(COUNTIF(BR8,'C:\Collection Dev\Stock Register\2010-2011\INDIRECT("[" & L5 & ".xlsm]Annual Expenditure'!$AI$15:$AL$1000")))

I USED THE INDIRECT COMMAND TO MAKE THE STRING AN ADDRESS. L5 TO L111 WILL HAVE 1 2 3 .....


oops...

DOES THIS WORK FOR YOU

Code:
=SUMPRODUCT(COUNTIF(BR8,INDIRECT("'C:\Collection Dev\Stock Register\2010-2011\[" & L5 & ".xlsm]Annual Expenditure'!$AI$15:$AL$1000")))
 
Upvote 0
I would like to do exactly the same with this formula but i'm not having much success

Code:
='C:\Collection Dev\Stock Register\2010-2011\[1.xlsm]Annual Expenditure'!$H$6

all i have at the moment is this but I'm receiving a runtime error

Code:
Range("b" & i).Formula = "=c:\collection dev\stock register\2010-2011\[" & i - 4 & ".xlsm]Annual Expenditure'!$h$6"
 
Upvote 0
I managed to sort it out
Code:
Range("B" & i).Formula = "='[" & i - 4 & ".xlsm]Annual Expenditure'!$h$6"
 
Upvote 0

Forum statistics

Threads
1,224,507
Messages
6,179,176
Members
452,893
Latest member
denay

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