答案1
Public Function GatherData(src As Range) As Variant
Dim tmp()
Dim temp()
ReDim temp(1 To 2, 1 To 1)
tmp = src.Value
Dim i As Integer
Dim j As Integer
For i = LBound(tmp, 1) + 1 To UBound(tmp, 1)
For j = LBound(tmp, 2) + 1 To UBound(tmp, 2)
If tmp(i, j) > "" Then
temp(1, UBound(temp, 2)) = tmp(i, 1)
temp(2, UBound(temp, 2)) = tmp(i, j)
ReDim Preserve temp(1 To 2, 1 To (UBound(temp, 2) + 1))
End If
Next j
Next i
ReDim Preserve temp(1 To 2, 1 To (UBound(temp, 2) - 1))
GatherData = WorksheetFunction.Transpose(temp)
End Function
该公式以数组公式的形式插入(Ctrl-Shift-Enter)。
如果目标范围比结果所需的范围长,则其余部分将填充 N/A。如果目标范围比结果所需的范围短,则其余值将丢失,且不会有任何通知。