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

gifariz

Board Regular
Joined
May 2, 2021
Messages
120
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

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
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,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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