Going from Bottom to top how to check if a cell contains a number, and if it does, then subtract that cell from the next number in the column

rsd007

New Member
Joined
Oct 24, 2022
Messages
38
Office Version
  1. 2019
Platform
  1. Windows
I am looking for formula to find the value in C if value in B column and subtract from the next number in upper B cell and my sheet is keep growing in top as new data is added in top line (27..........).

Thanks for all the help :)



ABC
27​
26​
41​
?
25​
24​
23​
22​
21​
20​
19​
33​
?
18​
17​
16​
9​
?
15​
14​
13​
12​
11​
6​
?
10​
5​
5-29=?
9​
8​
7​
29​
29-10=?
6​
5​
4​
3​
10​
10-40=?
2​
1​
40​
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
The data you posted has some invisible characters that are breaking the calculations since they are not technically pure numbers.

But assuming they are correctly formatted numbers, following code works for me:

VBA Code:
Sub Calc()
    lr = Range("B" & Rows.Count).End(xlUp).Row
    a = 0
    b = Trim(Range("B" & lr).Value)
    lr = lr - 1
    For i = lr To 1 Step -1
        If Not IsEmpty(Trim(Range("B" & i).Value)) And Trim(Range("B" & i).Value) <> "" And IsNumeric(Trim(Range("B" & i).Value)) Then
            a = Range("B" & i).Value
            Range("C" & i).Value = a - b
            b = a
        End If
    Next i
End Sub
 
Upvote 0
The data you posted has some invisible characters that are breaking the calculations since they are not technically pure numbers.

But assuming they are correctly formatted numbers, following code works for me:

VBA Code:
Sub Calc()
    lr = Range("B" & Rows.Count).End(xlUp).Row
    a = 0
    b = Trim(Range("B" & lr).Value)
    lr = lr - 1
    For i = lr To 1 Step -1
        If Not IsEmpty(Trim(Range("B" & i).Value)) And Trim(Range("B" & i).Value) <> "" And IsNumeric(Trim(Range("B" & i).Value)) Then
            a = Range("B" & i).Value
            Range("C" & i).Value = a - b
            b = a
        End If
    Next i
End Sub
Thankyou iggydarsa.
I am very new to excel and have no idea about vba.
is it possible for come up with some formula please.
 
Upvote 0
I personally dont know how to lookup a number at a random position by formula. Maybe some other user can have an idea how to solve this issue but only way I am capable of doing is via VBA.
 
Upvote 0
Is this what you are after?
If not, please post again with sample data** but also include the expected results and explain again in relation to that sample data.

** Best way to provide sample data etc is XL2BB

22 10 25.xlsm
ABC
127 
226418
325 
424 
523 
622 
721 
820 
9193324
1018 
1117 
121693
1315 
1414 
1513 
1612 
171161
18105-24
199 
208 
2172919
226 
235 
244 
25310-30
262 
27140 
Subtract
Cell Formulas
RangeFormula
C1:C27C1=IF(B1="","",IFNA(B1-INDEX(B2:B$30,MATCH(TRUE,INDEX(B2:B$30<>"",0),0)),""))
 
Upvote 0
Thanks For the help,
I was trying to use XL2BB but not working. as shown in photo, will like to subtract automatically 41-33=8, 33-9=24, 9-6=3, 6-5=1 and will show in column C and if result is negative show negative.
I was told that in EXCEL data should be entered from top to bottom or left to right

Thanks
 

Attachments

  • Screenshot .png
    Screenshot .png
    14.9 KB · Views: 6
Last edited:
Upvote 0
I was trying to use XL2BB but not working.
In what way is it not working?
At what point in the instructions in the link I provided does the problem occur?

You did not specify what you want to happen in row 21 where there is a number, but no more numbers below. See if this is headed in the right direction.

rsd007.xlsm
BC
1
2418
3 
4 
5A 
6 
7 
8 
93324
10 
11 
1293
13 
14 
15 
16 
1761
185-24
19 
20X 
2129#N/A
Subtract (2)
Cell Formulas
RangeFormula
C2:C21C2=IF(ISNUMBER(B2),B2-INDEX(B3:B$22,MATCH(TRUE,ISNUMBER(B3:B$22),0)),"")
 
Upvote 0
In what way is it not working?
At what point in the instructions in the link I provided does the problem occur?

You did not specify what you want to happen in row 21 where there is a number, but no more numbers below. See if this is headed in the right direction.

rsd007.xlsm
BC
1
2418
3 
4 
5A 
6 
7 
8 
93324
10 
11 
1293
13 
14 
15 
16 
1761
185-24
19 
20X 
2129#N/A
Subtract (2)
Cell Formulas
RangeFormula
C2:C21C2=IF(ISNUMBER(B2),B2-INDEX(B3:B$22,MATCH(TRUE,ISNUMBER(B3:B$22),0)),"")
Hello,

It was my mistake I was subtracting from next cell, which is not blank, needed other way subtracting 33-41= (-8); 9-33= (-24); 6-9= (-3); 5-6= (-1); 29-5= 24
Also ignore if blank or anything other then number.
Row 2 should be empty as it is the first entry and row 21 should be 29-5=24
 
Upvote 0
While I am considering that new information, what about the first two questions in my previous post?
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,881
Members
452,948
Latest member
Dupuhini

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