Results 1 to 4 of 4

Using TextJoin (Excel 2016) in VBA

Threaded View

ChemistB Using TextJoin (Excel 2016)... 02-21-2017, 04:07 PM
Keebellah Re: Using TextJoin (Excel... 02-21-2017, 04:34 PM
ChemistB Re: Using TextJoin (Excel... 02-21-2017, 06:11 PM
Keebellah Re: Using TextJoin (Excel... 02-21-2017, 06:52 PM
  1. #1
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Using TextJoin (Excel 2016) in VBA

    I tried a number of different approaches but can't seem to get this to work. ( I kept a few statements in there as comments to go back to in case there was an eureka moment.)

    I am copying unique records (relative to Head 1) from "First sheet" to "Final." Normally, I just want to copy the first instance for each cell. (Code works fine for that)
    However, if the header is "Aligned" or "Global", I want all instances to be copied to the cell.
    Here is my code and an example workbook attached. Thanks in advance.
    Sub test()
    Dim DataRng As Range
    Dim i As Integer, j As Integer, k1 As Integer, k_final As Integer
    Dim LstCol As Integer, LR As Integer
    Dim Mytext As String
    
    Set WSF = Application.WorksheetFunction
    Set DataRng = Sheets("First sheet").Range("A1:K10")
    LstCol = 11
    LR = 4
    
    For i = 2 To LstCol
        If Sheets("Final").Cells(1, i) = "Aligned" Or Sheets("Final").Cells(1, i) = "Global" Then
            Mytext = ""
            For j = 2 To LR
                k1 = WSF.Match(Sheets("Final").Cells(j, 1), WSF.Index(DataRng, 0, 1), 0)
                k_final = k1 + WSF.CountIf(WSF.Index(DataRng, 0, 1), Sheets("Final").Cells(j, 1)) - 1
    '            Mytext = WSF.TextJoin("," & Chr(10), True, Sheets("First sheet").Range(Cells(k1, i) & ":" & Cells(k_final, i)))
    '            Mytext = WSF.TextJoin("," & Chr(10), True, Sheets("First sheet").Range(Cells(k1, i), Cells(k_final, i)))
                Mytext = WSF.TextJoin("," & Chr(10), True, WSF.Index(DataRng, k1, i) & ":" & WSF.Index(DataRng, k_final, i))
    
                Sheets("Final").Cells(j, i) = Mytext
            Next j
        Else
            For j = 2 To LR
                Sheets("Final").Cells(j, i).Value = WSF.Index(DataRng, WSF.Match(Sheets("Final").Cells(j, 1), WSF.Index(DataRng, 0, 1), 0), i)
                
            Next j
        End If
    Next i
    
    End Sub
    Attached Files Attached Files
    Last edited by ChemistB; 02-21-2017 at 06:10 PM.
    ChemistB
    My 2?

    substitute commas with semi-colons if your region settings requires
    Don't forget to mark threads as "Solved" (Edit First post>Advanced>Change Prefix)
    If I helped, Don't forget to add to my reputation (click on the little star at bottom of this post)

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 23
    Last Post: 01-03-2022, 10:45 AM
  2. Removing duplicates from a TEXTJOIN
    By CKPHarry in forum Excel General
    Replies: 5
    Last Post: 01-02-2020, 05:57 PM
  3. TEXTJOIN only specific words
    By thoart in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 02-11-2017, 07:56 AM
  4. If Match then return TEXTJOIN
    By Cynops in forum Excel General
    Replies: 1
    Last Post: 11-09-2016, 07:51 AM
  5. TEXTJOIN function
    By Tony Valko in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 08-13-2016, 06:48 PM
  6. Replies: 2
    Last Post: 05-24-2016, 10:43 PM
  7. Excel 2016 & beyond!
    By jewelsharma in forum The Water Cooler
    Replies: 6
    Last Post: 01-10-2016, 05:53 AM

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