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".
 
Glad we could help & thanks for the feedback.
 
Upvote 0

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
You cannot use multiple cell in filterxml like that.
 
Upvote 0
Maybe try this then
VBA Code:
Sub Number_Multiplier()
        Dim Regex As Object
        Set Regex = CreateObject("vbscript.regexp")
        Dim k As Integer
        Regex.Global = True
        Regex.Pattern = "[0-9]+"
        lr = Range("A" & Rows.Count).End(xlUp).Row
        Dim mc As Object
        Dim S
        Dim NumRes As Double
       
        For k = 1 To lr
                Set mc = Regex.Execute(Range("A" & k))
                For S = 0 To mc.Count - 1
                        If S = 0 Then
                        NumRes = mc(0)
                        Else: NumRes = NumRes * mc(S)
                        End If
                Next S
                Range("B" & k) = NumRes
        Next k
       
       
End Sub
@Fluff is it possible to add an array controller here? it calculates everysingle cells, I need some cells in sheet to be calculated.
 
Upvote 0
You would need to ask the person who wrote the code.
 
Upvote 0
I created a function out it, to install it paste it in the vba editor and type =numbermultiplier in the worksheet and point to the cell.

VBA Code:
Function NumberMultiplier(rng As Range)
       
        Dim Regex As Object
        Set Regex = CreateObject("vbscript.regexp")
        Regex.Global = True
        Regex.Pattern = "[0-9]+"
       
        Dim mc As Object
        Dim S
        Dim NumRes As Double
      
       
        Set mc = Regex.Execute(rng.Value)
               
                For S = 0 To mc.Count - 1
                        If S = 0 Then
                        NumRes = mc(0)
                        Else: NumRes = NumRes * mc(S)
                        End If
                Next S
       
        NumberMultiplier = NumRes

      
      
End Function
 

Attachments

  • 1674410901194.png
    1674410901194.png
    7.6 KB · Views: 9
Upvote 0
Another user-defined function to consider.
I have assumed that the data will be in a column and that the range fed into the function will be more than one cell.

VBA Code:
Function SUMPROD(r As Range)
  With CreateObject("VBScript.RegExp")
    .Global = True
    .Pattern = "([A-Z]+)(?=\+|$)"
    SUMPROD = .Replace(Join(Application.Transpose(r), "+") & "+0", "")
    .Pattern = "[A-Z]+"
    SUMPROD = Evaluate(.Replace(SUMPROD, "*"))
  End With
End Function

cadandcode.xlsm
AB
1
24 M 10 AD287.3804
320 M 7 AD
4
513 M 2 AD
64 M 10 AD 2 KG
73.4 M 2.03 AD 0.2 KG
8
9
10
Sheet1
Cell Formulas
RangeFormula
B2B2=SUMPROD(A2:A10)
 
Upvote 0
Not sure why none of your codes worked, pasted vba into table and used function but nothing happened.
@Peter_SSs @shinigamilight
Note: I tried to paste it via ctrl shift enter
 

Attachments

  • Adsız3.png
    Adsız3.png
    78.7 KB · Views: 9
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