0
votes

This is my first post on Stack Overflow.. I've used this site multiple times with my VBA questions and most of the time, I've been able to find the answer. This time, however, I'm finding that I can't locate anything that helps me out. I've tried to figure out this problem from multiple angles and I can't seem to figure it out. I've gotten this SUMIFS code to work correctly using a single Workbook, but two presents a problem. The two sheets are "ActiveHedge.xlsm" and "LiveDataFeed.xlsm" and here is my code:

Sub Call_LiveDataFeed()
Workbooks.Open Filename:="Z:\Users\toms\Desktop\LiveDataFeed.xlsm"
End Sub
Sub CreateFNMA_MonthlyCoupons()
Workbooks("LiveDataFeed.xlsm").Activate
Range("D7").Formula = _
    "=SUMIFS('[ActiveHedge.xlsm]Active Hedge'!$I:$I,'[ActiveHedge.xlsm]Active Hedge'!$H:$H,">="&U9,'[ActiveHedge.xlsm]Active Hedge'!$I:$I,"<="&V9,'[ActiveHedge.xlsm]Active Hedge'!$K:$K,"<"&C5-14)"

So, I used & before the Cell references in the criteria range due to the first two criteria being decimals that will change on the "LiveDataFeed.xlsm" sheet and the final criteria is the date entered in the cell, minus 14 days; the inputted date will change as well.

I found that the SUMIFS function, as entered above, works perfectly fine as a function entered directly into a worksheet in the "LiveDataSheet.xlsm" workbook. However, when put in to VBA, it has a Run-Time Error "13"... My solution to this for using one workbook, was simply to put an extra " " around my >= , <= and <... This isn't working now... Any help would be much appreciated!

1

1 Answers

0
votes

When you have a formula that contains quotes in VBA you need to double them up so they aren't confused with the quote mark at the end of the formula, i.e.

Range("D7").Formula = _
    "=SUMIFS('[ActiveHedge.xlsm]Active Hedge'!$I:$I,'[ActiveHedge.xlsm]Active Hedge'!$H:$H,"">=""&U9,'[ActiveHedge.xlsm]Active Hedge'!$I:$I,""<=""&V9,'[ActiveHedge.xlsm]Active Hedge'!$K:$K,""<""&C5-14)"