pass text only from a textbox to a cell

rickblunt

Well-known Member
Joined
Feb 18, 2008
Messages
609
Office Version
  1. 2019
Platform
  1. Windows
Greetings, if I have a combination of text and numbers in a userform textbox, how would I tweak my code to ONLY send the text to a cell in a worksheet?

For example in my code snippet below the value in "deviceId" is "fire extinguisher 47". but I would like only the text "fire extinguisher" to be passed to the next blank cell in column B.

VBA Code:
.Range("B" & nextrow) = DeviceId.Value

I know how to do this in a formula, but I have never done it in code. I appreciate any assistance - thank you
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
You can use worksheet functions as Application.WorksheetFunction.<function>.....
 
Upvote 0
Yes, I know that this is one way but I should be able to do it simply by applying the desired format (parse it out) to my value as i pass it to the workbook. I did this on a app I made up years ago, because I needed to pass numbers over. It was just a single line added to the string. But I can't remember the exact syntax and I don't even remember where I used it exactly so i could look it up :/....
 
Upvote 0
This function should achieve what you want:

VBA Code:
Public Function Strip_Numbers(ByVal str As String) As String

    Dim oRegExp As Object
   
    Set oRegExp = CreateObject("vbscript.RegExp")
   
    oRegExp.Pattern = "\d"
    oRegExp.Global = True
   
    str = Trim(oRegExp.Replace(str, ""))
       
    Set oRegExp = Nothing

End Function

Usage Example:

VBA Code:
Debug.Print Strip_Numbers("fire extinguisher 47")
 
Upvote 0
Thanks Gokhan, I will try it out. I haven't used functions (though I understand why they are a good thing to use) that often so this will give me the impetus to start looking at these closer.
 
Upvote 0

Forum statistics

Threads
1,214,958
Messages
6,122,475
Members
449,087
Latest member
RExcelSearch

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