+ Reply to Thread
Results 1 to 8 of 8

average function for macro

Hybrid View

  1. #1
    Registered User
    Join Date
    12-19-2012
    Location
    dublin ireand
    MS-Off Ver
    Excel 2007
    Posts
    98

    average function for macro

    What I'm trying to do is just a simple average on a column of data. The data can have quite a variation in the number of cells that it fills. What I'm struggling with is getting the right code tolook to the first cell of data, then basically select all of the data in the column and give me the average. and display it in a different column
    Like I said the amount of data will change from day to day, so it has to be a dynamic sort of cell range within the average function. Anybody have any ideas?

    bascially need to convert this
    = AVERAGE(F2:F23)
    to one with not set rows as there might be more than 23 ot less than 23

    need it to be done while thi function is running

    Sub FUND_OWNERSHIP()Dim i As Long, LR As Long
    Application.ScreenUpdating = False
    On Error Resume Next
    
    
        
        
         Set ms = Workbooks("Book4.xlsx").Sheets("Allocation")
        With Worksheets("Allocation")
    
    
            LR = .Cells.Find("*", , , , xlByRows, xlPrevious).Row
              For i = 1 To LR
                 If IsDate(.Cells(i, 1)) Then
                     .Cells(i, 1).Copy
                    MyDate = ms.Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial(xlPasteValues)
                    MyDate.NumberFormat = "dd/mm/yyyy"
                End If
                    If UCase$(.Cells(i, 30).Value) = "FUND OWNERSHIP %" Then
                                                  
                    .Cells(i, "AD").Offset(1).Resize(4).Copy
                    
                    MyData = Format(ms.Range("B" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial(Transpose:=True), Percent)
                        
                       
    
    
                   
                  End If
              Next i
              
              
        End With
           
    Application.CutCopyMode = 0
    
    
    Application.ScreenUpdating = True
    
    
    End Sub
    Last edited by rjhe22; 01-10-2013 at 08:09 AM.

  2. #2
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    16,040

    Re: average function for macro

    Just select whole range.

    =AVERAGE(F:F) will give you average value of cells with numbers (ignoring text and blank cells).
    Never use Merged Cells in Excel

  3. #3
    Registered User
    Join Date
    12-19-2012
    Location
    dublin ireand
    MS-Off Ver
    Excel 2007
    Posts
    98

    Re: average function for macro

    and do you just put that line into macro function

  4. #4
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    16,040

    Re: average function for macro

    Just use:
    result = application.WorksheetFunction.Average(Range("F1:F65536"))

  5. #5
    Registered User
    Join Date
    12-19-2012
    Location
    dublin ireand
    MS-Off Ver
    Excel 2007
    Posts
    98

    Re: average function for macro

    can when my marco runs it might add 23 rows then i want to displau the average in a cell under it

    next month the marco might run again add new data to existing data and i want to average the new data and display that ina cell right under new data.

    will that code work. as im not sure.
    also how do u get it to write to the cell right after the rows

  6. #6
    Registered User
    Join Date
    12-19-2012
    Location
    dublin ireand
    MS-Off Ver
    Excel 2007
    Posts
    98

    Re: average function for macro

    i want it to turn out like this when the marco is run.
    i have the rest of it done but the averages
    eg1.xlsx

    so when the lines are run it does the average and put them into the monthly avaerage clolums but as i wont no if its 28 23 or 19 items been added i need it to be able to count till last date then enter it

    is it possible

  7. #7
    Registered User
    Join Date
    12-19-2012
    Location
    dublin ireand
    MS-Off Ver
    Excel 2007
    Posts
    98

    Re: average function for macro

    actually is it possible to calculate the average of say column b in sheet 1 and display that average in sheet 2 of a workbook

  8. #8
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    16,040

    Re: average function for macro

    Try

    Sheets("Sheet2").Range("C2") = application.WorksheetFunction.Average(Sheets("sheet1").Range("B1:B65536"))

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1