检查单元格的范围,并使用来自相应列的数据追加单元格

检查单元格的范围[比如C1:E5],如果单元格包含一个值,find它的行[比如行4]追加一个单元格[比如B6]与单元格的数据及其对应的行数据。

在这里输入图像说明

Sub Demo() Dim str As String str = "" For Each cel In Range("C1:E5") If Not IsEmpty(cel) Then If str = "" Then str = Range("B" & cel.Row) & "=" & cel.Value Else str = str & ", " & Range("B" & cel.Row) & "=" & cel.Value End If End If Next If str <> "" Then Range("B6") = str End Sub 
 Sub test() Dim rng As Range Dim tmp As String Set rng = Range("C1:E5") For Each cell In rng If cell.Value <> "" Then tmp = tmp + " " + Cells(cell.Row, 2).Value + " = " + cell.Value End If Next cell Range("B6").Value = tmp End Sub