Variable used to create average formula

kyddrivers

Board Regular
Joined
Mar 22, 2013
Messages
64
Office Version
  1. 365
Platform
  1. Windows
I am looking to create a macro that will prompt the user for a number. This number will represent the number of columns to used to create the average formula.

Here is where I am at so far...

Code:
Sub ave_test()


Dim averange As Integer
showavebox:
    averange = InputBox("Enter the number of weeks to calculate pattern and click OK", "Thanks")
    
    Range("A3").Select
    ActiveCell.FormulaR1C1 = "=AVERAGE(R[-2]C[2]:R[-2]C[averange])"
    Range("A4").Select
End Sub

So far, I am getting an application defined or object defined error on the formula line.

Boils down to this...need to get the formula to read the user entered variable in averange to inserted into the formula

Let me know if you have any questions.

Thanks for your help in advance.
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Ok I think I have figured it out...

Code:
Sub ave_test()
'
' ave_test Macro
'


'
Dim averange As Integer
showavebox:
    averange = InputBox("Enter the number of weeks to calculate pattern and click OK", "Thank You")
    
    Range("A3").Select
    ActiveCell.FormulaR1C1 = "=AVERAGE(R[-2]C[2]:R[-2]C[" & averange + 1 & "])"
    Range("A4").Select
End Sub
 
Upvote 0
I am looking to create a macro that will prompt the user for a number. This number will represent the number of columns to used to create the average formula.

Here is where I am at so far...

Code:
Sub ave_test()


Dim averange As Integer
showavebox:
    averange = InputBox("Enter the number of weeks to calculate pattern and click OK", "Thanks")
    
    Range("A3").Select
    [COLOR=#333333]ActiveCell.FormulaR1C1 = "=AVERAGE(R[-2]C[2]:R[-2]C[" & averange & "])"[/COLOR]
    Range("A4").Select
End Sub

Need to take this a step further.

What I am hoping to accomplish is take the first column reference (in this case 2) and add the user defined amount to create the 2nd column in the formula. See in orange below or something to that effect.

example:
Rich (BB code):
ActiveCell.FormulaR1C1 = "=AVERAGE(R[-2]C[2]:R[-2]C[2 + " & averange & "])"

Let me know if you have questions.

Thanks again for your help in advance!!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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