+ Reply to Thread
Results 1 to 8 of 8

Create a : deliminated string from titles of array of data, depending on if a value exists

Hybrid View

mrswain Create a : deliminated string... 08-10-2012, 07:03 AM
jason.b75 Re: Create a : deliminated... 08-10-2012, 07:31 AM
mrswain Re: Create a : deliminated... 08-10-2012, 07:51 AM
jason.b75 Re: Create a : deliminated... 08-10-2012, 08:13 AM
mrswain Re: Create a : deliminated... 08-10-2012, 09:15 AM
jason.b75 Re: Create a : deliminated... 08-10-2012, 09:26 AM
stunn Re: Create a : deliminated... 08-10-2012, 09:24 AM
mrswain Re: Create a : deliminated... 08-10-2012, 09:43 AM
  1. #1
    Registered User
    Join Date
    08-07-2012
    Location
    Bristol, England
    MS-Off Ver
    Excel 2003
    Posts
    28

    Create a : deliminated string from titles of array of data, depending on if a value exists

    Hello,

    I have a problem, in that I have rows of data some with values in, some without and some rows with data in multiple colums. I need to create a string of data in a colon ( deliminated format with no colon at the end by checking if a row has any data in a specific colum, if it does take the column name/title and add it to the string of data which would sit in the variant name column in the example data I have attached. Where there is no data for a row in a specific column these need to be ignored and go onto the next colum.

    I also need it to be possible to expand the number of colums, either by editing any code or simply by incerting a column. The number of rows I would need to do this to would be aproximatly 10000.

    I would be very greatful for any help you can provide.

    Best regards,

    Paul
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: Create a : deliminated string from titles of array of data, depending on if a value ex

    I think I follow,

    Try this code

    Function aconcat(a As Variant, Optional sep As String = "") As String
      ' Harlan Grove, Mar 2002
      Dim y As Variant
    
      If TypeOf a Is Range Then
      For Each y In a.Cells
      aconcat = aconcat & y.Value & sep
      Next y
      ElseIf IsArray(a) Then
      For Each y In a
      aconcat = aconcat & y & sep
      Next y
      Else
      aconcat = aconcat & a & sep
      End If
    
      aconcat = Left(aconcat, Len(aconcat) - Len(sep))
    End Function
    With this formula, Array confirmed with Shift Ctrl Enter

    Formula: copy to clipboard
    =SUBSTITUTE(aconcat(IF(A2:E2<>"",A2:E2,),":")&":","0:","")

  3. #3
    Registered User
    Join Date
    08-07-2012
    Location
    Bristol, England
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Create a : deliminated string from titles of array of data, depending on if a value ex

    Hello Jason,

    Thanks once again for helping out with this.

    I'm not sure if I'm implimenting this code correctly as I am getting 0's for every row and a circular reference message.

    I have entered the first long code as a module for the spreadsheet or as a macro for the sheet1, and have enetered the formula in the to cells in column A, but post circular reference message (taht suggest I have probably set this up wrong) I just get 0 in all the A column cells for all rows.

    I dont quite know what you mean by the Shift Ctrl Enter confirms the array or when that should be pressed?

    Thanks for your help.

    Paul

  4. #4
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: Create a : deliminated string from titles of array of data, depending on if a value ex

    Hi Paul

    I thought that the formula was going the other side and that column A should be part of the result.

    =SUBSTITUTE(aconcat(IF(B2:E2<>"",B2:E2,),":")&":","0:","")

    Adding extra columns is where formula can start to get messy, If you're going to add new data to column F, or insert it between A and B then the formula will need to be made dynamic to allow for that, if you insert new columns between B and E then the ranges will expand automatically.

  5. #5
    Registered User
    Join Date
    08-07-2012
    Location
    Bristol, England
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Create a : deliminated string from titles of array of data, depending on if a value ex

    Hi Jason,

    Sorry about that yeah needed the results of the formula to go into the Column A.

    I understand about the expansion issues and will make sure if we do need to expand the range that we do so from within the range as aposed to adding on an end so to speak.

    I'm still experiancing a bit of a problem not sure if this is just something to do with how I've put it in or not but I just get a result in colum A of #VALUE!

    Is there a change required in the function code to acomodate the fact that the result needs to be in column A or is the colum reference irelevant to the function?

    Thanks once again for your help.

    Paul

  6. #6
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: Create a : deliminated string from titles of array of data, depending on if a value ex

    I would guess that it's not array confirmed.

    Select the cell with the formula, ** make sure that you only have one cell selected **

    Enter the cell as if you were going to edit the formula, then press Shift Ctrl and Enter simultaneously.

    If you do it correctly, the formula will show the correct result for that row, and the formula bar at the top will show the formula enclosed in curly braces.

    I did have a mistake in my last formula, it should be

    =SUBSTITUTE(aconcat(IF(B2:E2<>"",":"&$B$1:$E$1,"")),":","",1)

    which is also adjusted to take Steve's suggestion into consideration, as I'm in agreement with that having re-read your question.
    Last edited by jason.b75; 08-10-2012 at 09:38 AM.

  7. #7
    Registered User
    Join Date
    07-16-2012
    Location
    Newcastle upon Tyne
    MS-Off Ver
    Excel 2010
    Posts
    69

    Re: Create a : deliminated string from titles of array of data, depending on if a value ex

    Hi Paul, I read your question somewhat differently, try this:
    Function concatHeaders(hdrng As Range, rng As Range, Optional delim As String = "") As String
    Dim cll As Range
    
        concatHeaders = ""
        For Each cll In rng.Cells
            If cll.Value <> "" Then concatHeaders = concatHeaders & hdrng.Cells(1, cll.Column - rng.Column + 1).Value & delim
        Next cll
    
    If Len(concatHeaders) Then concatHeaders = Left(concatHeaders, Len(concatHeaders) - Len(delim))
    End Function
    Using this formula in A2:Ax
    =concatHeaders($B$1:$E$1,$B2:$E2,":")
    Which does not have to be array-entered.
    Last edited by stunn; 08-10-2012 at 09:30 AM.
    Steve D. a.k.a. Stephen Dunn

  8. #8
    Registered User
    Join Date
    08-07-2012
    Location
    Bristol, England
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Create a : deliminated string from titles of array of data, depending on if a value ex

    Hi Jason,

    Top bannana.

    Sorry yeah I think I must be haing a bad case of Friday brain, for some reason I struggled to explain what I was trying to achieve with this problem.

    The latest function works perfectly just the job thanks once again, thats bloody brilliant.

    Also thanks for explaining what is and how to get a confirmed array I'll remember this next time I need it.

    Thanks once again very much apreciated.

    ---------- Post added at 02:43 PM ---------- Previous post was at 02:39 PM ----------

    Sorry my appologies Stunn, Jason was coming back with lots very helpful responses and I assumed it was him that had posted the new function.

    Thank you very much, as that function works great Stunn.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1