VBA code to produce substracted result

Wafee

Board Regular
Joined
May 27, 2020
Messages
104
Office Version
  1. 2013
Platform
  1. Windows
Hi,

Can someone help me with VBA code that does the following.
There will be three types of values in. They are numbers, percentage and fractions. For numbers and percentages it has to subtract and produce the output. for fractions it has to subtract above and below values separately and produce the output.

As shown below, in fractions it has take above values from both and substarct them and then do the same for below values.

Column AColumn BColumn C
506010
65%50%-5%
73/6578/605/-5
62/7060/65-2/-5
 
Then try this one. Should work for any number of "/" terms. eg 45/85/42/55/21 and 40/22/60/15/88

VBA Code:
Sub SubtractThem_v2()
  Dim r As Range
  Dim s1 As String, s2 As String, s As String
  Dim Bits1 As Variant, Bits2 As Variant
  Dim i As Long

  Application.ScreenUpdating = False
  For Each r In Range("A2", Range("A" & Rows.Count).End(xlUp))
      s1 = r.Text
      s2 = r.Offset(, 1).Text
      Select Case True
        Case InStr(1, s1, "/") > 0
          Bits1 = Split(s1, "/")
          Bits2 = Split(s2, "/")
          s = vbNullString
          For i = 0 To UBound(Bits1)
            s = s & "/" & (Bits2(i) - Bits1(i))
          Next i
          r.Offset(, 2).Value = Mid(s, 2)
        Case Right(s1, 1) = "%"
          r.Offset(, 2).Value = (Split(s2, "%")(0) - Split(s1, "%")(0)) & "%"
        Case IsNumeric(s1)
          r.Offset(, 2).Value = s2 - s1
      End Select
  Next r
  Application.ScreenUpdating = True
End Sub

Wafee 2020-08-20 1.xlsm
ABC
1
2506010
365%50%-15%
473/6578/605/-5
5PastPresent
650/60/7055/58/725/-2/2
762/7060/65-2/-5
820%20%0%
945/85/42/55/2140/22/60/15/88-5/-63/18/-40/67
Subtract
That works mate, thanks again.
 
Upvote 0

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Hi,

Can anyone help me with the same issue wherein i have both text and numbers combined in a cel, however wish to subtract the 2 numbers.

For eg: Net sales for Max 30000 vs Bea 43000

Need a sepearte column in which the difference is given
Please advise.
 
Upvote 0
Hi,

Can anyone help me with the same issue wherein i have both text and numbers combined in a cel, however wish to subtract the 2 numbers.

For eg: Net sales for Max 30000 vs Bea 43000

Need a sepearte column in which the difference is given
Please advise.
Welcome to the MrExcel board!

Instead of one example with no expected results, what about you give us 5-6 examples with expected results with XL2BB so we can copy to test with?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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