VBA average selection on other sheet

teatimecrumpet

Active Member
Joined
Jun 23, 2010
Messages
307
Hi,

I'm having trouble averaging a selection from another sheet and placing the answer in a different sheet. I want to set the selection of the range to be a variable name and then have the app take that range to run the calculation on. But my code will just return a zero. The code does work if I change meanrange to selection.



Sorry for the code I'm certainly going beyond my reach.

Sub datrang()
Dim foundOne As Range
Dim meanrange As Range
Dim answer As Integer
Dim startdate As String

On Error Resume Next
With ActiveWorkbook.Sheets("data")
Set foundOne = .Range("A:A").Find(What:=(startdate), after:=.Range("a1"), LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False)
If foundOne.Row > 1 Then
foundOne.Offset(0, 1).Select
Range(Selection, Selection.End(xlDown)).Select
'Range(foundOne, foundOne.End(xlDown)).Select
'Range(.Range("a1"), foundOne.Offset(-1, 0)).EntireRow.Delete shift:=xlUp
Set meanrange = Sheets("data").Range(Selection)
End If
End With

Sheets("calculations").Range("D2").Value = Application.WorksheetFunction.Average(meanrange)

On Error GoTo 0

End Sub

Thanks,
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

Forum statistics

Threads
1,224,607
Messages
6,179,871
Members
452,949
Latest member
Dupuhini

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