extracting number from text string and doing calculations

%3CLINGO-SUB%20id%3D%22lingo-sub-858068%22%20slang%3D%22en-US%22%3Eextracting%20number%20from%20text%20string%20and%20doing%20calculations%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-858068%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3EI%20have%20to%20extract%20the%20left%20most%20amount%20and%20the%20number%20of%20days%20from%20the%20below%20attached%20text%20string%20and%20perform%20mathematical%20operations%20depending%20on%20the%20number%20of%20days.%3C%2FSPAN%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CDIV%20class%3D%22cms_table%22%3E%3CTABLE%3E%3CTBODY%3E%3CTR%3E%3CTD%3ECondition%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3E%24280%20for%20days%201%20through%206%2C%20%240%20afterwards%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3E%24240%20for%20days%201%20through%203%2C%20%240%20afterwards%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3E%24290%20for%20days%201%20through%204%2C%20%2410%20afterwards%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3E%24280%20for%20days%201%20through%205%2C%20%2410%20afterwards%3C%2FTD%3E%3C%2FTR%3E%3C%2FTBODY%3E%3C%2FTABLE%3E%3C%2FDIV%3E%3CP%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23800000%22%3EFinal%20sum%20for%20all%20the%20below%20rows%20to%20be%20calculated%20according%20to%20logic.%3CBR%20%2F%3EAmount*4%20(%20If%20number%20of%20days%20if%20%26gt%3B%3D4)%3CBR%20%2F%3EAmount*number%20of%20days(%20If%20number%20of%20days%20is%20less%20than%204)%3CBR%20%2F%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CSPAN%3EExample%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E1)%20The%20first%20sum%20should%20be%20280*4%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E2)%20second%20sum%20should%20be%20240*3%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-858068%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-858091%22%20slang%3D%22en-US%22%3ERe%3A%20extracting%20number%20from%20text%20string%20and%20doing%20calculations%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-858091%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F406336%22%20target%3D%22_blank%22%3E%40vicasso%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPlease%20try%20this%20formula%20in%20cell%20B2%20and%20then%20drag%20it%20down%3A%3C%2FP%3E%3CPRE%3E%3DIF(MID(A2%2CSEARCH(%22gh%22%2CA2)%2B3%2CSEARCH(%22%2C%22%2CA2)-(SEARCH(%22gh%22%2CA2)%2B3))%2B0%26gt%3B%3D4%2C(LEFT(A2%2CSEARCH(%22%20%22%2CA2))%2B0)*4%2C(LEFT(A2%2CSEARCH(%22%20%22%2CA2))%2B0)*(MID(A2%2CSEARCH(%22gh%22%2CA2)%2B3%2CSEARCH(%22%2C%22%2CA2)-(SEARCH(%22gh%22%2CA2)%2B3))%2B0))%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHope%20that%20helps%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-858112%22%20slang%3D%22en-US%22%3ERe%3A%20extracting%20number%20from%20text%20string%20and%20doing%20calculations%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-858112%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F406336%22%20target%3D%22_blank%22%3E%40vicasso%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20may%20simply%20try%20the%20following%20User%20Defined%20Function.%20Place%20the%20following%20Function%20on%20a%20Standard%20Module%20like%20Module1%20and%20then%20use%20it%20on%20the%20Worksheet%20just%20like%20a%20regular%20Excel%20Formula.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3EFunction%20CustomProduct(ByVal%20str%20As%20String)%20As%20Long%0ADim%20Matches%20As%20Object%0ADim%20Amount%20%20As%20Long%0ADim%20Days%20%20%20%20As%20Long%0A%0AWith%20CreateObject(%22VBScript.RegExp%22)%0A%20%20%20%20.Global%20%3D%20False%0A%20%20%20%20.Pattern%20%3D%20%22%5E%5C%24(%5Cd%2B)%20for%20days%201%20through%20(%5Cd%2B)%22%0A%20%20%20%20%0A%20%20%20%20If%20.test(str)%20Then%0A%20%20%20%20%20%20%20%20Set%20Matches%20%3D%20.Execute(str)%0A%20%20%20%20%20%20%20%20Amount%20%3D%20Matches(0).submatches(0)%0A%20%20%20%20%20%20%20%20Days%20%3D%20Matches(0).submatches(1)%0A%20%20%20%20%20%20%20%20If%20Days%20%26gt%3B%3D%204%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20CustomProduct%20%3D%20Amount%20*%204%0A%20%20%20%20%20%20%20%20Else%0A%20%20%20%20%20%20%20%20%20%20%20%20CustomProduct%20%3D%20Amount%20*%20Days%0A%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20End%20If%0AEnd%20With%0AEnd%20Function%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EThen%20you%20may%20use%20it%20on%20the%20Worksheet%20like%20this...%3C%2FP%3E%3CP%3EIn%20B2%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3E%3DCustomProduct(A2)%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20710px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F132050i3557E9E600B25E97%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22CustomProduct.jpg%22%20title%3D%22CustomProduct.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-858183%22%20slang%3D%22en-US%22%3ERe%3A%20extracting%20number%20from%20text%20string%20and%20doing%20calculations%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-858183%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F35679%22%20target%3D%22_blank%22%3E%40Haytham%20Amairah%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIt%20doesn't%20seem%20to%20work%2C%20i%20guess%20we%20have%20to%20remove%20the%20%24%20symbol%20and%20also%20convert%20the%20text%20to%20number.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-858320%22%20slang%3D%22en-US%22%3ERe%3A%20extracting%20number%20from%20text%20string%20and%20doing%20calculations%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-858320%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F406336%22%20target%3D%22_blank%22%3E%40vicasso%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBoth%20the%20solutions%20work%20on%20the%20sample%20data%20you%20provided.%3C%2FP%3E%3CP%3EHow%20are%20the%20not%20working%3F%20Can%20you%20elaborate%20it%20a%20bit%20more%3F%20Both%20the%20solutions%20take%20care%20of%20the%20%24%20sign%20in%20the%20beginning%20of%20the%20strings.%3C%2FP%3E%3CP%3ECan%20you%20provide%20few%20more%20strings%20you%20are%20having%20trouble%20with%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-859115%22%20slang%3D%22en-US%22%3ERe%3A%20extracting%20number%20from%20text%20string%20and%20doing%20calculations%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-859115%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F406336%22%20target%3D%22_blank%22%3E%40vicasso%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20like%20the%20%3CSTRONG%3ERegEx%3C%2FSTRONG%3E%20approach%20and%20would%20identify%20it%20as%20having%20the%20greatest%20potential%20for%20extension%20to%20more%20complex%20cases.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDespite%20that%20I%20went%20ahead%20with%20a%20formula-based%20approach%20using%20worksheet%20functions%20and%20named%20formulas.%26nbsp%3B%20Picking%20up%20the%20notation%20implicit%20in%20the%20instructions%20in%20cell%20%3CSTRONG%3EC2%3C%2FSTRONG%3E%2C%20I%20defined%20'%3CSTRONG%3Enumber%3C%2FSTRONG%3E'%20and%20'%3CSTRONG%3Eamount%3C%2FSTRONG%3E'%20to%20be%3C%2FP%3E%3CP%3E%3CSTRONG%3E%3CFONT%3E%3D%20MID(Condition%2C%202%2C%20SEARCH(%22%20%22%2C%20Condition)%20-%202)%3C%2FFONT%3E%3C%2FSTRONG%3E%20and%3C%2FP%3E%3CP%3E%3CSTRONG%3E%3CFONT%3E%3D%20VALUE(%20MID(Condition%2C%20SEARCH(%22%2C%22%2C%20Condition)%20-%201%2C%201%20)%20)%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CFONT%3Erespectively.%26nbsp%3B%20Having%20tucked%20the%20messy%20steps%20out%20of%20sight%2C%20the%20formula%20is%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%3CFONT%3E%3D%20amount%20*%20IF(number%26lt%3B4%2C%20number%2C%204)%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CFONT%3EI%2C%20naturally%2C%20did%20this%20as%20a%20dynamic%20array%20formula%20but%20it%20is%20also%20possible%20to%20use%20CSE%20formulas%20or%20even%20relative%20referencing%20(given%20an%20appropriate%20definition%20of%20'%3CSTRONG%3ECondition%3C%2FSTRONG%3E')%20.%3C%2FFONT%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-860798%22%20slang%3D%22en-US%22%3ERe%3A%20extracting%20number%20from%20text%20string%20and%20doing%20calculations%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-860798%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F394231%22%20target%3D%22_blank%22%3E%40Subodh_Tiwari_sktneer%3C%2FA%3E%26nbsp%3B%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20for%20this%20awesome%20function.%20Can%20you%20please%20explain%20to%20me%20the%20functionality%20of%20it%20as%20i%20want%20to%20write%20similar%20functions%20for%20one%20of%20my%20projects.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20again%20%3CLI-EMOJI%20id%3D%22lia_slightly-smiling-face%22%20title%3D%22%3Aslightly_smiling_face%3A%22%3E%3C%2FLI-EMOJI%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-860882%22%20slang%3D%22en-US%22%3ERe%3A%20extracting%20number%20from%20text%20string%20and%20doing%20calculations%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-860882%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F406336%22%20target%3D%22_blank%22%3E%40vicasso%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20used%20Regular%20Expression%20which%20%3CSPAN%3Eis%20a%20special%20text%20string%20for%20describing%20a%20search%20pattern%3C%2FSPAN%3E%20to%20extract%20the%20numbers%20we%20are%20interested%20in.%3C%2FP%3E%3CP%3EIf%20you%20go%20to%20%3CA%20href%3D%22https%3A%2F%2Fregex101.com%2Fr%2FXItM9Q%2F1%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ethis%20page%3C%2FA%3E%2C%20it%20shows%20you%20how%20does%20it%20work.%3C%2FP%3E%3CP%3EI%20used%20the%20search%20pattern%20as%20%3CSTRONG%3E%5E%5C%24(%5Cd%2B)%20for%20days%201%20through%20(%5Cd%2B).%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3EThe%20special%20symbols%20used%20in%20the%20search%20pattern%20has%20the%20following%20meaning...%3C%2FP%3E%3CP%3E%5E%20--%26gt%3B%20Search%20from%20the%20beginning%20of%20the%20string.%3C%2FP%3E%3CP%3E%5C%24%20--%26gt%3B%20Literal%20character%20%24%3C%2FP%3E%3CP%3E%5Cd%20--%26gt%3B%20Match%20a%20digit%3C%2FP%3E%3CP%3E(%20)%20--%26gt%3B%20used%20for%20groupings%20i.e.%20if%20the%20pattern%20matches%20anything%2C%20the%20string%20returned%20will%20have%20two%20groups.%3C%2FP%3E%3CP%3E%2B%20--%26gt%3B%20Repeats%20the%20previous%20pattern%20one%20or%20more%20times%2C%20in%20this%20case%20one%20or%20more%20digits.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESo%20basically%20I%20am%20asking%20the%20RegEx%20engine%20to%20return%20me%20two%20sets%20of%20numbers%20in%20two%20different%20groups%20if%20the%20number%20in%20the%20first%20group%20is%20preceded%20by%20a%20%24%20sign%20and%20followed%20by%20a%20string%20%22%20for%20days%201%20through%26nbsp%3B%20%22%20which%20is%20then%20followed%20by%20another%20number.%3C%2FP%3E%3CP%3EAnd%20if%20the%20RegEx%20engine%20finds%20any%20matching%20string%20based%20on%20the%20search%20pattern%2C%20we%20fetch%20both%20the%20numbers%20in%20two%20variables%20called%20Amount%20and%20Days%20and%20do%20the%20calculation%20based%20on%20the%20criteria.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20may%20also%20find%20the%20following%20image%20helpful.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F132322iA1F88E42EE676CEA%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22regex%20tester.jpg%22%20title%3D%22regex%20tester.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional Contributor

I have to extract the left most amount and the number of days from the below attached text string and perform mathematical operations depending on the number of days.

Condition
$280 for days 1 through 6, $0 afterwards
$240 for days 1 through 3, $0 afterwards
$290 for days 1 through 4, $10 afterwards
$280 for days 1 through 5, $10 afterwards


Final sum for all the below rows to be calculated according to logic.
Amount*4 ( If number of days if >=4)
Amount*number of days( If number of days is less than 4)

Example
1) The first sum should be 280*4
2) second sum should be 240*3

7 Replies
Highlighted

@vicasso

 

Hi,

 

Please try this formula in cell B2 and then drag it down:

=IF(MID(A2,SEARCH("gh",A2)+3,SEARCH(",",A2)-(SEARCH("gh",A2)+3))+0>=4,(LEFT(A2,SEARCH(" ",A2))+0)*4,(LEFT(A2,SEARCH(" ",A2))+0)*(MID(A2,SEARCH("gh",A2)+3,SEARCH(",",A2)-(SEARCH("gh",A2)+3))+0))

 

Hope that helps

Highlighted

@vicasso 

 

You may simply try the following User Defined Function. Place the following Function on a Standard Module like Module1 and then use it on the Worksheet just like a regular Excel Formula.

 

 

 

Function CustomProduct(ByVal str As String) As Long
Dim Matches As Object
Dim Amount  As Long
Dim Days    As Long

With CreateObject("VBScript.RegExp")
    .Global = False
    .Pattern = "^\$(\d+) for days 1 through (\d+)"
    
    If .test(str) Then
        Set Matches = .Execute(str)
        Amount = Matches(0).submatches(0)
        Days = Matches(0).submatches(1)
        If Days >= 4 Then
            CustomProduct = Amount * 4
        Else
            CustomProduct = Amount * Days
        End If
    End If
End With
End Function

Then you may use it on the Worksheet like this...

In B2

 

=CustomProduct(A2)

 

CustomProduct.jpg

 

Highlighted

@Haytham Amairah 

 

It doesn't seem to work, i guess we have to remove the $ symbol and also convert the text to number. 

Highlighted

@vicasso 

Both the solutions work on the sample data you provided.

How are the not working? Can you elaborate it a bit more? Both the solutions take care of the $ sign in the beginning of the strings.

Can you provide few more strings you are having trouble with?

Highlighted

@vicasso 

I like the RegEx approach and would identify it as having the greatest potential for extension to more complex cases.

 

Despite that I went ahead with a formula-based approach using worksheet functions and named formulas.  Picking up the notation implicit in the instructions in cell C2, I defined 'number' and 'amount' to be

= MID(Condition, 2, SEARCH(" ", Condition) - 2) and

= VALUE( MID(Condition, SEARCH(",", Condition) - 1, 1 ) )

respectively.  Having tucked the messy steps out of sight, the formula is

= amount * IF(number<4, number, 4)

I, naturally, did this as a dynamic array formula but it is also possible to use CSE formulas or even relative referencing (given an appropriate definition of 'Condition') .

Highlighted

Hi @Subodh_Tiwari_sktneer ,

 

Thanks for this awesome function. Can you please explain to me the functionality of it as i want to write similar functions for one of my projects. 

 

Thanks again

Highlighted

@vicasso 

 

I used Regular Expression which is a special text string for describing a search pattern to extract the numbers we are interested in.

If you go to this page, it shows you how does it work.

I used the search pattern as ^\$(\d+) for days 1 through (\d+).

The special symbols used in the search pattern has the following meaning...

^ --> Search from the beginning of the string.

\$ --> Literal character $

\d --> Match a digit

( ) --> used for groupings i.e. if the pattern matches anything, the string returned will have two groups.

+ --> Repeats the previous pattern one or more times, in this case one or more digits.

 

So basically I am asking the RegEx engine to return me two sets of numbers in two different groups if the number in the first group is preceded by a $ sign and followed by a string " for days 1 through  " which is then followed by another number.

And if the RegEx engine finds any matching string based on the search pattern, we fetch both the numbers in two variables called Amount and Days and do the calculation based on the criteria.

 

You may also find the following image helpful.

regex tester.jpg