选择所有非空白单元格和关联的行

时间:2016-07-12 19:50:22

标签: excel-formula excel-2010

我一直在寻找超过3小时的公式或vba解决方案,因为某些原因找不到任何东西。

我需要查看列,查找所有非空单元格,然后将其输出到其他位置。 以下是数据的一个小示例表:

Original data

这就是我想要的结果:

Output table

我发现了许多解决方案,其中大多数都是= value和= match的组合,但我得到零结果,或者结果在列中有空白,应该永远不会有空白。

1 个答案:

答案 0 :(得分:2)

试试这个宏

Sub findBlankCells()

Dim ws As Worksheet
Dim lastrow As Long
Dim i As Long
Dim j As Long
'change the sheet to whatever name sheet you have
ws = ThisWorkbook.Sheets("Sheet1")
'didn't chose A since it seems like you have more blank values there
lastrow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
'j represents the row to copy to
j = 1
'loop through all the rows
For i = 1 To lastrow
'only copy to I and J column if both do not equal blank
If ws.Cells(i, "A") <> "" And ws.Cells(i, "B") <> "" Then
    ws.Cells(j, "I") = ws.Cells(i, "A")
    ws.Cells(j, "J") = ws.Cells(i, "B")
    'increase j if the statement is true to go on to the next row to copy to
    j = j + 1
End If
Next i
End Sub