+ Reply to Thread
Results 1 to 5 of 5

VBA not overwriting cells

Hybrid View

  1. #1
    Registered User
    Join Date
    07-13-2011
    Location
    Glasgow
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    21

    VBA not overwriting cells

    HI I have this code and it is averaging every 12 cells.

     Sub avg_sun()
    Dim i As Long
    
    For i = 35 To 8674 Step 12
    
    Range("CC" & Rows.Count).End(xlUp).Offset(1).Value = WorksheetFunction.Average(Worksheets("sun").Range("BP" & i).Resize(12))
    
    Next i
    
    End Sub

    However I have noticed a problem. When the code is run the first time and the destination cells were the averages are printed to are blank the code works correctly.

    However if the code is run for a second time instead of overwriting the answers already in the cells the new answers are placed at the end

    For instance if the first time run the code puts the averages in cells C2 to C722 the next time the code is run instead of overwriting C2 to C722 It places the averages in cells C723 to 1443.

    Any idea how I can change this code to over right the cells.

    Cheers

    David
    Last edited by mdavid800; 08-07-2011 at 12:47 PM.

  2. #2
    Valued Forum Contributor
    Join Date
    06-19-2010
    Location
    Holywell, N Wales, UK
    MS-Off Ver
    Excel 2013
    Posts
    470

    Re: VBA not overwriting cells

    Why not clear the column before entering the loop?
    Columns("CC").ClearContents

  3. #3
    Valued Forum Contributor
    Join Date
    02-12-2011
    Location
    The Netherlands
    MS-Off Ver
    365
    Posts
    860

    Re: VBA not overwriting cells

    Or:
     Sub avg_sun()
    Dim i As Long
    
    Range("CC2:CC" & Range("CC" & Rows.Count).End(xlUp).Row).ClearContents
    For i = 35 To 8674 Step 12
    
    Range("CC" & Rows.Count).End(xlUp).Offset(1).Value = WorksheetFunction.Average(Worksheets("sun").Range("BP" & i).Resize(12))
    
    Next i
    
    End Sub
    Last edited by HSV; 08-07-2011 at 12:26 PM.
    Harry.

  4. #4
    Registered User
    Join Date
    07-13-2011
    Location
    Glasgow
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    21

    Re: VBA not overwriting cells

    Thanks guys, nice simple solution

    cheers

    ps. how do you mark these threads as solved

  5. #5
    Valued Forum Contributor
    Join Date
    06-19-2010
    Location
    Holywell, N Wales, UK
    MS-Off Ver
    Excel 2013
    Posts
    470

    Re: VBA not overwriting cells

    See Forum Rules No.9

    9. Acknowledge the responses you receive, good or bad. If your problem is solved, please say so clearly, and mark your thread as Solved: Click the Edit button on your first post in the thread, Click Go Advanced, select [SOLVED] from the Prefix dropdown, then click Save Changes. If more than two days have elapsed, the Edit button will not appear -- ask a moderator to mark it

+ 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