+ Reply to Thread
Results 1 to 5 of 5

Output selection to a single string

Hybrid View

  1. #1
    Registered User
    Join Date
    12-17-2008
    Location
    Malaysia
    Posts
    12

    Output selection to a single string

    I have multiple rows containing list of serials as below.

    A2004 / A2018 / A2038 / A2122 / A2166

    I would like to format these serials in such that they are concatenated into a single string with each serials separated by a comma and the results shall be positioned at the last column of each row. Also the string must have an (M&E) text at the end. So the result should look like this.

    A2004, A2018, A2038, A2122, A2166 (M&E)

    Below is the code for input from a single column instead of rows. How do I tweak to get my desired result.

    Sub test()
    For Each UsrCell In Selection
    a = a & UsrCell.Value & ";"
    Next UsrCell
    a = Left(a, Len(a) - 1)
    Open "d:\work\testfile.txt" For Output As #1 'Change your path here
    Print #1, a;
    Close #1
    End Sub

  2. #2
    Forum Contributor
    Join Date
    04-01-2009
    Location
    Irvine, CA
    MS-Off Ver
    Excel 2010
    Posts
    280

    Re: Output selection to a single string

    It looks like you can just add to your final "a" string...

    a = Left(a, Len(a) - 1) & " (M&E)"

  3. #3
    Registered User
    Join Date
    12-17-2008
    Location
    Malaysia
    Posts
    12

    Re: Output selection to a single string

    Hi Chance 2. Thanks for your reply. I'm relatively new to VBA and will need further explanation. I would like to have a button to perform the task and input the results in Column K of my attachment. Kindly help.
    Attached Files Attached Files

  4. #4
    Forum Contributor
    Join Date
    04-01-2009
    Location
    Irvine, CA
    MS-Off Ver
    Excel 2010
    Posts
    280

    Re: Output selection to a single string

    This code will add the data to column K

    Sub test()
    Dim iRow As Long
    Dim iCol As Long
    
    iRow = 2
    
    Do Until IsEmpty(Range("A" & iRow))
        Range("K" & iRow) = ""
        For iCol = 1 To 10
            If Len(Cells(iRow, iCol)) > 0 Then
                Range("K" & iRow) = Range("K" & iRow) & "," & Cells(iRow, iCol)
            End If
        Next
        Range("K" & iRow) = Mid(Range("K" & iRow), 2) & " (M&E)"
        iRow = iRow + 1
    Loop
    
    End Sub

  5. #5
    Registered User
    Join Date
    12-17-2008
    Location
    Malaysia
    Posts
    12

    Re: Output selection to a single string

    Thanks Chance2. Made some tweaks and works like a charm.

+ 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