+ Reply to Thread
Results 1 to 4 of 4

Get Macros To Overwrite Total On Same Cell

Hybrid View

  1. #1
    Registered User
    Join Date
    06-29-2012
    Location
    Edmonton, Alberta
    MS-Off Ver
    Excel 2007
    Posts
    41

    Question Get Macros To Overwrite Total On Same Cell

    Hello,

    I have this code that counts the # of occupied cells and inputs the count into an empty cell in column F.

    Sub CountOccupiedCells()
        Dim nRow As Long
        Dim nCol As Long
        Dim nCount As Long
        
        For nRow = 2 To ActiveSheet.UsedRange.Rows.Count
            For nCol = 1 To ActiveSheet.UsedRange.Columns.Count
                If Not IsEmpty(Cells(nRow, nCol)) Then nCount = nCount + 1
            Next nCol
        Next nRow
        Range("F" & Cells(Rows.Count, "F").End(xlUp).Row + 1) = nCount
    End Sub
    How do i change this so that the macros puts the count total in cell F3 everytime (even if it has to overwrite the last total it counted).

    I want it to do this because the occupied cells its counting could increase, and I want only 1 updated total to appear when i run the macros after i've added more entries. Because right now it puts the new total under the last total.

    thanks!

  2. #2
    Forum Contributor wakeupcall's Avatar
    Join Date
    02-29-2012
    Location
    Sofia, Bulgaria
    MS-Off Ver
    Excel 2010
    Posts
    155

    Re: Get Macros To Overwrite Total On Same Cell

    Hi,

    I believe this should do the trick

    
    Sub CountOccupiedCells()
        Dim nRow As Long
        Dim nCol As Long
        Dim nCount As Long
        
        For nRow = 2 To ActiveSheet.UsedRange.Rows.Count
            For nCol = 1 To ActiveSheet.UsedRange.Columns.Count
                If Not IsEmpty(Cells(nRow, nCol)) Then nCount = nCount + 1
            Next nCol
        Next nRow
        Range("F3") = nCount
    End Sub

  3. #3
    Registered User
    Join Date
    06-29-2012
    Location
    Edmonton, Alberta
    MS-Off Ver
    Excel 2007
    Posts
    41

    Unhappy Re: Get Macros To Overwrite Total On Same Cell

    Hello,

    Thank you for your reply.
    I tried what you gave me with just a few simple numbers to make sure it worked and it did perfectly. The problem is when I applied it to the workbook I needed it to, It didn't fill the same cell like it did with my example, it filled the cell underneath the previous one again.

    Not sure why, does anyone know?

    Thanks

  4. #4
    Forum Contributor wakeupcall's Avatar
    Join Date
    02-29-2012
    Location
    Sofia, Bulgaria
    MS-Off Ver
    Excel 2010
    Posts
    155

    Re: Get Macros To Overwrite Total On Same Cell

    Hi,

    this is probably because the used range in the second excel workbook is greater than it appears at first sight.

    Press Ctrl+End and see where it takes you. If it takes you to a cell that is way below the last one occupied, you should select the seemingly empty cells and clear all/delete the column/row.

    Re-running the macro after that should work properly

+ 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