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
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Are the original values actual numbers or text?
Does this user-defined function do what you want?

VBA Code:
Function Subtr(r1 As Range, r2 As Range) As Variant
  Select Case True
    Case InStr(1, r1.Text, "/") > 0
      Subtr = (Split(r2.Text, "/")(0) - Split(r1.Text, "/")(0)) & "/" & (Split(r2.Text, "/")(1) - Split(r1.Text, "/")(1))
    Case Right(r1.Text, 1) = "%"
      Subtr = Format(r2 - r1, "0.0%")
    Case Else
      Subtr = r2 - r1
  End Select
End Function

Wafee 2020-08-20 1.xlsm
ABC
1506010
265%50%-15.0%
373/6578/605/-5
462/7060/65-2/-5
Subtract
Cell Formulas
RangeFormula
C1:C4C1=Subtr(A1,B1)
 
Upvote 0
Are the original values actual numbers or text?
Does this user-defined function do what you want?

VBA Code:
Function Subtr(r1 As Range, r2 As Range) As Variant
  Select Case True
    Case InStr(1, r1.Text, "/") > 0
      Subtr = (Split(r2.Text, "/")(0) - Split(r1.Text, "/")(0)) & "/" & (Split(r2.Text, "/")(1) - Split(r1.Text, "/")(1))
    Case Right(r1.Text, 1) = "%"
      Subtr = Format(r2 - r1, "0.0%")
    Case Else
      Subtr = r2 - r1
  End Select
End Function

Wafee 2020-08-20 1.xlsm
ABC
1506010
265%50%-15.0%
373/6578/605/-5
462/7060/65-2/-5
Subtract
Cell Formulas
RangeFormula
C1:C4C1=Subtr(A1,B1)

Hi Peter,

Original data will have both text and numbers. The function you have provide should do but can you help me with the code that does the above action using values in A and B column and produces result in C. It should do until last row of data. Thank you in advance.
 
Upvote 0
can you help me with the code that does the above action using values in A and B column and produces result in C. It should do until last row of data.
Try this with a copy of your workbook.

VBA Code:
Sub SubtractThem()
  Dim r As Range
  Dim s1 As String, s2 As String
  
  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
          r.Offset(, 2).Value = (Split(s2, "/")(0) - Split(s1, "/")(0)) & "/" & (Split(s2, "/")(1) - Split(s1, "/")(1))
        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
 
Upvote 0
t
Try this with a copy of your workbook.

VBA Code:
Sub SubtractThem()
  Dim r As Range
  Dim s1 As String, s2 As String
 
  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
          r.Offset(, 2).Value = (Split(s2, "/")(0) - Split(s1, "/")(0)) & "/" & (Split(s2, "/")(1) - Split(s1, "/")(1))
        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
This perfectly works, thank you:)(y)
 
Upvote 0
You're welcome. Thanks for the follow-up. :)
Hi Peter,

Can you help me with two more requirements with the above code!!
1. Need a line of code that does the subtraction for 3 numbers which are in the format 50/60/70. (Eg: result for 50/60/70 and 55/58/72 should be 5/-2/2)

2. I have few headers in the middle of the sheet with names as "Past(column A)" and "Present(column B)". The code should ignore these headers while doing the substraction. These will be be there in A and B columns in multiple places.

Thank you in advance.
 
Upvote 0
Need a line of code that does the subtraction for 3 numbers which are in the format 50/60/70
Is this a separate question or are such values mixed up among the 2-number values like below?

The code would already ignore any header rows (unless those header values contained a "/" or "%" character).

Wafee 2020-08-20 1.xlsm
AB
25060
365%50%
473/6578/60
550/60/7055/58/72
662/7060/65
Subtract
 
Upvote 0
Is this a separate question or are such values mixed up among the 2-number values like below?

The code would already ignore any header rows (unless those header values contained a "/" or "%" character).

Wafee 2020-08-20 1.xlsm
AB
25060
365%50%
473/6578/60
550/60/7055/58/72
662/7060/65
Subtract

That's right, it is mixed up among 2 number values.
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

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