How to eliminate characters from a textbox

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
564
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
VBA Code:
Private Sub txtbxPrdctName_Exit(ByVal Cancel As MSForms.ReturnBoolean)
   
    If Len(txtbxPrdctName.Value) > 2 Then
        Me.txtbxPrdctName.Value = StrConv(Me.txtbxPrdctName.Value, vbProperCase) & " " & Me.txtbxMgAmt.Value & " " & Me.txtbxCnt.Value
        Me.txtbxTYRD.Value = Replace(Me.txtbxPrdctName.Value, "8ct", " ")
    End If
   
End Sub
The question I have is with the code below:
Code:
Me.txtbxTYRD.Value = Replace(Me.txtbxPrdctName.Value, "8ct", " ")
The above code works and removes the "8ct" but I was wondering if there was a way to replace/remove any number entered if followed with the letters "ct". The potential problem is that number could vary from a one digit number to a three digit number. I tried the following codes but neither worked.
VBA Code:
Me.txtbxTYRD.Value = Replace(Me.txtbxPrdctName.Value, "###" & "ct", " ")
Me.txtbxTYRD.Value = Replace(Me.txtbxPrdctName.Value, "###ct", " ")

Thank you
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
How about this ....
Rich (BB code):
Private Sub txtbxPrdctName_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    With Me.txtbxPrdctName
        If Len(.Value) > 2 Then
            .Value = StrConv(.Value, vbProperCase) & " " & Me.txtbxMgAmt.Value & " " & Me.txtbxCnt.Value
            Me.txtbxTYRD.Value = Strip_Ct(.Value)
        End If
    End With
End Sub

using the function below
VBA Code:
Public Function Strip_Ct(ByVal argStr As String) As String

    Const ct    As String = "ct"
    Dim x       As Long
    Dim i       As Long

    x = InStr(1, argStr, ct, vbTextCompare)
    For i = x To 1 Step -1
        If Not IsNumeric(Mid(argStr, i - 1, 1)) Then
            Strip_Ct = Left(argStr, i - 1) & Right(argStr, Len(argStr) - x - 1)
            Exit For
        End If
    Next i
End Function
 
Upvote 0
You're welcome. On second thougt the previous code has a flaw. A TextBox value of "the actual price is 88ct." results in a TextBox value of "the aual price is ." I have solved this shortcoming below, although the result may still contain excess spaces.
VBA Code:
Public Function Strip_Ct(ByVal argStr As String) As String

    Const ct    As String = "ct"
    Dim x       As Long
    Dim i       As Long

    x = InStr(1, argStr, ct, vbTextCompare)
    For i = x To 1 Step -1
        If Not IsNumeric(Mid(argStr, i - 1, 1)) Then
            If x <> i Then
                Strip_Ct = Left(argStr, i - 1) & Right(argStr, Len(argStr) - x - 1)
                Exit For
            Else
                Strip_Ct = argStr
                Exit For
            End If
        End If
    Next i
End Function
 
Upvote 0
Your original code seems to be working as expected. No errors, but thank you for the update.
 
Upvote 0

Forum statistics

Threads
1,215,731
Messages
6,126,532
Members
449,316
Latest member
sravya

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