Vba 从顶部删除连接的重复项

Vba 从顶部删除连接的重复项,vba,Vba,我已经编写了以下宏,当我将值保存到即时窗口时,它正在工作,但不知道如何将以“,”分隔的值填充到一个单元格中。基本上,我在一列中查找“活动”,如果找到,请转到左侧的4个单元格并从中获取信息。。。 你能帮忙吗 Dim Active() As Variant Dim i ReDim Active(Range("G9:G24").Cells.Count) For Each Zelle In Range("G9:G24") If InStr(1, Zelle, "Active") <> 0 T

我已经编写了以下宏,当我将值保存到即时窗口时,它正在工作,但不知道如何将以“,”分隔的值填充到一个单元格中。基本上,我在一列中查找“活动”,如果找到,请转到左侧的4个单元格并从中获取信息。。。 你能帮忙吗

Dim Active() As Variant
Dim i
ReDim Active(Range("G9:G24").Cells.Count)
For Each Zelle In Range("G9:G24")
If InStr(1, Zelle, "Active") <> 0 Then
Active(i) = Zelle.Offset(0, -4)
End If
i = i + 1
Next Zelle

 For i = LBound(Active) To UBound(Active)
 If Trim(Active(i)) <> "" Then
   Debug.Print Active(i)
   End If
  Next i
  End Sub
Dim Active()作为变量
昏暗的我
ReDim活动(范围(“G9:G24”)。单元格。计数)
对于范围内的每个Zelle(“G9:G24”)
如果仪表(1,Zelle,“激活”)为0,则
有效(i)=零偏移量(0,-4)
如果结束
i=i+1
下一个泽尔
对于i=LBound(活动)到UBound(活动)
如果微调(激活(i))“”则
调试.打印活动(i)
如果结束
接下来我
端接头
添加以下内容

Dim s As String
然后像这样重写循环:

For i = LBound(Active) To UBound(Active)
    If Trim(Active(i)) <> "" Then
        s = s & IIf(Len(s)>0, ",", "") & trim(Active(i))
      End If
Next i
i=LBound(活动)到UBound(活动)的

如果微调(激活(i))“”则
s=s&IIf(透镜>0,“,”)和微调(激活(i))
如果结束
接下来我

然后您可以将
s
分配给一个单元格。

您可以通过循环遍历只与C列中的非空白单元格相对应的所需范围单元格来大大缩短代码

    Dim Zelle As Range
    Dim resultStrng As String

    For Each Zelle In Range("G9:G24").Offset(,-4).SpecialCells(xlCellTypeConstants) '<--| loop through not blank cell in range 4 columns to the left of G9:G24
        If InStr(1, Zelle.Offset(, 4), "Active") <> 0 And Trim(Zelle) <> "" And Instr(resultStrng, Trim(Zelle)) =0 Then resultStrng = resultStrng & Trim(Zelle) & "," '<--| update your result string whenever current cell has a character and its 4 columns to the right offset cell meets the "Active" criteria
    Next Zelle
    If resultStrng <> "" Then resultStrng = Left(resultStrng, Len(resultStrng) - 1) '<-- remove the last comma from 'resultStrng'
Dim Zelle作为范围
Dim resultStrng作为字符串

对于范围(“G9:G24”).Offset(,-4).SpecialCells(xlCellTypeConstants)“中的每个Zelle,您可能希望查看联接函数。@RichHolton联接函数是自然的,但
Trim(Active(i))”
表明它可能有空字段,在联接后必须修复这些字段。@JohnColeman很好。代码正在工作,现在,我尝试只从数组中添加唯一的值。尝试使用另一个For循环,但无法查看此…有什么想法吗?请参阅编辑的代码。如果它解决了您的问题,您可能希望将答案标记为已接受。非常感谢。