Results 1 to 18 of 18

Thread: [RESOLVED] Excell: converting comma-separated array

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    9

    Resolved [RESOLVED] Excell: converting comma-separated array

    Hello,

    A little "help me please" with Excell and VB.

    I have an array of comma-separated values posted in a single cell. It looks like something1, something2, something3, something4 etc. I have many cells across worksheet with same type of content, with different total and word lengths.

    I should have a new row for each value from that cell and post the value to separate cell, creating a column after all.

    I'm really "virgin" with VB, can I use a script to automate this process?

    Thank you in advance!

  2. #2
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Excell: converting comma-separated array

    arseniy:

    Look up the following in the Visual Basic HelpHeap:
    (Menu Bar: Tools > Macro > Visual Basic Editor > Help: "Split Function")

    Split Function

    Description
    Returns a zero-based, one-dimensional array containing a specified number of substrings.

    Syntax
    Split(expression[, delimiter[, limit[, compare]]])

    You can "Split" on the comma. Do you know how many values are in the CSV string in each cell? Can you determine which cells are populated with CSV strings? Do the separated values go on the same sheet or on a new sheet?

    What you are trying to do is very easy. Give us a few answers to these basic questions and I'm sure you will get a very comprehensive answer.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    9

    Re: Excell: converting comma-separated array

    Hello Webtest,

    >Do you know how many values are in the CSV string in each cell?

    No, it differs. The thing is, I tried to use EXCELL's "FIND" function to count how many commas are there in the syntax, but it seems it's a prohibited operation (returns error).

    >Can you determine which cells are populated with CSV strings?

    I thought about counting how many spaces are there, but it also returns only the first space found.

    >Do the separated values go on the same sheet or on a new sheet?

    The same sheet.

    >What you are trying to do is very easy.
    >Give us a few answers to these basic questions and I'm sure you will
    >get a very comprehensive answer.

    I hope so. To me it's like a new dimension.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excell: converting comma-separated array

    How are you importing the values? You can change the delimiter so when imported it will use the comma and place each in separate cells.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Excell: converting comma-separated array

    I think that this is what you need. In this code I am assuming that the cells with the comma separated values are in Row "A". If they are not, you will need to change the starting position. I have also included a sample file that shows my code in action.
    VB Code:
    1. Sub arseniy_commas()
    2. Const sSeparator As String = "," 'The value to be used to split the cells
    3.  
    4. Dim rngSource As Range
    5. Dim saResults() As String
    6. Dim lItemNum As Long
    7.  
    8.     'Start in cell A1
    9.     Set rngSource = ThisWorkbook.Worksheets(1).Range("A1")
    10.    
    11.     'Loop through all used cells in row A
    12.     Do While rngSource.Value <> ""
    13.         saResults = Split(rngSource.Value, sSeparator)
    14.        
    15.         'Loop through each item in the array
    16.         For lItemNum = LBound(saResults) To UBound(saResults)
    17.             'Writing the value to the next cell below the source cell
    18.             rngSource.Offset(1 + lItemNum, 0).Value = saResults(lItemNum)
    19.         Next lItemNum
    20.        
    21.         'Move to the next source cell
    22.         Set rngSource = rngSource.Offset(0, 1)
    23.     Loop
    24.  
    25.     Set rngSource = Nothing
    26. End Sub
    Attached Files Attached Files
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  6. #6
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Excell: converting comma-separated array

    Either Declan is a lot faster than me, or else he has nothing better to do!

    As for me, I have to WORK for a living. This may be similar to Declan's code:
    Code:
    Option Explicit
    Sub Macro1()
        'Split out Comma Separated Values from SELECTION into a new list
        Dim aRow As Long
        Dim aCol As Long
        Dim str_SplitArray() As String    
        ' Set the initial cell location for split data list (My TEST string was in B2)
        aRow = 3
        aCol = 1
        
        ' Loop through the data in the selected cell
        Do
            ' Split the long string into TWO pieces if possible
            str_SplitArray = Split(Selection.Value, ",", 2)
            'Write the split out value to the "New" area eliminating lead/trail whitespaces
            Cells(aRow, aCol).Value = Trim(str_SplitArray(0))
            'Check if there are ONE or TWO pieces to process
            If UBound(str_SplitArray) > 0 Then
                'TWO pieces ...
                'Write the remainder back on top of the original string
                Selection.Value = str_SplitArray(1)
            Else 'Only ONE piece is left
                'Clear out the source
                Selection.Value = ""
                'End the loop now
                Exit Do
            End If
            'Advance the Row or Column for the New Data
            aRow = aRow + 1
        Loop
    
    End Sub
    Last edited by Webtest; Mar 27th, 2006 at 01:17 PM. Reason: Redefined Dim for str_SplitArray() to "String" from "Variant"
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  7. #7
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Excell: converting comma-separated array

    Quote Originally Posted by Webtest
    Either Declan is a lot faster than me, or else he has nothing better to do!
    A little bit of both!

    I think either of our samples should be enough to get him started....
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  8. #8
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Excell: converting comma-separated array

    Declan ...

    I couldn't get the code to work with "Option Explicit" until I dimmed the array as "Variant". Does your code work under Option Explicit? Sometime I'll look more deeply into the data types in this operation.

    Edit: I got it working. I don't know what I was doing wrong ... just in a hurry ... I've got a meeting coming up shortly so it's back to the grindstone!
    Last edited by Webtest; Mar 27th, 2006 at 01:19 PM.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  9. #9
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Excell: converting comma-separated array

    arseniy ...

    Take a good look at RobDog's post. If you can import the data properly, you don't need any code at all.

    Also, my code barfs with a "Subscript out of range" error if your input string is terminated with a "," comma (there is no text after it). However, in this case the "UBound" function for the array Split returns a value of "-1". That will be an easy test.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  10. #10

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    9

    Re: Excell: converting comma-separated array

    Hello RobDog888,

    Quote Originally Posted by RobDog888
    How are you importing the values? You can change the delimiter so when imported it will use the comma and place each in separate cells.
    No, this isn't a .csv import, this is a buggy-secretary-girl-worksheet-after-several-years.

    The thing is, that it has a wrap-text cell with tons of comma-separated entries of values in each row.

    This is a date->visits worksheet. She entered it in the way: cell 1 : 05/11/2005 cell 2 : 123456, 12345, 123, 12354, etc., next row: cell 1 : 05/12/2005 cell 2 : ....

  11. #11

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    9

    Re: Excell: converting comma-separated array

    Hello again DKenny, Webtest,

    Thank you for your examples!

    A short question: to get the macros working for all cells in a column F, should it be a:

    VB Code:
    1. 'Start in cell F1
    2.     Set rngSource = ThisWorkbook.Worksheets(1).Range("F1")
    3.    
    4.     'Loop through all used cells in column F
    5.     Do While rngSource.Value <> ""
    6.         saResults = Split(rngSource.Value, sSeparator)
    7.  
    8.        
    9.         'Loop through each item in the array
    10.         For lItemNum = LBound(saResults) To UBound(saResults)
    11.             'Writing the value to the next cell below the source cell
    12.             rngSource.Offset(1 + lItemNum, 0).Value = saResults(lItemNum)
    13.         Next lItemNum
    14.        
    15.         'Move to the next source cell
    16.         Set rngSource = rngSource.Offset(1, 0)
    17.     Loop

    ?

    Like, Offset specifies the vector, while Range characterizes the start and end points?

    I'll test it with my worksheet tomorrow, I suppose it should solve my hassle!

    Cheers!

  12. #12
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Excell: converting comma-separated array

    Arseniy ...

    This sets the Starting Point (Cell) for the scan of the source data:

    Set rngSource = ThisWorkbook.Worksheets(1).Range("F1")

    This Stops the looping (walking down the column) when you encounter the first empty cell:

    Do While rngSource.Value <> ""

    Declan's code walks ACROSS the sheet in Row 1 finding new source cells to split ...

    Set rngSource = rngSource.Offset(0, 1)

    He puts the split out data into the column directly below each source data cell.

    If your Source data starts in Cell F1 as indicated, the following code will not work:

    rngSource.Offset(1 + lItemNum, 0)

    This will start cramming split out data into F2 and walk down from there! This will overwrite your data in column F, which I'm sure you do NOT want to do! However, you don't say where you DO want the data to go!

    You really need to define where the split out data is going to go, and describe that to us. You have finally stated the the source data is in Column F (and implied that it terminates at the bottom of the data with the first empty cell!). If you don't have a solid plan, there is no way you can write code!
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  13. #13
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Excell: converting comma-separated array

    I have many cells across worksheet with same type of content, with different total and word lengths.
    This is why I wrote the code to move across the row rather than down the column.

    The thing is, that it has a wrap-text cell with tons of comma-separated entries of values in each row.
    This implies that the data is down a row.
    If so, then you don't need any code. Just select the column and in the data menu select "Text to Columns" and select comma delimited.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  14. #14

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    9

    Re: Excell: converting comma-separated array

    Hello again DKenny, Webtest,

    Thanks again for your support.

    To get the situation clear, I attached an example of what I have and what I need to have.

    This is how far I got:

    I understood (but not much) the VBScript you advised and this is what I'm now fronting:

    1) I have to insert N blank rows after currently analyzed cell, where N = last element in array of separated sources

    2) I have to delete source row with comma-separated values after I have my parsing done

    Overall syntax and concepts misunderstanding makes me to irk for your help again, however, this is what I tried:

    VB Code:
    1. Option Explicit
    2.  
    3. Sub arseniy_commas()
    4. Const sSeparator As String = ", " 'The value to be used to split the cells
    5.  
    6. Dim rngSource As Range
    7. Dim saResults() As String
    8. Dim rowsnumber As Long
    9. Dim lItemNum As Long
    10.  
    11.     'Start in cell C3
    12.     Set rngSource = ThisWorkbook.Worksheets(1).Range("C1")
    13.    
    14.     'Loop through all used cells in Column C
    15.     Do While rngSource.Value <> ""
    16.        
    17.         saResults = Split(rngSource.Value, sSeparator)
    18.        
    19.         'HERE I tried to set rowsnumber = UBound(saResults) and get the number
    20.         'of rows to be insterted
    21.         'HERE I tried to use ThisWorkbook.Worksheets(1).addItem rowsbunmber
    22.         'to insert blank rows of the interest
    23.                      
    24.         'Loop through each item in the array
    25.         For lItemNum = LBound(saResults) To UBound(saResults)
    26.             'Writing the value to the next cell below the source cell
    27.             rngSource.Offset(1 + lItemNum, 0).Value = saResults(lItemNum)
    28.         Next lItemNum
    29.  
    30.          
    31.         'Move to the next source cell in column
    32.         Set rngSource = rngSource.Offset(1 + lItemNum, 0)
    33.  
    34.         'HOW To delete entire row where we've just have had the source?
    35.         'Like, ThisWorkbook.Worksheets(1).RemoveItem   ??
    36.         'But the addressing will be hurt, isn't it?
    37.  
    38.        
    39.     Loop
    40.  
    41.     Set rngSource = Nothing
    42. End Sub

    Thank you again for support,

    I haven't even realized yesterday that I would go so far.
    Attached Files Attached Files
    Last edited by arseniy; Mar 28th, 2006 at 04:33 AM.

  15. #15
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Excell: converting comma-separated array

    Do I understand this correctly ... You might have a comma separated string in cell F1 and another one in F2. You want to change the string in F1 into a vertical list starting at F1 and going down from there with each split value in a cell in the vertical list. Then, at the bottom of the list for F1, you want to continue the list going down with the split out values that were in F2 ... extending the list down. So ... now you will have a continuous list down Column F with all of the split out values from all of the Comma Separated Strings that were in Column F to begin with.

    Is this correct?

    If it is correct, what I would do is to use or insert a new column, say column G, and walk down column F splitting out values into column G. Once all of the values in Column F have been split into Column G, then I would just delete the old column F, and my Column G that is now the value list would automatically become Column F.

    Does this make sense, and is it what you are trying to do?

    Stay with us and do not worry about your English. It is evidently not your first language, but you are doing quite well!
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  16. #16

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    9

    Re: Excell: converting comma-separated array

    Hey,

    Thank you very much for your help guys!

    Keywords:

    cell, replace, Visual, Basic, Excell, delete, row, content, array, comma, separated, covert, automate

    Here is what I made in the end:

    VB Code:
    1. Option Explicit
    2.  
    3. Sub arseniy_commas()
    4. Const sSeparator As String = ", " 'The value to be used to split the cells
    5.  
    6. Dim rngSource As Range
    7. Dim saResults() As String
    8. Dim numberRows As Integer
    9. Dim lItemNum As Long
    10.  
    11.     'Start in cell C1
    12.     Set rngSource = ThisWorkbook.Worksheets(1).Range("C1")
    13.    
    14.     'Loop through all used cells in Column C
    15.     Do While rngSource.Value <> ""
    16.        
    17.         saResults = Split(rngSource.Value, sSeparator)
    18.        
    19.         numberRows = UBound(saResults)
    20.        
    21.         rngSource.Offset(1).EntireRow.Resize(numberRows).Insert
    22.         rngSource.EntireRow.Copy rngSource.EntireRow.Offset(1).Resize(numberRows).EntireRow
    23.        
    24.         On Error Resume Next
    25.                            
    26.         'Loop through each item in the array
    27.         For lItemNum = LBound(saResults) To UBound(saResults)
    28.             'Writing the value to the next cell below the source cell
    29.             rngSource.Offset(lItemNum, 0).Value = saResults(lItemNum)
    30.         Next lItemNum
    31.            
    32.  
    33.            
    34.         'Move to the next source cell in column
    35.         Set rngSource = rngSource.Offset(lItemNum, 0)
    36.        
    37.     Loop
    38.  
    39.     Set rngSource = Nothing
    40. End Sub

    Attached is the example of how it works.

    Cheers!
    Attached Files Attached Files

  17. #17
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Excell: converting comma-separated array

    arseniy
    Does this mean that it is now working as you need or is there another question?

    If it is working, can you mark this thread as resolved, (under the "Thread Tools" at the top of the page)
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  18. #18

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    9

    Re: [RESOLVED] Excell: converting comma-separated array

    Yep!

    Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width