水文數據處理中,經常會遇到將一個表格進行重新排列,實現把很多列的數據合並為一列,因此使用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