Macro writes SUMIF formula to a cell, Range and Sum_range need to be variable

excells

New Member
Joined
Feb 6, 2014
Messages
29
Hi all,
The following line of code writes as SUMIF formula into cell E1:

Code:
Range("e1").Value = "=SUMIF(b1:b10,""Materials"",Y1:Y10)"

Range for the SUMIF formula always starts on b1 but the amount of rows needs to be a variable
Sum_range also starts on Y1 but the number of rows needs to be variable as well

Above I used b1:b10 and Y1:Y10 just to force it to work. My question is, how do I get the code to write the formula on a cell with RANGE and sum_range being variables

I tried creating the RANGE using variables, as follows:

Code:
Set varRangeOfItems1 = ActiveSheet.Range("b1", ActiveSheet.Range("b1").End(xlDown))
Range("e100").Value = "=SUMIF(varRangeOfItems1,""Materials"",Y1:Y10)"

The result I got written on the cell e100 was:
=SUMIF(varRangeOfItems1,"=Materials",Y1:Y10)
which gives the error: #NAME? obviously
what I needed written on the cell was: =SUMIF(b1:b10,"=Materials",Y1:Y10), where b1:b10 came from a variable.

Is it possible to achieve what I'm trying to do?
Can somebody shed some light on this?

Thanks in advance
Regards,
Juan
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Try
Code:
Dim lr As Long
lr = ActiveSheet.Range("b" & Rows.Count).End(xlUp).Row
Range("e1").Formula = "=SUMIF(b1:b" & lr & ",""Materials"",Y1:Y" & lr & ")"
 
Upvote 0
Hi Juan

How is the variable varRangeOfItems1 dimensioned and initialised?

Hi pgc01 ,
Thanks for your reply.

I dimensioned it as follows:

Dim varRangeOfItems1 As Variant

I thought it was initialised by means of the expression:


Set varRangeOfItems1 = ActiveSheet.Range("b1", ActiveSheet.Range("b1").End(xlDown))
What are your thoughts?

Regards,
Juan
 
Upvote 0
Hi Jonmo1,
Thanks so much for your assistance. It worked perfectly!
have a lovely day.

Regards,
Juan
 
Upvote 0
Hi Juan

Just as a follow-up, using your code:

Code:
Dim varRangeOfItems1 As Range

Set varRangeOfItems1 = ActiveSheet.Range("b1", ActiveSheet.Range("b1").End(xlDown))
Range("e100").Formula = "=SUMIF(" & varRangeOfItems1.Address(False, False) & ",""Materials"",Y1:Y10)"
 
Upvote 0
Hi Pgc01,
Works great. it opened my eyes to a property entirely new to me (.Address). Thanks a lot for following through.

cheers
Juan
 
Upvote 0

Forum statistics

Threads
1,214,994
Messages
6,122,633
Members
449,092
Latest member
bsb1122

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