How can I put this array formula using VBA?

asad

Well-known Member
Joined
Sep 9, 2008
Messages
1,434
Hello Guys,

I got the following array formula given to me on this forum:
Code:
{=IF(A7 < > A$5,"",$A$5&" "&TEXT(MIN(IF('O:\Operations Supervisor\`Rosters 2011\[Leave  allocation 2011 Master.xls]Leave Approved'!$I$15:$I$215=$E7,IF('O:\Operations Supervisor\`Rosters 2011\[Leave  allocation 2011 Master.xls]Leave Approved'!$I$4:$FQ$4 > =$BC$1,IF('O:\Operations Supervisor\`Rosters 2011\[Leave  allocation 2011 Master.xls]Leave Approved'!$I$15:$FQ$215="",'O:\Operations Supervisor\`Rosters 2011\[Leave  allocation 2011 Master.xls]Leave Approved'!$I$4:$FQ$4-7)))),"dd/mm/yyyy"))}

It is working fine.

I want to put this formula in the ranges G8:G37, G39:G63, G67:G72 and so on. There are 8 ranges like this all in column G. Is there a way to use VBA to do this for me? The reason for this is that I got another macro that clears column G and then I am left with no formulas in there. I will add this extra bit of VBA to the existing macro so that I don't have to fill this formula in manually every time I run the original macro.

Thanks for your help.
Asad
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
I tried following code, but is not working.
Code:
[COLOR=black][B]Sub AsadFormula()[/B][/COLOR]
[COLOR=red]Range("G8:G10").FormulaR1C1 = "IF(RC[-6]<>"A/L","","A/L "&TEXT(MIN(IF('F:\ALI\MrExcel\Footscray Roster\[Leave  allocation 2011 Master.xls]Leave Approved'!$I$15:$I$215=RC[-2],IF('F:\ALI\MrExcel\Footscray Roster\[Leave  allocation 2011 Master.xls]Leave Approved'!$I$4:$FQ$4 > =R[1]C[55],IF('F:\ALI\MrExcel\Footscray Roster\[Leave  allocation 2011 Master.xls]Leave Approved'!$I$15:$FQ$215="",'F:\ALI\MrExcel\Footscray Roster\[Leave  allocation 2011 Master.xls]Leave Approved'!$I$4:$FQ$4-7)))),"dd/mm/yyyy"))"
[/COLOR]End Sub
The part on top "Sub AsadFormula()" gets highligghted in yellow and the formula goes to red colour.
What is wrong with this formula?
Any help?

Asad
 
Upvote 0
Now I changed it to
Code:
Sub AsadFormula()
Range("G8:G10").FormulaArray = "=IF(RC[-6]<>""A/L"","",""A/L ""&TEXT(MIN(IF('F:\ALI\MrExcel\Footscray Roster\[Leave  allocation 2011 Master.xls]Leave Approved'!$I$15:$I$215=RC[-2],IF('F:\ALI\MrExcel\Footscray Roster\[Leave  allocation 2011 Master.xls]Leave Approved'!$I$4:$FQ$4>=R[1]C[55],IF('F:\ALI\MrExcel\Footscray Roster\[Leave  allocation 2011 Master.xls]Leave Approved'!$I$15:$FQ$215="",'F:\ALI\MrExcel\Footscray Roster\[Leave  allocation 2011 Master.xls]Leave Approved'!$I$4:$FQ$4-7)))),""dd/mm/yyyy""))"
End Sub
Still is not working. Now, just the formula part gets highlighted in yellow. Any ideas please.

Asad
 
Upvote 0
Try...

Code:
[font=Verdana]Range("G8").FormulaArray = "=IF(RC[-6]<>""A/L"","""",""A/L ""&TEXT(MIN(IF(R15C9:R215C9=RC[-2],IF(R4C9:R4C173>=R[1]C[55],IF(R15C9:R215C173="""",R4C9:R4C173-7)))),""dd/mm/yyyy""))"

Range("G8").Copy

Range("G9:G10").PasteSpecial xlPasteFormulas
    [/font]

For multiple ranges, replace...

Code:
[font=Verdana]Range("G9:G10").PasteSpecial xlPasteFormulas[/font]

with

Code:
[font=Verdana]Range("G9:G37,G39:G63,G67:G72").PasteSpecial xlPasteFormulas
    [/font]
 
Upvote 0
Try...

Code:
[FONT=Verdana]Range("G8").FormulaArray = "=IF(RC[-6]<>""A/L"","""",""A/L ""&TEXT(MIN(IF(R15C9:R215C9=RC[-2],IF(R4C9:R4C173>=R[1]C[55],IF(R15C9:R215C173="""",R4C9:R4C173-7)))),""dd/mm/yyyy""))"[/FONT]
 
[/QUOTE]
 
But how will it look at file [QUOTE]'F:\ALI\MrExcel\Footscray Roster\[Leave  allocation 2011 Master.xls]Leave Approved'!$I$15:$I$215[/QUOTE]
 
The code that you gave will not pick the file from the location I showed above, will it?
 
Asad
 
Upvote 0
Add the appropriate path/workbook name/sheet name...
 
Upvote 0
I tried and it gave me an error. The code is
Code:
Sub AsadFormula()
Range("G8").FormulaArray = "=IF(RC[-6] < > ""A/L"","""",""A/L ""&TEXT(MIN(IF('F:\ALI\MrExcel\Footscray Roster\[Leave  allocation 2011 Master.xls]Leave Approved'!R15C9:R215C9=RC[-2],IF('F:\ALI\MrExcel\Footscray Roster\[Leave  allocation 2011 Master.xls]Leave Approved'!R4C9:R4C173 > =R[1]C[55],IF('F:\ALI\MrExcel\Footscray Roster\[Leave  allocation 2011 Master.xls]Leave Approved'!R15C9:R215C173="""",R4C9:R4C173-7)))),""dd/mm/yyyy""))"
Range("G8").Copy
Range("G9:G10").PasteSpecial xlPasteFormulas
    
End Sub

The line with the formula gets highlighted in yellow.
What is it taht I am doing wrong? Can't figure it myself. Your help is much appreciated.

Asad
 
Upvote 0
I tried giving name to path as "mypath" and inserted it in the formula, but same result.
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,834
Members
452,947
Latest member
Gerry_F

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