我编写了以下宏,它在将值保存到立即窗口时工作,但不知道如何填充值,用“,”分隔到一个单元格中。基本上我在列中查找“活动”,如果找到,则向左移动4个单元格并从那里获取信息... 你能帮忙吗?
{{1}}
答案 0 :(得分:2)
添加以下
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
然后您可以将s
分配给单元格。
答案 1 :(得分:1)
您可以通过仅在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'