将一列的文本与另一列

时间:2017-08-22 01:50:10

标签: vba excel-vba excel

我是VBA的初学者。

我在下面附上一张图片来说明我的问题。

excel screenshot

我希望将第一列中的玩家名称连接到我在每个列(B到D)中列出的所有结果,即:Tramon Williams向1码开球,Tramon威廉姆斯离开1码处,Tramon WIlliams离开铲球1码,然后下到Tramon Williams的2码处,Tramon Williams离开2码,等等。

完成后,继续前往Frostee Rucker(下一位玩家)。

A列中有2900个条目可以在某些结果中连接。

1 个答案:

答案 0 :(得分:0)

我在Excel表格中进行了很多改进(例如,20在写入punts 20 yards列时提供与punts相同的信息...)< / p>

但是:你可以一步一步,所以让我们坚持你想要的解决方案,以及如何用相当简单的方法实现它(主要是循环)

试试这个例子并玩弄它

Option Explicit

Sub PopulateArray()

    Dim blndimensioned As Boolean
    blndimensioned = False

    ' array for your concatenated strings
    Dim arrFoobar() As String

    ' array with the fieldnames to concatenate
    Dim arrColumns() As Variant
    arrColumns = Array("Punts", "Rush Left End", "Rush Left Tackle", "Rush Left Guard")

    Dim fldname

    ' adjust this to your actual sheet
    With GetRecordset(ThisWorkbook.Sheets("Tabelle1").UsedRange)

        ' loop the rows
        While Not .EOF

            ' loop the columnvalues
            For Each fldname In arrColumns

                If blndimensioned = True Then
                    ReDim Preserve arrFoobar(0 To UBound(arrFoobar) + 1)
                Else
                    ReDim Preserve arrFoobar(0 To 0)
                    blndimensioned = True
                End If

                ' dimension the array, populate it with concatenated values
                arrFoobar(UBound(arrFoobar)) = ![Playtype MasterList] & " " & .Fields(fldname).Value

            Next fldname

            .MoveNext
        Wend


    End With

End Sub

Function GetRecordset(rng As Range) As Object

    'Recordset ohne Connection:
    'https://usefulgyaan.wordpress.com/2013/07/11/vba-trick-of-the-week-range-to-recordset-without-making-connection/

    Dim xlXML As Object
    Dim rst As Object

    Set rst = CreateObject("ADODB.Recordset")
    Set xlXML = CreateObject("MSXML2.DOMDocument")
    xlXML.LoadXML rng.Value(xlRangeValueMSPersistXML)

    rst.Open xlXML

    Set GetRecordset = rst

End Function