Subtract 2 numbers from cell with text

Alphacsulb

Active Member
Joined
Mar 20, 2008
Messages
414
I'm not sure if this is possible but I have a cell with the following:

Sample Ballot Pamphlets / 30.5 of 32 pages /

Is it possible to make a formula that says, look for the 2 numbers in the cell and subtract the 1st number from the second.

Result = 1.5

I know it's a long shot but figured it might be possible.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
I'm not sure if this is possible but I have a cell with the following:

Sample Ballot Pamphlets / 30.5 of 32 pages /

Is it possible to make a formula that says, look for the 2 numbers in the cell and subtract the 1st number from the second.

Result = 1.5

I know it's a long shot but figured it might be possible.
Will the text always be the same except for the numbers?

Sample Ballot Pamphlets / 30.5 of 32 pages /
Sample Ballot Pamphlets / 300 of 2000 pages /
Sample Ballot Pamphlets / 10 of 10 pages /
 
Upvote 0
One possibility (a bit klunky, though) is to do Text to Columns then :-

<TABLE style="WIDTH: 110pt; BORDER-COLLAPSE: collapse" border=0 cellSpacing=0 cellPadding=0 width=147><COLGROUP><COL style="WIDTH: 110pt; mso-width-source: userset; mso-width-alt: 5376" width=147><TBODY><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #f0f0f0; BORDER-LEFT: #f0f0f0; BACKGROUND-COLOR: transparent; WIDTH: 110pt; HEIGHT: 12.75pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: #f0f0f0" id=td_post_2700009 class=xl65 height=17 width=147>=MAX(A1:I1)-MIN(A1:I1)</TD></TR></TBODY></TABLE>
 
Upvote 0
Text to Columns wont work because I could have multiple lines like the example given:

Sample Ballot Pamphlets / 30.5 of 32 pages /
Sample Ballot Pamphlets / 300 of 2000 pages /
Sample Ballot Pamphlets / 10 of 10 pages /

Helper columns can be used.
 
Upvote 0
Correct, they are always the same.
It's not exactly elegant, but it works...

Book1
AB
2Sample Ballot Pamphlets / 30.5 of 32 pages /1.5
3Sample Ballot Pamphlets / 300 of 2000 pages /1700
4Sample Ballot Pamphlets / 10 of 10 pages /0
Sheet1

Formula entered in B2:

=MID(A2,SEARCH("of",A2)+3,SEARCH(" page",A2)-SEARCH("of",A2)-3)-MID(A2,SEARCH("/",A2)+2,SEARCH(" of",A2)-SEARCH("/",A2)-2)
 
Upvote 0
It works when I put it into my a new spreadsheet, but it doesn't work in my existing document. The result is always 0. All of my cells have a format of General, any ideas on why? I had the same problem with a different formula but couldn't find out the answer.
 
Upvote 0
when I copied your dilema into a spreadsheet it was merged and centred and Biff's formula did not work when I removed merge and centre from the cell it worked fine, its how you have the original page formatted
 
Upvote 0
Alphacsulb,

If you were looking for a macro solution:


Sample data before the macro:


Excel Workbook
AB
1Sample Ballot Pamphlets / 30.5 of 32 pages /
2Sample Ballot Pamphlets / 300 of 2000 pages /
3Sample Ballot Pamphlets / 10 of 10 pages /
4
Sheet1





After the macro:


Excel Workbook
AB
1Sample Ballot Pamphlets / 30.5 of 32 pages /1.5
2Sample Ballot Pamphlets / 300 of 2000 pages /1700
3Sample Ballot Pamphlets / 10 of 10 pages /0
4
Sheet1





Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


Code:
Option Explicit
Sub Subtract2()
' hiker95, 04/28/2011
' http://www.mrexcel.com/forum/showthread.php?t=546805
Dim c As Range, H As String, Sp, a As Double, b As Double
Application.ScreenUpdating = False
For Each c In Range("A1", Range("A" & Rows.Count).End(xlUp))
  H = Mid(c, Application.Find("/", c, 1) + 2, Application.Find("pages", c, 1) - 2 - Application.Find("/", c, 1) - 1)
  Sp = Split(H, " of ")
  c.Offset(, 1).Value = CDbl(Sp(1)) - CDbl(Sp(0))
Next c
Application.ScreenUpdating = True
End Sub


Then run the Subtract2 macro.
 
Upvote 0
It works when I put it into my a new spreadsheet, but it doesn't work in my existing document. The result is always 0. All of my cells have a format of General, any ideas on why? I had the same problem with a different formula but couldn't find out the answer.
It would help if you could post a sample file with data that this formula does not work on.

You can use a free file hosting site if need be.
 
Upvote 0

Forum statistics

Threads
1,224,566
Messages
6,179,555
Members
452,928
Latest member
101blockchains

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