+ Reply to Thread
Results 1 to 7 of 7

Find 0's down a column and replace the Average of adjacent cells

Hybrid View

  1. #1
    Registered User
    Join Date
    11-15-2011
    Location
    St. Louis, MO
    MS-Off Ver
    Excel 2013
    Posts
    69

    Red face Find 0's down a column and replace the Average of adjacent cells

    I would like to search a column for 0, when found, replace with the Average of the cell above and the cell below. I found a piece of code that will do what I want, but would like to find a way to incorporate that piece of code that does work into a Find and Replace. I don't want to have to loop through every cell.

    Sub Replace_Value_w_Ave()
    
    Dim rngLocal As Range   'Create the Range
        
        Set rngLocal = Union(ActiveCell.Offset(-1, 0).Resize(1, 1), ActiveCell.Offset(1, 0).Resize(1, 1))   'Set the Range value to be the cell before and the cell after
        rngAvg = WorksheetFunction.Average(rngLocal)    'Set the variable to the average of the 2 cells.
        ActiveCell.Value = rngAvg
    
    End Sub
    The above code works great if I pre-select a cell with a zero, and run this macro. My attempt to place this into a macro that includes the Find and Replace hasn't worked.
    Sub TestAverageFormula()
    '
    '
    '
    
    Dim rngLocal As Range
        Set rngLocal = Union(ActiveCell.Offset(-1, 0).Resize(1, 1), ActiveCell.Offset(1, 0).Resize(1, 1))
        rngAvg = WorksheetFunction.Average(rngLocal)
    
        Columns("H:H").Select
        Selection.Find(What:="0", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
            :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
            False, SearchFormat:=False).Activate
        ActiveCell.Replace What:="0", Replacement:=rngAvg, _
            LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat _
            :=False, ReplaceFormat:=False
            Selection.FindNext(After:=ActiveCell).Activate
    End Sub
    Any Suggestions or ideas of another way, would be helpful.
    Steve

  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: Find 0's down a column and replace the Average of adjacent cells

    Try this:

    Sub TestAverageFormula(): Dim F As Range
    Dim rngLocal As Range, rngAvg As Single
        
        Set F = Range("H:H").Find(0)
        
        Do
        If Not F Is Nothing Then
        
    Set rngLocal = Union(F.Offset(-1, 0).Resize(1, 1), F.Offset(1, 0).Resize(1, 1))
                    rngAvg = WorksheetFunction.Average(rngLocal)
        
        F.Replace 0, rngAvg
            Set F = Range("H:H").FindNext(F)
        End If:    Loop While Not F Is Nothing
    End Sub
    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-15-2011
    Location
    St. Louis, MO
    MS-Off Ver
    Excel 2013
    Posts
    69

    Re: Find 0's down a column and replace the Average of adjacent cells

    xladept, Thank you very much.
    Your code works but is working on cells with leading zeros. Many of the values in column H are below 1 decimal values with leading zero's, so it is returning some strange results. I need this to only work on cells that only have a single zero, and return values of like kind, single decimal place values.
    Last edited by sdingman; 07-12-2017 at 02:54 PM. Reason: Forgot

  4. #4
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Find 0's down a column and replace the Average of adjacent cells

    Try this:

    Replace the zeros with =Memo()

    The Memo function will do the heavy work for you.

    
    Sub Macro5()
        Columns("B:B").Replace What:="0", Replacement:="=memo()", LookAt:=xlWhole
    End Sub


    
    Function Memo()
    
    If Application.Caller.Row > 1 Then
    
    A = Application.Caller.Offset(-1, 0)
    Else
    A = 0
    End If
    
    Memo = (A + Application.Caller.Offset(1, 0)) / 2
    
    End Function
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  5. #5
    Registered User
    Join Date
    11-15-2011
    Location
    St. Louis, MO
    MS-Off Ver
    Excel 2013
    Posts
    69

    Re: Find 0's down a column and replace the Average of adjacent cells

    mehmetcik, Works great, and very, very fast. Thank You
    Steve

  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: Find 0's down a column and replace the Average of adjacent cells

    Oops

    Sub TestAverageFormula(): Dim F As Range
    Dim rngLocal As Range, rngAvg As Single
        
        Set F = Range("H:H").Find(0, lookat:=xlWhole)
        
        Do
        If Not F Is Nothing Then
        
    Set rngLocal = Union(F.Offset(-1, 0).Resize(1, 1), F.Offset(1, 0).Resize(1, 1))
                    rngAvg = WorksheetFunction.Average(rngLocal)    
                                                                                  F.Value=rngAvg
            Set F = Range("H:H").FindNext(F)
        End If:    Loop While Not F Is Nothing
    End Sub
    Last edited by xladept; 07-12-2017 at 03:38 PM. Reason: Replacer not wanted - better version

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

    Re: Find 0's down a column and replace the Average of adjacent cells

    You're welcome and thanks for the rep!

    *I modified the code.

+ 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. [SOLVED] Average column if adjacent cells are between a specific number range
    By Madtaxi in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 06-20-2017, 08:56 PM
  2. Looping 'find' to find a value and replace a value in an adjacent column
    By Boltsie in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-12-2016, 06:54 PM
  3. Macro to find all specific values in column and replace adjacent cell values
    By dblock02 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-23-2013, 06:03 AM
  4. Average range of cells in column if values in adjacent column are equal
    By RyNye in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-09-2012, 10:12 AM
  5. Find and replace based on adjacent cells?
    By Leo328i6 in forum Excel General
    Replies: 3
    Last Post: 03-16-2011, 03:59 AM
  6. Find multiple words and replace the adjacent column with one word
    By ic3cold in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-13-2011, 05:04 PM
  7. Average non-adjacent cells in column
    By raydaw in forum Excel General
    Replies: 1
    Last Post: 05-22-2008, 10:11 AM

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