Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Find last cell in column and subtract.

0

I am in need of some help. I have a worksheet with dates from 1/1/18 to 12/25/30 in column A. once a once a week a a number value is entered into the corrisponding row in column B. I have a formula in column C =IF(SUM(C2-C1)=0,"",SUM(C2-C1)) I have this formula filled in the entire column. I woulf like it to look up the column for the last value so i dont have to enter a value into column B just to make the calculation work. and i need all the dates to remain in column A, removing the unused rows would work but they need to stay. Any help is much appreciated!  I have added a test workbook to show what i am trying to do. The first column containing dates does not matter. The dates are uses in othe code within the workbook.

Answer
Discuss

Discussion

Hi Justin.

Sorry but I can't make sense of your question. You say you have a formula in the entire column C but that refers to values in the same column (so makes no sense since you'd get circular reference issues). Does the calculation have to relate to the row holding current date in column A?

I'm going out now but please edit your original question to explain and use the Add Files... button to upload a representative Excel file (without any personal data) to show your existing data. Then we should be able to give specific help.
John_Ru (rep: 6142) Feb 22, '22 at 9:52 am
I have made an edit and uploaded a sample file, Dates do not relate to the calculation.
Justin2010 (rep: 8) Feb 22, '22 at 1:01 pm
Add to Discussion

Answers

0
Selected Answer

Justin

Your formula was in column D (not C as you said) but you say that there is  "other code within the workbook" so I'll suggest a VBA answer. This means you won't need a formula in column D (I've deleted them in the attached file).

The code below is "behind" Sheet1 in the attached , revised file. It uses the Worksheet_Change event which  is triggered whenever an entry is made on that sheet (or the same value is confirmed by "Enter"). Target is what VBA recognises as the cell(s) changed incidentally.

I've commented the code below so you can (hopefully) follow what's going on.

Private Sub Worksheet_Change(ByVal Target As Range)
  
'do nothing if column C wasn't changed or more than one cell was changed
If Intersect(Target, Columns(3)) Is Nothing Or Target.CountLarge > 1 Then Exit Sub
'do nothing if  if a number wasn't entered
If IsNumeric(Target.Value) = False Then Exit Sub

' disable events so thghis macro isn't re-triggered if it chnages values
Application.EnableEvents = False
    'loop back through rows
    For n = Target.Row - 1 To 2 Step -1
    ' see if there's a value in each previous row
        If Cells(n, 3).Value <> "" And IsNumeric(Cells(n, 3)) Then
            ' if so, make D = previous last less new number
            Target.Offset(0, 1).Value = Target.Value - Cells(n, 3).Value
            ' ### optional- clear all other values in D
            'Range("D2:D" & Target.Row - 1).Value = ""
            
            ' stop checking
            Exit For
        End If
    Next n
' re-enable events
Application.EnableEvents = True

End Sub

To test it, put  a value in C36 say- it will put a value in D36.

It will add a value anytime you change a value in C but if you want it to clear the previous values in D, uncomment the line in bold above.

Note that (after a couple of checks to ensure just a single value was entered) there's a line Application.EnableEvents = False (so that the macro doesn't run again as it any makes changes).

You'll see that it loops backwards through column C (from the changed row) to work out the last non-blank value and then adds a calculated value in column D and stops looping back. 

If however you really need a formula, paste this into cell D3:

=IF(ISBLANK(C3),"",C3-LOOKUP(2,1/(C$2:C2<>""),C$2:C2))

and copy it down column D. Unless there's a blank in column D, it will find the last non-blank value and subtract it from what's in column C

Hope this helps.

Discuss

Discussion

John_Ru, thank you, your code is nice but  i need a formula. I am already running worksheet change events, sheets are protected and cells are locked. My Workbook is aprox 42 sheets. Each sheet being different.
Justin2010 (rep: 8) Feb 22, '22 at 5:22 pm
Understood Justin (though those factors can be handled in VBA). It's late here and I can't think of a formula at present. Doubt I'll have time tomorrow either, sorry.
John_Ru (rep: 6142) Feb 22, '22 at 5:37 pm
Justin- I just revised my Answer to add a formula (after the VBA bit.). If that works for you, kindly select my Answer.
John_Ru (rep: 6142) Feb 22, '22 at 6:07 pm
Thank you, works perfect. Just what I was looking for.
Justin2010 (rep: 8) Feb 22, '22 at 6:16 pm
Great, thanks for selecting my answer Justin 
John_Ru (rep: 6142) Feb 22, '22 at 6:18 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login