VBA实现多列数据合并为一列

分类:代码, 博客 标签:

水文数据处理中,经常会遇到将一个表格进行重新排列,实现把很多列的数据合并为一列,因此使用VBA实现多列数据合并为一列的功能,这一功能在日常工作也经常会用到,水文工具集提供的这一VBA函数为MultiColumnsToOneColumn,具体源代码如下:

Option Explicit
'================================
' 多列数据合并为一列
' MultiColumnsToOneColumn
' http://www.cnhup.com
'================================
Sub MultiColumnsToOneColumn()
    Dim shtNew As Worksheet
    Dim rngSelection As Range
    Dim rngDest As Range
    Dim i As Integer
    Dim j As Integer
    
    Dim iPosOfRow As Integer
    
    Set rngSelection = Selection
    Set shtNew = Sheets.Add
    Set rngDest = shtNew.Cells(1, 1)
    iPosOfRow = 0
    For j = 1 To rngSelection.Columns.Count
        For i = 1 To rngSelection.Rows.Count
            rngDest.Offset(iPosOfRow, 0).Value = rngSelection.Cells(i, j).Value
            iPosOfRow = iPosOfRow + 1
        Next
    Next
End Sub


分类:代码, 博客 标签:

发表评论

You must be logged in to post a comment.