Formula Help Please

SARABECK

Board Regular
Joined
Jan 5, 2012
Messages
132
Hi Everyone,
I have truck ids that comes through the ledger with their appropriate sales and purchase dollars. The dollars should offset when a sale and purchase IDs are the same. However, the issues i'm running into is spaces in between, zeros in between, letters in the beginning and letters in the end for these truck IDs.

I use the 2 formulas below to help but it is not capturing everything.

Formula 1 - To remove all the D in front.
Code:
=IF(LEFT(B2,2)="D*",RIGHT(B2,LEN(B2)-2),IF(RIGHT(B2,1)="D",LEFT(B2,LEN(B2)-1),B2))

Formula 2 - To remove extra spaces in between.
Code:
=SUBSTITUTE(C8," ","")



Truck IDsFormula 1Formula 2Problems
SaleACL123ACL123Letter in the Front
PurchD*ACL123ACL123Letter in the Front
SaleABC 329ABC 329ABC329Letter in the Front and SPACE - No Match
PurchD*ABC329ABC329ABC329Letter in the Front and SPACE - No Match
SaleABC 35060BABC 35060BABC35060BLetter in the Front, SPACE and LETTER B - No Match
PurchD*ABC35060ABC35060ABC35060Letter in the Front, SPACE and LETTER B - No Match
SalePVHC20BPVHC20BPVHC20BLetter in the Front and in the end - No Match
PurchD*PVHC20PVHC20PVHC20Letter in the Front and in the end - No Match
SaleCC 01116CC 01116CC01116Letter in the Front, SPACE and ZERO in between
PurchD*CC1116CC1116CC1116Letter in the Front, SPACE and ZERO in between
SaleGOPV 036BGOPV 036BGOPV036BLetter in the Front, SPACE, ZERO IN BETWEEN (need to remove B)
PurchD*GOPV36BGOPV36BGOPV36BLetter in the Front, SPACE, ZERO IN BETWEEN (need to remove B)
SalePVBL0099PVBL0099PVBL0099Letter in the Front, Zeros in Between
PurchD*PVBL99PVBL99PVBL99Letter in the Front, Zeros in Between

<tbody>
</tbody>


Thank you.

Please delete my other "Formula Needed" topic. I submitted this again as my original topic was not loading probably. My apologizes.
 
Last edited:
Peter_SSs, Thank you so much! Just awesome! thank you!!.
Glad it worked well for you. :)


I would like to apply this to my Rail IDs, the concept is the same except the Rail IDs starts with B in stead of D.
It's a guess without seeing some sample data, but you have definitely changed too many "D"s to "B"s. If this doesn't work, please post some representative data and expected results in 2 columns like my screen shot in post #9.

Rich (BB code):
Function RailID(s As String) As String
  With CreateObject("VBScript.RegExp")
    .Pattern = "^(B\*)?(\D+)(0*)(\d+)(\D)?$"
    RailID = .Replace(Replace(s, " ", ""), "$2$4")
  End With
End Function
 
Upvote 0

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Thank you Peter_SSs. How come you didn't change all of the Ds to Bs?
Can you please add comments in the coding so i can use elsewhere too.
thank you so much.
 
Last edited:
Upvote 0
Thank you Peter_SSs. How come you didn't change all of the Ds to Bs?
Can you please add comments in the coding so i can use elsewhere too.
thank you so much.
I can't add a lot by way of comments as there isn't actually much code in the function. Also, the function uses 'Regular Expressions' which can be quite complex to understand and any sort of introduction to Regular Expressions is beyond what could be expected in a forum like this.

Never-the-less I will try to give an outline of how this function, in particular its pattern, works.
Regular expressions look for a 'pattern', defined by the code, in a string.
In a RegEx pattern () divide the pattern into sections and certain characters have special meaning as well as their own (literal) meaning. To identify whether a character represents itself or a special meaning, that character can be preceded by a "\" or not.

The pattern in this RailID function works like this, after any spaces are first removed from the string.

^(B\*)?(\D+)(0*)(\d+)(\D)?$

^ represents the beginning of the string
followed by
(B\*)? the actual letter B followed by an asterisk. The "?" means that the preceding section may or may not occur (once) in the string
followed by
(\D+) \D represents a non-digit, "+" means one or more times
followed by
(0*) the digit zero, "*" means zero or more times.
followed by
(\d+) \d represents a digit, "+" means one or more times
followed by
(\D)? \D represents a non-digit, "?" means zero or 1 times
followed by
$ the end of the string

Finally, the .Replace(...., "$2$4") says that if the pattern is found, then return the 2nd and 4th sections. They are the red sections, being a series of 1 or more non-digits followed by a series of 1 or more digits.

Hopefully from that you can see why not all the "D"s were changed to "B"s. Most of them were preceded by a "\" giving their special meaning as either a non-digit or a digit.
 
Upvote 0

Forum statistics

Threads
1,217,356
Messages
6,136,078
Members
449,988
Latest member
Mabbas

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