【问题标题】:Looping through two rows of data in excel to match cells and add a string to new cell in vba循环遍历excel中的两行数据以匹配单元格并将字符串添加到vba中的新单元格
【发布时间】:2012-11-14 00:42:14
【问题描述】:

我有一列唯一值 (A) 和第二组列,其中的值来自一组唯一值 (B) 和另一个值 (C)。

基本上我想要做的是循环遍历所有唯一值 A,然后有一个内部循环遍历 B,它可能多次具有来自 A 的相同值,并且每次找到匹配项时,添加将值 (C) 连接到连接的字符串,然后当它找到所有这些值时,将其插入新单元格并移动到下一个 A 值并执行相同操作。

我遇到了各种错误,并尝试了多种方法,但根本不经常使用 VBA,我没有到达那里,也没有找到任何好的信息。

任何关于我如何做到这一点的帮助或想法都会有所帮助,下面是我正在使用的代码。

这基本上就是电子表格的样子

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

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

所以在我的新列中,我会在 A 列旁边插入 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

【问题讨论】:

    标签: vba excel


    【解决方案1】:
    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 
    

    您最终会在 Col D 中得到以下输出:

    1, 2, 3
    1, 2, 3
    1, 2, 3
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多