VBA code to get an array of numeric values from a string

gifariz

Board Regular
Joined
May 2, 2021
Messages
112
Office Version
  1. 365
Platform
  1. Windows
As in post title, I need a VBA code (can be sub or function) to get an array of numeric values given a string input.

Suppose given a string variable: "H (120x150x250)x12.5x15/10",
desired output would be a double array with elements of 120, 150, 250, 12.5, 15, 10.

The string will have random format (unknown delimiter), so Split function can't be used.
I think hard but my brain can't find effective way to do this.
Any help guys? No need to be complete code, simply idea would be helpful.
TIA.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
See if this is heading in the right direction (though I did use Split :))

VBA Code:
Sub StringToArray()
  Dim RX As Object
  Dim s As String
  Dim ary As Variant
  
  s = "H (120x150x250)x12.5x15/10"
  Set RX = CreateObject("VBScript.RegExp")
  RX.Global = True
  RX.Pattern = "[^\d.]"
  ary = Split(Application.Trim(RX.Replace(s, " ")))
End Sub
 
Upvote 0
Try this code
VBA Code:
Public Function ExtractNumber(ByVal s As String) 
Dim i As Long
For i = 1 To Len(s)
    If Mid(s, i, 1) Like "[0-9.]" = False Then Mid(s, i, 1) = " "
Next i
    ExtractNumber = Split(Application.Trim(s), " ")
End Function
 
Last edited:
Upvote 0
Solution
Thank you Peter & Phuoc. Sorry for my late reply.

Both of your code works (I just need to CDbl the result). I choose Phuoc because it has no extra object (I don't get used with object yet).
The idea of replacing unwanted character with delimiter would make me able to make a working code, but I also learnt many things from both of your answers:
- 'Like' in If
- Patterns
- applying Mid (and probably many other functions) on left side of the equal operator
- VBScript.RegExp library
I will explore more about this

Thank you all
 
Upvote 0
You're welcome. Glad we could help. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,733
Members
448,987
Latest member
marion_davis

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