水文数据处理中,经常会遇到将一个表格进行重新排列,实现把很多列的数据合并为一列,因此使用VBA实现多列数据合并为一列的功能,这一功能在日常工作也经常会用到,提供的这一VBA函数为MultiColumnsToOneColumn,具体源代码如下:
01.
Option
Explicit
02.
'================================
03.
' 多列数据合并为一列
04.
' MultiColumnsToOneColumn
05.
'
06.
'================================
07.
Sub
MultiColumnsToOneColumn()
08.
Dim
shtNew
As
Worksheet
09.
Dim
rngSelection
As
Range
10.
Dim
rngDest
As
Range
11.
Dim
i
As
Integer
12.
Dim
j
As
Integer
13.
14.
Dim
iPosOfRow
As
Integer
15.
16.
Set
rngSelection = Selection
17.
Set
shtNew = Sheets.Add
18.
Set
rngDest = shtNew.Cells(1, 1)
19.
iPosOfRow = 0
20.
For
j = 1
To
rngSelection.Columns.Count
21.
For
i = 1
To
rngSelection.Rows.Count
22.
rngDest.Offset(iPosOfRow, 0).Value = rngSelection.Cells(i, j).Value
23.
iPosOfRow = iPosOfRow + 1
24.
Next
25.
Next
26.
End
Sub