Extracting Comma Values from a String, Loading into Array

MacroEcon1337

Board Regular
Joined
Mar 16, 2017
Messages
65
I have a single column of values on an excel spreadsheet.

The contents of each individual cell correspond to Gate Codes, and are inputted exactly like the red text below:

example 1: 4
example 2: 4,6
example 3: 2,6,7
example 4: 5,12,1

Note: there will NEVER be a triple digit number - just 1-2 digit numbers (separated by commas).


The problem is I cannot figure out how to extract JUST the individual numbers (without the commas) -- and store them into an array. I thought I solved it, but then it failed on double digit numbers.


Not entirely relevant - but my end goal/purpose here is to subsequently loop through the array contents, then take certain actions based on their values. Any assistance greatly appreciated.

MC
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Very generically, this code would look at each example in your post and put each example into an array named "spl". This is assuming that the examples as posted start in cell A1 and continue down that column.

Code:
Sub test()


    Dim arr, spl
    Dim lRow As Long, i  As Long
    
    lRow = Cells(Rows.Count, 1).End(xlUp).Row
    arr = Range("A1:A" & lRow)
    For i = LBound(arr) To UBound(arr)
        spl = Split(arr(i, 1), ",")
    Next
    
End Sub
 
Upvote 0
Code:
Sub test()


    Dim arr, spl
    Dim lRow As Long, i  As Long
    
    lRow = Cells(Rows.Count, 1).End(xlUp).row
    arr = Range("A1:A" & lRow)
    
    For i = LBound(arr) To UBound(arr)
        spl = Split(arr(i, 1), ",")
       
    Next
   
   MsgBox spl(2)
   MsgBox arr(1)
     
End Sub



1. TY for the response. I tried the solution to no avail. Both of the MsgBox commands (see above - appended to the end of your code) result in "Subscript out of range" errors.

2. The key is that each individual cell has a "set" of multiple values (separated by commas). And I have to extract each set in

3. The pseudocode I see is:


Get current cell value (2,15,7)

Load myArray(1) = 2
Load myArray(2) = 15
Load myArray(3) = 7


At this point - I can look through myArray, perform some actions, clear myArray, move on to the next cell. Is there a way to modify the code to meet this goal? TY for the idea and help so far,

MC
 
Upvote 0
1. TY for the response. I tried the solution to no avail. Both of the MsgBox commands (see above - appended to the end of your code) result in "Subscript out of range" errors.
The lower bound for any array created by the Split function is always 0, so the upper bound for the array would always be the count of the number of elements in the array minus one.
 
Last edited:
Upvote 0
Does this produce the array you want?
Code:
Sub test()


    Dim arr, sStr As String
    Dim lRow As Long, i  As Long
    
    lRow = Cells(Rows.Count, 1).End(xlUp).Row
    sStr = Join(Application.Transpose(Range("A1:A" & lRow)), ",")
    arr = Split(sStr, ",")
    For i = LBound(arr) To UBound(arr)
        MsgBox arr(i)
    Next i
     
End Sub
 
Upvote 0
Does this produce the array you want?
Code:
Sub test()


    Dim arr, sStr As String
    Dim lRow As Long, i  As Long
    
    lRow = Cells(Rows.Count, 1).End(xlUp).Row
    sStr = Join(Application.Transpose(Range("A1:A" & lRow)), ",")
    arr = Split(sStr, ",")
    For i = LBound(arr) To UBound(arr)
        MsgBox arr(i)
    Next i
     
End Sub

JoeMo - Worked like a charm, thank you!

Also Rick R - ty for the array bounding explanation.
 
Upvote 0
Does this produce the array you want?
Code:
Sub test()


    Dim arr, sStr As String
    Dim lRow As Long, i  As Long
    
    lRow = Cells(Rows.Count, 1).End(xlUp).Row
    sStr = Join(Application.Transpose(Range("A1:A" & lRow)), ",")
    arr = Split(sStr, ",")
    For i = LBound(arr) To UBound(arr)
        MsgBox arr(i)
    Next i
     
End Sub


Ah okay - one final thing, which I know I could eventually figure out on my own, so feel free to disregard me. Is there a way to tweak it, so that it ONLY returns the values for one cell (ie A1 or A2 or A3)? Otherwise, I have no way of knowing which values came from which cells (since they all message box at once).

My plan was to either clear, or erase and redim the array for each row. So we can pretend there is only one cell from which to extract the values -- then I will figure out how to work it into my loop.

EDIT: I DID try valiently by remove the "transpose" and hardcode "A1", but it did not work :)
 
Last edited:
Upvote 0
Ah okay - one final thing, which I know I could eventually figure out on my own, so feel free to disregard me. Is there a way to tweak it, so that it ONLY returns the values for one cell (ie A1 or A2 or A3)? Otherwise, I have no way of knowing which values came from which cells (since they all message box at once).

My plan was to either clear, or erase and redim the array for each row. So we can pretend there is only one cell from which to extract the values -- then I will figure out how to work it into my loop.

EDIT: I DID try valiently by remove the "transpose" and hardcode "A1", but it did not work :)
I don't understand why you care which array element came from which cell. The transpose is an array whose individual elements contain the values for an individual cell - try the modified code below.
Code:
Sub test()


    Dim arr, sStr As String, arrByCell
    Dim lRow As Long, i  As Long
    
    lRow = Cells(Rows.Count, 1).End(xlUp).Row
    arrByCell = Application.Transpose(Range("A1:A" & lRow))
    sStr = Join(arrByCell, ",")
    arr = Split(sStr, ",")
    For i = LBound(arr) To UBound(arr)
        MsgBox arr(i)
    Next i
    'readout cell by cell sequentially
    For i = LBound(arrByCell) To UBound(arrByCell)
        MsgBox arrByCell(i)
    Next i
     
End Sub
 
Upvote 0
I don't understand why you care which array element came from which cell. The transpose is an array whose individual elements contain the values for an individual cell - try the modified code below.
Code:
Sub test()


    Dim arr, sStr As String, arrByCell
    Dim lRow As Long, i  As Long
    
    lRow = Cells(Rows.Count, 1).End(xlUp).Row
    arrByCell = Application.Transpose(Range("A1:A" & lRow))
    sStr = Join(arrByCell, ",")
    arr = Split(sStr, ",")
    For i = LBound(arr) To UBound(arr)
        MsgBox arr(i)
    Next i
    'readout cell by cell sequentially
    For i = LBound(arrByCell) To UBound(arrByCell)
        MsgBox arrByCell(i)
    Next i
     
End Sub


Nailed it - thank you!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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