1

一意の値 (A) の列と、一意の値のセット (B) と別の値 (C) の値を含む列の 2 番目のセットがあります。

基本的に、私がやろうとしているのは、すべての一意の値 A をループしてから、A から同じ値を複数回持つ可能性のある B を通過する内部ループを持ち、一致が見つかるたびに値を追加することです ( C)連結された文字列に変換し、それらの値がすべて見つかったら、それを新しいセルに挿入し、次の A 値に移動して同じことを行います。

さまざまなエラーが発生し、これをいくつかの方法で試しましたが、VBA をまったく使用していないため、そこに到達できず、これに関する適切な情報が見つかりませんでした。

これを行う方法についてのヘルプやアイデアは役に立ちます。以下は私が使用しているコードです。

これは基本的にスプレッドシートがどのように見えるかです

Column A ----- Column B -------- Column C 

A-----------------A---------------stringA  
A-----------------A --------------stringB
C-----------------B---------------stringC

したがって、私の新しい列では、列 AI の隣に stringA、stringB が挿入されます。

うまくいけば、それは理にかなっています。

Sub contactStuff()
    Dim roleName As String
    Dim rowNumber As Integer
    Dim userName As String
    Dim userRoleName As String
    Dim concatString As String
    Dim roleNumber As Integer
    roleNumber = 2
    rowNumber = 2
    For Each c In Worksheets("parentRoles").Range("C2:C856").Cells
        roleName = c.Value
        Do
            userRoleName = Worksheets("parentRoles").Range("G" & rowNumber)
            If userRoleName = roleName Then
                contactString = concatString & ", " & Worksheets("parentRoles").Range("E" & rowNumber)
                rowNumber = rowNumber + 1
            End If
        Loop While userRoleName = roleName
        Worksheets("parentRoles").Range("D" & roleNumber).Value = concatString
    Next
End Sub
4

1 に答える 1

0
Sub Button1_Click()
    Dim a As Long
    Dim b As Long
    Dim colA As Long
    Dim colB As Long
    Dim newString As String

    ' gets the last row in a column '
    colA = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row
    colB = ActiveSheet.Cells(ActiveSheet.Rows.Count, 2).End(xlUp).Row

    With Worksheets("Sheet1")
        ' First loop takes you through each unique value in column A '
        For a = 1 To colA
            newString = ""
            ' For every unique value in column A loop through column B and get the value from column C to build your string '
            For b = 1 To colB
                If .Cells(b, 2) = .Cells(a, 1) Then
                    If newString <> "" Then
                        newString = newString & ", " & Cells(b, 3) ' gets the value from col C and adds it to your new string '
                    Else
                        newString = Cells(b, 3)
                    End If
                End If
            Next
            ' Place the new string in column D'
            .Cells(a, 4).Value = newString
        Next
    End With
End Sub

次の列を使用します。

Col A   Col B   Col C
a         a       1 
b         b       1 
c         c       1 
          a       2 
          b       2 
          c       2 
          a       3 
          b       3 
          c       3 

列 D に次の出力が表示されます。

1, 2, 3
1, 2, 3
1, 2, 3
于 2012-11-13T16:42:14.890 に答える