VBA: Calculate average then copy to another excel

yccyccycc

New Member
Joined
Aug 4, 2011
Messages
23
I have few data that I need to calculate average:

A B C
9 3 6
5 2 4
3 6 8

I need to calculate the average of column A/B/C --> Average (A2:C2) and Average (A3:C3) and Average (A4:C4).
The end result should be pasted to another workbook from A1:A3

I would like to use VBA since I have written some VBA to other parts of the report....Please advise. Thanks!
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.

GopalaKrishnaJ

Board Regular
Joined
Dec 19, 2011
Messages
75
Try this

Dim wbkSource As Workbook
Dim wbkDest As Workbook
Dim rngData As Range
Dim intItem As Integer

Set wbkSource = ThisWorkbook
Set wbkDest = Workbooks.Add

Set rngData = wbkSource.Worksheets("Sheet1").Range("A1").CurrentRegion
Set rngData = rngData.Resize(, rngData.Columns.Count + 1)


rngData.Columns(rngData.Columns.Count).Value = "=Average(" & rngData.Rows(1).Resize(, rngData.Columns.Count - 1).Address(0, 0) & ")"
rngData.Copy
wbkDest.Worksheets(1).Range("A1").PasteSpecial xlPasteValues
Application.CutCopyMode = False

rngData.Columns(rngData.Columns.Count).Delete

Set wbkSource = Nothing
Set wbkDest = Nothing
Set rngData = Nothing
intItem = Empty
 
Last edited:

GopalaKrishnaJ

Board Regular
Joined
Dec 19, 2011
Messages
75
if you need only result in another page then try this

Sub GetAverage()

Dim wbkSource As Workbook
Dim wbkDest As Workbook
Dim rngData As Range
Dim intItem As Integer

Set wbkSource = ThisWorkbook
Set wbkDest = Workbooks.Add

With wbkSource.Worksheets("Sheet1")
Set rngData = Intersect(.Range("A1").CurrentRegion, .Range("A1").CurrentRegion.Offset(1))
If rngData Is Nothing Then Exit Sub
Set rngData = rngData.Resize(, rngData.Columns.Count + 1)
End With

rngData.Columns(rngData.Columns.Count).Value = "=Average(" & rngData.Rows(1).Resize(, rngData.Columns.Count - 1).Address(0, 0) & ")"
rngData.Columns(rngData.Columns.Count).Copy
wbkDest.Worksheets(1).Range("A2").PasteSpecial xlPasteValues
wbkDest.Worksheets(1).Range("A1").Value = "Average_Result"
Application.CutCopyMode = False

rngData.Columns(rngData.Columns.Count).Delete

Set wbkSource = Nothing
Set wbkDest = Nothing
Set rngData = Nothing
intItem = Empty

End Sub

Try this

Dim wbkSource As Workbook
Dim wbkDest As Workbook
Dim rngData As Range
Dim intItem As Integer

Set wbkSource = ThisWorkbook
Set wbkDest = Workbooks.Add

Set rngData = wbkSource.Worksheets("Sheet1").Range("A1").CurrentRegion
Set rngData = rngData.Resize(, rngData.Columns.Count + 1)


rngData.Columns(rngData.Columns.Count).Value = "=Average(" & rngData.Rows(1).Resize(, rngData.Columns.Count - 1).Address(0, 0) & ")"
rngData.Copy
wbkDest.Worksheets(1).Range("A1").PasteSpecial xlPasteValues
Application.CutCopyMode = False

rngData.Columns(rngData.Columns.Count).Delete

Set wbkSource = Nothing
Set wbkDest = Nothing
Set rngData = Nothing
intItem = Empty
 

Watch MrExcel Video

Forum statistics

Threads
1,130,112
Messages
5,640,174
Members
417,130
Latest member
Darion2021

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
Top