+ Reply to Thread
Results 1 to 6 of 6

Problems with code for keeping running total

Hybrid View

  1. #1
    Registered User
    Join Date
    01-12-2013
    Location
    Massachusetts
    MS-Off Ver
    Excel 2007
    Posts
    3

    Problems with code for keeping running total

    Hi All,
    I have a spreadsheet that i have been using and refining since being treasurer for an organization. My term is up and im relinquishing control to someone who is far less capable than i with Excel.
    So im trying to simplify some of the monthly tasks that need to be done when entering data into this spreadsheet.

    I came across this VBA code which allows me to keep a running total of a monthly collected amount.
    When i enter the amount in one cell, it adjusts the total in another.
    Heres is the code:
    Private Sub Worksheet_Change(ByVal Target As Excel.Range)
          With Target
             If .Address(False, False) = "B25" Then
                If IsNumeric(.Value) Then
                   Application.EnableEvents = False
                   Range("B26").Value = Range("B26").Value + .Value
                   Application.EnableEvents = True
                End If
             End If
          End With
        End Sub
    The issue im having is that i have created a new sheet for each month of the year and have created ones through 2015 to help keep it simple for the new guy. While the VBA code works well, im not sure how to carry over the total from the prior months sheet to the new months sheet and still keep this code working.
    Im sure there is a way and was hoping someone could help me out with this. Im no Excel guru, so please be gentle

    On another note.
    The work sheets for every month of the year are 46 rows x 5 columns in size with fairly simple formulas.
    The only VBA code is whats posted above. With that said, at what point should i consider creating a new workbook. This current one goes from 2008 to 2015 and thus has approximately 96 sheets.
    Last edited by LOTL; 01-12-2013 at 03:27 PM. Reason: Change Thread Title

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

    Re: Problems with code for keeping running total

    Are your sheets in order that you could add value from previous sheet?

    for example, if you have number of sheets:
     n = Worksheets.Count
    and new sheet is last (n-th), then you could add
    Worksheets(n).Range("B26").value = Worksheets(n).Range("B26").value + Worksheets(n-1).Range("B26").value
    Does that help?

  3. #3
    Registered User
    Join Date
    01-12-2013
    Location
    Massachusetts
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Problems with code for keeping running total

    Thanks Zbor,
    All years are in order with year end summary's in between. I am only using this VBA code starting February of 2013 going forward.

    If i was going to try this, would i just drop your code in as i have shown below.
    I used 12 for the value of N for the 12 months of the year. How would this work for the 1st month, if the year end summary was present? And is 12 the correct number to use or should i enter in 24 for the next 2 years i have created sheets for?
    Private Sub Worksheet_Change(ByVal Target As Excel.Range)
          With Target
             If .Address(False, False) = "B25" Then
                If IsNumeric(.Value) Then
                   Application.EnableEvents = False
                   Range("B26").Value = Range("B26").Value + .Value
                   Application.EnableEvents = True
                   Worksheets(12).Range("B26").value = Worksheets(12).Range("B26").value + Worksheets(n-1).Range("B26").value
                End If
             End If
          End With
        End Sub

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

    Re: Problems with code for keeping running total

    You can also set worksheets at beggining so it won't influence if you run macro in wrong sheet.
    For example

    Dim wslast as Worksheet, wsprevious as Worksheet
    Dim n as integer
    
    n = Worksheets.Count
    Set wslast = Sheets(n-8)
    Set wsprevious = Sheets(n-9)
    
     If IsNumeric(.Value) Then
                   Application.EnableEvents = False
                   wslast.Range("B26").Value = wslast.Range("B26").Value + .Value
    wslast.Range("B26").value = wslast.Range("B26").value + wsprevious.Range("B26").value
    End if

  5. #5
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,632

    Re: Problems with code for keeping running total

    Hard to say without sample workbook: depends how you add sheets. Worksheet(1) is first worksheet. Can be some Summary also, not only your data.
    Worksheet(2) is second sheet.. And so on...
    Worksheet(12) should be variable, not static. So if you have in total n worksheets (for example 20), and you dealing with 12-th (because you have some additional sheets) then your formula would look something like:

     
    Dim n as integer
    n = Worksheets.Count
    Worksheets(n-8).Range("B26").value = Worksheets(n-8).Range("B26").value + Worksheets(n-9).Range("B26").value
    Because 12 is 20-8, and previous is 20-9.
    Of course, if yoiu add sheets in reverse order you must reverse your logic.

  6. #6
    Registered User
    Join Date
    01-12-2013
    Location
    Massachusetts
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Problems with code for keeping running total

    Thanks again Zbor, but its all a little beyond my grasp. Ill either leave it as is or just go back to the manual method.

+ 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