+ Reply to Thread
Results 1 to 3 of 3

Running Total Using VBA Macros

Hybrid View

  1. #1
    Registered User
    Join Date
    01-26-2022
    Location
    Colorado
    MS-Off Ver
    Mac
    Posts
    2

    Running Total Using VBA Macros

    Hey all,

    This is my first ever attempt at using macros on Excel, so forgive me if this is a simple question.

    I am currently trying to write a code for a running total between two cells. Online I was able to find the following code:

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Range("F3"), Target) Is Nothing Then ExitSub
    [G3] = [F3] + [G3]
    End Sub
    At first I tried copying and pasting this code several times and simply changing the Cells referenced, but after some further reading, if I understand correctly, there can be only one "Worksheet_Change" Sub. So I changed my code to the following:

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Intersect(Range("F3"), Target) Then
        [G3] = [F3] + [G3]
        End If
        If Intersect(Range("F4"), Target) Then
        [G4] = [F4] + [G4]
        End If
        If Intersect(Range("F5"), Target) Then
        [G5] = [F5] + [G5]
        End If
        If Intersect(Range("F6"), Target) Then
        [G6] = [F6] + [G6]
        End If
        If Intersect(Range("F7"), Target) Then
        [G7] = [F7] + [G7]
        End If
        If Intersect(Range("F8"), Target) Then
        [G8] = [F8] + [G8]
        End If
        If Intersect(Range("F9"), Target) Then
        [G9] = [F9] + [G9]
        End If
    End Sub
    However, I now get an error:

    "Run-time error '91':
    Object variable or With block variable not set"

    After adding "Is Object" to my code in all of the If-Then statements:

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Intersect(Range("F3"), Target) Is Object Then
        [G3] = [F3] + [G3]
        End If
        If Intersect(Range("F4"), Target) Is Object Then
        [G4] = [F4] + [G4]
        End If
        If Intersect(Range("F5"), Target) Is Object Then
        [G5] = [F5] + [G5]
        End If
        If Intersect(Range("F6"), Target) Is Object Then
        [G6] = [F6] + [G6]
        End If
        If Intersect(Range("F7"), Target) Is Object Then
        [G7] = [F7] + [G7]
        End If
        If Intersect(Range("F8"), Target) Is Object Then
        [G8] = [F8] + [G8]
        End If
        If Intersect(Range("F9"), Target) Is Object Then
        [G9] = [F9] + [G9]
        End If
    End Sub
    And got the error:

    "Run-time error '424':
    Object required'

    I understand enough about coding, in general, to be relatively certain that there is some error with definitions and/or data type, but I don't know excel well enough to fix this.


    As an example of what I want my code to do, I am trying to track weekly hours and total hours spent doing a task. For example, in F3, I would like to add the hours spent doing a task that week, and then have the cell G3 update (as a running total) with the previous value in G3 added to the new value entered into F3. I would like to do this for 7 different cases, each one updating separately. The closest I got was this code:
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Intersect(Range("F3:F9"), Target) Is Nothing Then Exit Sub
        [G3] = [F3] + [G3]
        [G4] = [F4] + [G4]
        [G5] = [F5] + [G5]
        [G6] = [F6] + [G6]
        [G7] = [F7] + [G7]
        [G8] = [F8] + [G8]
        [G9] = [F9] + [G9]
    End Sub
    Which did what I wanted, but if I updated any of the Cells in F3:F9, it would re-add the others. So like if I had 10 hours for the week in F3, G3 would go to 10, but if I then added 5 hours in F4, G3 would go to 20 (adding F3 again) and G4 would go to 5, and so on.


    Again I apologize if this is an easy fix, but any help would be appreciated.

    Thanks!

    - Tyler
    Last edited by treohr; 01-26-2022 at 04:10 PM. Reason: Solved

  2. #2
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    31,252

    Re: Running Total Using VBA Macros

    try


    Private Sub Worksheet_Change(ByVal Target As Range)
        If Intersect(Range("F3:F9"), Target) Is Nothing Then Exit Sub
        Application.EnableEvents = False
        Target.Offset(0, 1) = Target.Offset(0, 1) + Target.Value
        Application.EnableEvents = True
    End Sub
    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

  3. #3
    Registered User
    Join Date
    01-26-2022
    Location
    Colorado
    MS-Off Ver
    Mac
    Posts
    2

    Re: Running Total Using VBA Macros

    John,

    That worked, thanks!

    -Tyler

+ 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. Replies: 4
    Last Post: 04-26-2021, 11:13 AM
  2. Running total after payments running total on excel
    By tmagplayr in forum Excel General
    Replies: 7
    Last Post: 09-26-2017, 02:01 PM
  3. Replies: 8
    Last Post: 07-19-2017, 12:06 PM
  4. [SOLVED] Event Driven Macros stop running after running a routine macro
    By ChemistB in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-18-2017, 12:34 PM
  5. [SOLVED] Hiding the file when running macros and after running macros
    By thilag in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-10-2016, 07:15 AM
  6. Pivot Chart Running Total - Removing Current Year Future Total
    By car3489 in forum Excel Charting & Pivots
    Replies: 5
    Last Post: 07-27-2013, 03:35 AM
  7. % of Running Total to Grand Total in Pivot Table
    By David in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-17-2005, 04:05 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