how to remove certain set of number and letters

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
563
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
The following code removes any special characters from Inputbox answer.
VBA Code:
Dim answ as Variant
Do
    answ = InputBox("Please enter the amount")

'cancel pressed
    If StrPtr(answ) = 0 Then Exit Sub
     answ = UCase(answ)
     If answ = "" Or IsNumeric(answ) Then
        MsgBox "Answer must not be left blank and must contain a letter." & vbCrLf & vbCrLf & "Your answer: " & answ, vbCritical, "Invalid Entry"
    Else
        Dim special_Characters As String
        Dim i As Long
      
        special_Characters = """<,.>?/:;{[}]|\+=_-)(*&^%$#@!'"
      
        For i = 1 To Len(special_Characters)
            answ = Replace(answ, Mid(special_Characters, i, 1), "")
            Debug.Print answ
          
            If i = Len(special_Characters) Then Exit Do
        Next
      
    End If
Loop
So how can I remove certain words that might be entered or combination of letters and numbers from the Inputbox string. Here's an example of what might be entered.
SA Allergy Medicine 15mg 10ct Bottle
So using the bold example entry I would only want to keep Allergy Medicine 15mg from that string and eliminate the rest.

Potential problems/issues to consider
  • The mg value can change but will always be in this format ###mg or ##mg (No spaces between the number and the letters of the unit of measure(mg) in this example)
  • The ct value can change but will always be in this format ###ct or ##ct (No spaces between the number and the letters of the unit of measure(ct) in this example)
  • Remove words from text like "SA" or "Bottle" in this example and not just the letters
Is this even possible? If so how?
Thank You for any help offered.
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Split String into 'word' array elements and compare with either another array or a combo of regex (for XXXct items) and unwanted words array.
 
Upvote 0
Is this even possible? If so how?
Thank You for any help offered.

etbd.png

Code:
// Table1
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    ETBD = Table.TransformColumns(Source, {{"raw", each Text.BetweenDelimiters(_, " ", " ", 0, 2), type text}})
in
    ETBD
 
Upvote 0
Since there is no control over what a user might enter into the input box, it would be very difficult to construct code which can accomodate all the variations of leading and trailing characters , or for that matter to determine where the significant characters to keep would begin.. For code to detect characters to delete and characters to keep, there must be either a distinguishable and consistent pattern of characters or a set criteria that can be compared against. Unfortunately, VBA does not have the AI capabilities to learn how to distinguish standard descriptive phrases which are embedded in other text simply by the spelling of the text. Those phrases would have to be listed somewhere so that a comparison could be made against text entered into an input box by a user. But if that list could be made, then it would be more efficient to use a ComboBox listing those items and let the user select from that rather than use the input box.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,948
Messages
6,122,420
Members
449,083
Latest member
Ava19

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