Get numbers only from each line in textbox

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,421
Office Version
  1. 2016
Platform
  1. Windows
I have a textbox in a userform which has multiple lines of values - the values are strings mixed of text and numbers, (the numbers are always in one block as below). I need to extract the numbers only and separate them with a slash if someone can assist please?

List example;

UK 789435 TEST
GB 648563 TEST2

Result example;

789435 / 648563

Thanks all :-)
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi
see if this will work for you

VBA Code:
Private Sub CommandButton1_Click()
    Dim strText()   As String, strNumber As String
    Dim Word        As Variant
    
    strText = Split(TextBox1.Text, " ")
    For Each Word In strText
        If IsNumeric(Word) Then strNumber = strNumber & Word & " / "
    Next
    
    MsgBox Mid(strNumber, 1, Len(strNumber) - 3)
    
End Sub

Dave

1729073302880.png
 
Upvote 0
Hi, thanks - that works!

The only issue I have is that I've just tested it with loads of numbers, one of which is '2900' and it's cutting the last zero off, leaving me with '290' - any idea why that would be?
 
Upvote 0
one of which is '2900' and it's cutting the last zero off, leaving me with '290' - any idea why that would be?

Rich (BB code):
MsgBox Mid(strNumber, 1, Len(strNumber) - 3)

Change value 3 shown to 2

Dave
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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