Extracting numbers from a text and multiplying them numbers eachother

cadandcode

Board Regular
Joined
Jan 21, 2023
Messages
125
Office Version
  1. 2019
Platform
  1. Windows
  2. Mobile
Hello, I checked most of the topics but couldnt find the exact formula.

AB
14 M 10 AD40
220 M 7 AD140

I would like to get B2 results just like that from A1 and A2 columns. Extract 4 and 10 from A1 and multiply them eachother in B1 (B1 is just an example) like I tried to describe above.
I found a vba code to extract 4 and 10 from that text cell into B1 like "410" but I couldnt find a way to multiply them as "=4*10" in B1 instead of putting them together like "410".
 
I have no idea what that was all about. Can you
- post a small set of sample data with XL2BB where the existing function does not produce the expected result
- tell us what the expected result is and why
Kitap3.xlsx
DEF
102.2 AD 4
114 M 10 AD2 M 2 AD
122 M 2 AD
136 AD 2 M
14
152 AD 6 M 3
16#DEĞER!
Sayfa1
Cell Formulas
RangeFormula
F16F16=SUMPROD(D10:F15)


Formula calculates the cells on only 1 column. Formula gives VALUE error when I change array to more than 1 columns. Also I would like to withdraw 1 cell from that formula array which formula includes. Like =(SUMPROD(A1:B4)-B3)
 
Upvote 0

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Glad you got XL2BB going, that should help.

Formula calculates the cells on only 1 column. Formula gives VALUE error when I change array to more than 1 columns.
Yes, the function was written for data as set out in all your samples given throughout the thread (single column) so not too surprising that a suggested solution gives an error for a different situation. ;)

You have not explained how data in multiple columns should be treated. For the sample data shown in post #61, what is the expected result and how did you get it calculating manually? Please set out the step/calculations clearly.


Also I would like to withdraw 1 cell from that formula array which formula includes. Like =(SUMPROD(A1:B4)-B3)
Could you post (XL2BB) a representative set of sample data for A1:B4, provide the expected result and explain how that expected result is calculated manually?
 
Upvote 0
Glad you got XL2BB going, that should help.


Yes, the function was written for data as set out in all your samples given throughout the thread (single column) so not too surprising that a suggested solution gives an error for a different situation. ;)

You have not explained how data in multiple columns should be treated. For the sample data shown in post #61, what is the expected result and how did you get it calculating manually? Please set out the step/calculations clearly.



Could you post (XL2BB) a representative set of sample data for A1:B4, provide the expected result and explain how that expected result is calculated manually?
In that sheet, as you see I would dont like to get 6*2 calculation on purpose, it is independant variable that I will choose which to add in the calculation wherever it is.

Kitap3.xlsx.xlsm
DEFGHI
102.2 AD 4
114 M 10 AD2 M 2 AD
122 M 2 AD
136 AD 2 M
14
152 AD 6 M 3
16=SUMPROD(D10:F15)
17what it should do -->=(2,2*4)+(4*10)+(2*2)+(2*2)+(2*6*3)
18
Sayfa1
 
Upvote 0
Thanks for the extra sample and calculation. See if this could work for you.

VBA Code:
Function SUMPROD(r As Range, Optional rExcl As Range) As Double
  Dim s As String
  
  s = Application.TextJoin("+", True, r)
  With CreateObject("VBScript.RegExp")
    .Global = True
    .Pattern = "([A-Z]+)(?=\+|$)"
    If r.Count = 1 Then
      s = .Replace(s & "+0", "")
    Else
      s = .Replace(s & "+0", "")
    End If
    .Pattern = "[A-Z]+"
    SUMPROD = Evaluate(.Replace(s, "*"))
    If Not rExcl Is Nothing Then SUMPROD = SUMPROD - SUMPROD(rExcl)
  End With
End Function

cadandcode.xlsm
DEF
102.2 AD 4
114 M 10 AD2 M 2 AD
122 M 2 AD
136 AD 2 M
14
152 AD 6 M 3
1692.8
Sheet2
Cell Formulas
RangeFormula
F16F16=SUMPROD(D10:F15,E13)
 
Upvote 0
@Peter_SSs Works great. I appreciate your help sir.
Sorry, I posted a draft code, not my final. Please try/use this instead.

VBA Code:
Function SUMPROD(r As Range, Optional rExcl As Range) As Double
  Dim s As String
  
  s = Application.TextJoin("+", True, r)
  With CreateObject("VBScript.RegExp")
    .Global = True
    .Pattern = "([A-Z]+)(?=\+|$)"
    s = .Replace(s & "+0", "")
    .Pattern = "[A-Z]+"
    SUMPROD = Evaluate(.Replace(s, "*"))
    If Not rExcl Is Nothing Then SUMPROD = SUMPROD - SUMPROD(rExcl)
  End With
End Function
 
Upvote 0
Solution
Kitap3.xlsx.xlsm
F
15337 -- 338
16675
Sayfa1
Cell Formulas
RangeFormula
F16F16=SUMPROD(F15)


Kitap3.xlsx.xlsm
F
14339 -- 340
15337 -- 338
161354
Sayfa1
Cell Formulas
RangeFormula
F16F16=SUMPROD(F14:F15)


Peter, it SUMed numbers.
 
Upvote 0
Peter, it SUMed numbers.
The function was written for the specifications you provided previously. This is a completely different data format. You can't just keep changing the requirements and expect suggestions to work.

Nor can you expect helpers to continually put effort into providing solutions if they are just going to be told they have wasted their time because the requirements have changed.
 
Upvote 0
The function was written for the specifications you provided previously. This is a completely different data format. You can't just keep changing the requirements and expect suggestions to work.

Nor can you expect helpers to continually put effort into providing solutions if they are just going to be told they have wasted their time because the requirements have changed.
I understand.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,193
Members
452,616
Latest member
intern444

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