Extract all numbers in an expression

ChewCS

New Member
Joined
Jun 17, 2022
Messages
15
Office Version
  1. 2016
Platform
  1. Windows
Hi, I would like to seek assistance from this forum in a certain task.

I have a cell with an expression that includes ranges of numbers. Something like "1-3,13,15-17". I would like to have a VBA function that extracts all the numbers within this single expression and store them in an array. Something like

array(1,1) is 1, array(1,2) is 2, array(1,3) is 3, array(1,4) is 13, array(1,5) is 15, array(1,6) is 16, array(1,7) is 17.

Is it possible?

Thanks in advance!
 
Change this line

arg = "'" & path & "[" & file & "]" & sheet & "'!" & ref

as

arg = "'" & path & "[" & file & "]" & sheet & "'!" & ref.address
Hi, I am getting a Run-time error "9", subscript out of range , and the highlighted line is

ary(Ro, Val(M(T))) = 1
 
Upvote 0

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Is the value is more than 18.
I think you will find that Val(M(T)) = 0 which out of range to ary.

The 3 changes I made to @kvsrinivasamurthy's code to get it to work for me were:
Added address per K
VBA Code:
arg = "'" & path & "[" & file & "]" & sheet & "'!" & ref.Address

Replaced FormulaArray with just Formula
VBA Code:
.Formula = "=" & arg

Added an If statement to ary(Ro, Val(M(T))) = 1.
Ideally we would exit both loops at this point but start with this.
Rationale: The formulas pulling in the data from the closed file return a Zero value when it runs out of data rows in the import data source.
The macro is using the value returned as weeks (1-18). ary is dimensioned to start at 1, the zero is out of range and in any case signifies the end of the data.

VBA Code:
        Else
            If M(T) <> 0 Then ary(Ro, Val(M(T))) = 1    ' ADDED IF STATEMENT
        End If
  
    Next T
 
Upvote 0
Change the for loop code as below.
VBA Code:
For Ro = 1 To UBound(S, 1)
  If S(Ro, 1) <> "" Then
    P = Split(Replace(S(Ro, 1), "-", ","), ",")
    M = Split(S(Ro, 1), ",")
    For T = 0 To UBound(M)
  
        If InStr(1, M(T), "-") > 0 Then
            N = Split(M(T), "-")
            For Ta = Val(N(0)) To Val(N(UBound(N)))
            ary(Ro, Ta) = 1
            Next Ta
        Else
            ary(Ro, Val(M(T))) = 1
        End If
    Next T
  End If
Next Ro
 
Upvote 0
Hi gurus, I've managed to run both your solutions! Thank you so much! As I can only provide one "Mark as solution", I hesitate to mark any. But I will edit my original post to indicate both are working. Once again, thank you so much for your kind assistance!! :)
 
Upvote 0
Hi, I would like to seek assistance from this forum in a certain task.

I have a cell with an expression that includes ranges of numbers. Something like "1-3,13,15-17". I would like to have a VBA function that extracts all the numbers within this single expression and store them in an array. Something like

array(1,1) is 1, array(1,2) is 2, array(1,3) is 3, array(1,4) is 13, array(1,5) is 15, array(1,6) is 16, array(1,7) is 17.

Is it possible?

Thanks in advance!
Looks like I can't edit my posts.

Post #34 by @kvsrinivasamurthy works

And Post #22 by @Alex Blakenburg works too.
 
Upvote 0
Thank you for testing and providing feedback on both options. The post references were a value add ;). Glad we were able to help.
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

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