将来自多列的数据合并到一列中

我有以下几点:
在这里输入图像说明

我正在使用@ BigBobby的答案来实现这一点:

Sub MoveData() Range("H5:H99").ClearContents START_ROW = 5 START_COL = 1 STEP_COL = 2 OUTPUT_ROW = 5 OUTPUT_COL = 8 Limit_Col = 9 Row = START_ROW Col = START_COL Out_Row = OUTPUT_ROW While Col < Limit_Col While Cells(Row, Col).Value <> "" Cells(Out_Row, OUTPUT_COL).Value = Cells(Row, Col).Value Out_Row = Out_Row + 1 Row = Row + 1 Wend Row = START_ROW Col = Col + STEP_COL Wend End Sub 

但是,正如你所看到的,我希望得到那些列出现在空白单元格后的值。 但是这段代码无法将那些以黄色突出显示的单元格拉出
如何修改这段代码来提取一个或多个空白单元格后可能出现的所有数据?

调整这个代码:

 While Cells(Row, Col).Value <> "" Cells(Out_Row, OUTPUT_COL).Value = Cells(Row, Col).Value Out_Row = Out_Row + 1 Row = Row + 1 Wend 

对于:

 Do until row > Cells(65536, Col).End(xlUp).Row Cells(Out_Row, OUTPUT_COL).Value = Cells(Row, Col).Value Out_Row = Out_Row + 1 Row = Row + 1 Loop 

这基本上检查行是否已经通过数据的最后一行,如果它已经移动到下一列。

编辑

不要复制空白单元格使用这个:

  Do until row > Cells(65536, Col).End(xlUp).Row If Cells(Row, Col).Value <> "" then Cells(Out_Row, OUTPUT_COL).Value = Cells(Row, Col).Value Out_Row = Out_Row + 1 End If Row = Row + 1 Loop 

前面的答案很接近,但是它也会复制所有的空格。 这段代码应该做你所需要的:

 Sub MoveData() START_ROW = 5 START_COL = 1 STEP_COL = 2 OUTPUT_ROW = 5 OUTPUT_COL = 10 Row = START_ROW Col = START_COL Out_Row = OUTPUT_ROW While Col < OUTPUT_COL While Row < ActiveSheet.UsedRange.Rows.Count If Cells(Row, Col).Value <> "" Then Cells(Out_Row, OUTPUT_COL).Value = Cells(Row, Col).Value Out_Row = Out_Row + 1 End If Row = Row + 1 Wend Row = START_ROW Col = Col + STEP_COL Wend End Sub