Autosum formula.

mdileep

Board Regular
Joined
Dec 8, 2019
Messages
54
Office Version
  1. 2007
Platform
  1. Windows
  2. Mobile
Hi friends Asking a query so a long time.

D4 = E4 + F4 + G4
If I put value in D4=400 and then E4 and F4 150 each then by formula automatically get value 100 in G4.
If I put value in D4=400 and then F4 and G4 150 each then by formula automatically get value 100 in E4.
If I put value in D4=400 and then E4 and G4 150 each then by formula automatically get value 100 in F4.
with excel 2007.
Thanks.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
The following is what I came up with for you to test. It seems to work for the tests I put it through.

Just right click on the sheet tab at the bottom of the excel window that you want to run the code in, select 'View Code' and then paste the following code into the large window that appears. Then return to the sheet and test it out.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'
    If Target.Cells.Count = 1 Then                                                          ' If only one cell changed then ...
        If Not Intersect(Target, Range("D4:G4")) Is Nothing Then                            '   If the Target cell is in the SumRange then ...
            Dim BlanksInRange   As Long
            BlanksInRange = WorksheetFunction.CountBlank(Range("D4:G4"))                    '       Get count of blank cells in the SumRange
'
            Application.EnableEvents = False                                                '       Turn off EnableEvents to prevent recursive looping
'
            If BlanksInRange = 0 Then GoTo NoBlankInRangeErrorHandler                       '       If no blanks in the SumRange then goto NoBlankInRangeErrorHandler
            If BlanksInRange = 1 Then GoTo CheckForDesiredNewEntries                    '       If only 1 blank in the SumRange then goto CheckForDesiredNewEntries
'
NormalProcessing:
            Select Case BlanksInRange
                Case 1                                                                      '           If only 1 blank cell in the SumRange then ...
                    Dim MissingCell As String
                    MissingCell = Range("D4:G4").SpecialCells(xlCellTypeBlanks).Address(0, 0)   '               Set MissingCell = the blank cell address found
'
                    Select Case MissingCell
                        Case "D4"                                                           '                   If MissingCell = D4 then ...
                            Range("D4").Formula = "=Sum(E4:G4)"                             '                       Set D4 = Sum(E4:G4
                        Case "E4"                                                           '                   If MissingCell = E4 then ...
                            Range("E4").Formula = "=Sum(D4,-F4,-G4)"                        '                       Set E4 = D4 - F4 - G4
                        Case "F4"                                                           '                   If MissingCell = F4 then ...
                            Range("F4").Formula = "=Sum(D4,-E4,-G4)"                        '                       Set F4 = D4 - E4 - G4
                        Case "G4"                                                           '                   If MissingCell = G4 then ...
                            Range("G4").Formula = "=Sum(D4,-E4,-F4)"                        '                       Set G4 = D4 - E4 - F4
                    End Select
'
                    Range("D4:G4").Value = Range("D4:G4").Value                             '               Clear formulas from SumRange, leave just the values
            End Select
        End If
    End If
'
    Application.EnableEvents = True                                                         ' Turn EnableEvents back on
    Exit Sub
'
NoBlankInRangeErrorHandler:                                                                 ' No blanks found in the SumRange after a change so ...
    Application.Undo                                                                        '   Undo the last change
    Application.EnableEvents = True                                                         '   Turn EnableEvents back on
    Exit Sub
'
CheckForDesiredNewEntries:                                                                  ' One blank found in the SumRange after a change so ...
    Application.Undo                                                                        '   Undo the last change
'
    BlanksInRange = WorksheetFunction.CountBlank(Range("D4:G4"))                            '   Do a new check for # of blanks in the SumRange
'
    If BlanksInRange = 0 Then                                                               '   If BlanksInRange = 0 then ... User probably wanting to delete/restart
        Application.Undo                                                                    '       Undo the last change
        Application.EnableEvents = True                                                     '       Turn EnableEvents back on
        Exit Sub
    Else                                                                                    '   Else ...
        If BlanksInRange = 2 Then                                                           '       User probably still entering initial values
            Application.Undo                                                                '           Undo the last change
'
            BlanksInRange = WorksheetFunction.CountBlank(Range("D4:G4"))                    '           Do a new check for # of blanks in the SumRange
            GoTo NormalProcessing
        End If
    End If
End Sub
 
Upvote 0
The following is what I came up with for you to test. It seems to work for the tests I put it through.

Just right click on the sheet tab at the bottom of the excel window that you want to run the code in, select 'View Code' and then paste the following code into the large window that appears. Then return to the sheet and test it out.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'
    If Target.Cells.Count = 1 Then                                                          ' If only one cell changed then ...
        If Not Intersect(Target, Range("D4:G4")) Is Nothing Then                            '   If the Target cell is in the SumRange then ...
            Dim BlanksInRange   As Long
            BlanksInRange = WorksheetFunction.CountBlank(Range("D4:G4"))                    '       Get count of blank cells in the SumRange
'
            Application.EnableEvents = False                                                '       Turn off EnableEvents to prevent recursive looping
'
            If BlanksInRange = 0 Then GoTo NoBlankInRangeErrorHandler                       '       If no blanks in the SumRange then goto NoBlankInRangeErrorHandler
            If BlanksInRange = 1 Then GoTo CheckForDesiredNewEntries                    '       If only 1 blank in the SumRange then goto CheckForDesiredNewEntries
'
NormalProcessing:
            Select Case BlanksInRange
                Case 1                                                                      '           If only 1 blank cell in the SumRange then ...
                    Dim MissingCell As String
                    MissingCell = Range("D4:G4").SpecialCells(xlCellTypeBlanks).Address(0, 0)   '               Set MissingCell = the blank cell address found
'
                    Select Case MissingCell
                        Case "D4"                                                           '                   If MissingCell = D4 then ...
                            Range("D4").Formula = "=Sum(E4:G4)"                             '                       Set D4 = Sum(E4:G4
                        Case "E4"                                                           '                   If MissingCell = E4 then ...
                            Range("E4").Formula = "=Sum(D4,-F4,-G4)"                        '                       Set E4 = D4 - F4 - G4
                        Case "F4"                                                           '                   If MissingCell = F4 then ...
                            Range("F4").Formula = "=Sum(D4,-E4,-G4)"                        '                       Set F4 = D4 - E4 - G4
                        Case "G4"                                                           '                   If MissingCell = G4 then ...
                            Range("G4").Formula = "=Sum(D4,-E4,-F4)"                        '                       Set G4 = D4 - E4 - F4
                    End Select
'
                    Range("D4:G4").Value = Range("D4:G4").Value                             '               Clear formulas from SumRange, leave just the values
            End Select
        End If
    End If
'
    Application.EnableEvents = True                                                         ' Turn EnableEvents back on
    Exit Sub
'
NoBlankInRangeErrorHandler:                                                                 ' No blanks found in the SumRange after a change so ...
    Application.Undo                                                                        '   Undo the last change
    Application.EnableEvents = True                                                         '   Turn EnableEvents back on
    Exit Sub
'
CheckForDesiredNewEntries:                                                                  ' One blank found in the SumRange after a change so ...
    Application.Undo                                                                        '   Undo the last change
'
    BlanksInRange = WorksheetFunction.CountBlank(Range("D4:G4"))                            '   Do a new check for # of blanks in the SumRange
'
    If BlanksInRange = 0 Then                                                               '   If BlanksInRange = 0 then ... User probably wanting to delete/restart
        Application.Undo                                                                    '       Undo the last change
        Application.EnableEvents = True                                                     '       Turn EnableEvents back on
        Exit Sub
    Else                                                                                    '   Else ...
        If BlanksInRange = 2 Then                                                           '       User probably still entering initial values
            Application.Undo                                                                '           Undo the last change
'
            BlanksInRange = WorksheetFunction.CountBlank(Range("D4:G4"))                    '           Do a new check for # of blanks in the SumRange
            GoTo NormalProcessing
        End If
    End If
End Sub
Thank you !
 
Upvote 0

Forum statistics

Threads
1,214,545
Messages
6,120,132
Members
448,947
Latest member
test111

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top