VBA puts formula into dynamic range

helpexcel

Well-known Member
Joined
Oct 21, 2009
Messages
656
Hi,

I have no idea why this won't work. I get an application-defined/object-defined error. I have wsC defined earlier in the code. Range1/2/3 are Defined Name Ranges in the workbook.
Any help is appreciated.

Dim lastrow2, LastColumn As Long
LastColumn = wsC.Cells(2, Columns.Count).End(xlToLeft).Column
lastrow2 = wsC.Cells(Rows.Count, "A").End(xlUp).Row


Range(Cells(3, 26), Cells(lastrow2, LastColumn)).FormulaR1C1 = "=IF(SUMPRODUCT((Range1=Sheet2!$A3)+0,(Range2=Sheet2!Z$2)+0,(Range3))=0,"""",SUMPRODUCT((Range1=Sheet2!$A3)+0,(Range2=Sheet2!AA$2)+0,(Range3))"
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Are Range1, Range2 etc named ranges?

If they are then all you are missing is a closing parentheses right at the end.
 
Upvote 0
Thanks, they are named ranges. I updated the formula and checked it on the worksheet and it works, but it still errors out in the macro.

Range(Cells(3, 26), Cells(lastrow2, LastColumn)).FormulaR1C1 = "=IF(SUMPRODUCT((Range1=Sheet2!$A3)+0,(Range2=Sheet2!Z$2)+0,(Range3))=0,"""",SUMPRODUCT((Range1=Sheet2!$A3)+0,(Range2=Sheet2!Z$2)+0,(Range3)))"
 
Upvote 0
Which sheet are the formulas meant to be going on?

Does this work?
Code:
With wsC
    .Range(.Cells(3, 26), .Cells(lastrow2, LastColumn)).FormulaR1C1 = "=IF(SUMPRODUCT((Range1=Sheet2!$A3)+0,(Range2=Sheet2!Z$2)+0,(Range3))=0,"""",SUMPRODUCT((Range1=Sheet2!$A3)+0,(Range2=Sheet2!AA$2)+0,(Range3)))"
End With
 
Upvote 0
Still errors out.

The formulas are going into Sheet2. I wonder if wsC is messing it up, because I have code to say wsC=Sheet2 (this is the shorthand version).
 
Upvote 0
Oops, didn't notice the R1C1 there. :eek:
 
Upvote 0
any thoughts on how i change the index/match that sums a dynamic range in a column to sum a dynamic range in a row?
 
Upvote 0

Forum statistics

Threads
1,223,099
Messages
6,170,114
Members
452,302
Latest member
TaMere

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