+ Reply to Thread
Results 1 to 7 of 7

average certain blocks of cells VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    11-18-2014
    Location
    australia
    MS-Off Ver
    2015
    Posts
    43

    average certain blocks of cells VBA

    i have a function that is dividing the number of rows used by 360 degrees. this number is different every time the sheet is run

    now i want to take the data i have and average the values in chunks corresponding to that number

    for example: i have 100 rows of data, so i will need the first 4 rows averaged and a number returned, then the next 4 rows...etc. until i've reached the last row of data.

    i'm having a harder time than i probably should be with this one.

    thanks (again) forum!

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: average certain blocks of cells VBA

    Can you post your function?

    Don't forget to use code tags - you just highlight the code and click on the #.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Registered User
    Join Date
    11-18-2014
    Location
    australia
    MS-Off Ver
    2015
    Posts
    43

    Re: average certain blocks of cells VBA

    thanks!

    Dim i As Integer
    Dim avg As Double
    
    For i = 2 To lrownew
    avg = (Range("i" & i).Value + Range("i" & i + 1).Value + Range("i" & i + 2).Value + Range("i" & i + 3).Value) / deg
    Range("n" & i).Value = avg
    Next i
    this "works", as long as deg = 4 which is not true

    i simply can't figure out how to just do the average every nth block
    Last edited by ferday; 06-07-2016 at 07:27 PM.

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: average certain blocks of cells VBA

    Is this something like what you want?

    Sub Ferday(): Dim lrownew As Long, i As Long, deg As Single, avg As Double
    Dim WF As WorksheetFunction: Set WF = WorksheetFunction
    lrownew = 701: deg = 3#
    For i = 2 To lrownew
    avg = WF.Sum((Range("I" & i).Resize(1, 4).Value)) / deg
    Range("N" & i).Value = avg
    Next i
    End Sub

  5. #5
    Registered User
    Join Date
    11-18-2014
    Location
    australia
    MS-Off Ver
    2015
    Posts
    43

    Re: average certain blocks of cells VBA

    thanks mate

    that got me results, but not correct ones. i'm not even sure why they were incorrect, i'm trying some more permutations of your code to see if i can source it

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: average certain blocks of cells VBA

    Can you post your whole code? - I had to make some assignments so it would run for me.

  7. #7
    Registered User
    Join Date
    11-18-2014
    Location
    australia
    MS-Off Ver
    2015
    Posts
    43

    Re: average certain blocks of cells VBA

    i've currently got it working with a bit of a bodge (it still won't average just the grouping, but the counting was really easy)

    it's really fast right now even with 20000 lines, so i'm happy but i'm always interested in learning new and better ways of doing this stuff for as often as i have to use it

    thanks so much for your time

    lrownew is just the last row number of data, since the user can paste any amount
    deg is based on the entered diameter of the object, and just translates the circumference into 10 degree chunks which is what i'm trying to average over (so they aren't important to the function, they are entered data)

    Dim i As Long, avg As Double, j As Long, avg2 As Double
    Dim WF As WorksheetFunction: Set WF = WorksheetFunction
    For i = 2 To lrownew
    avg = WF.Sum((Range("I" & i).Resize(deg, 1).Value)) / deg
    Range("k" & i).Value = avg
    avg2 = WF.Sum((Range("J" & i).Resize(deg, 1).Value)) / deg
    Range("l" & i).Value = avg2
    Next i
    
    For j = 2 To lrownew Step deg
    Sheet2.Range("a" & j).Value = Range("k" & j).Value
    Sheet2.Range("b" & j).Value = Range("k" & j).Value + 10
    Sheet2.Range("c" & j).Value = Range("l" & j).Value
    Sheet2.Range("d" & j).Value = Range("l" & j).Value + 10
    
    Next j
    Last edited by ferday; 06-08-2016 at 12:50 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Sorting blocks of cells according to how full they are
    By mywinningsmile in forum Excel General
    Replies: 9
    Last Post: 12-19-2014, 02:53 PM
  2. Count blocks of cells that have a meet a ste value,
    By Tim Lord in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-16-2012, 06:50 AM
  3. Using macros to identify blocks of data and summarise those blocks
    By gophbeav in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-05-2012, 04:35 AM
  4. Transposing blocks of cells
    By parkinsc in forum Excel General
    Replies: 1
    Last Post: 10-05-2007, 01:29 PM
  5. Format blocks of cells
    By Tellm in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-14-2007, 07:19 AM
  6. Replies: 1
    Last Post: 03-26-2007, 07:01 PM
  7. [SOLVED] Transposing blocks of cells-What would be the code to do this?
    By Dave in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-23-2006, 12:00 PM

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