经常会遇到将一个表格进行重新排列,实现把很多列的数据合并为一列,因此使用VBA实现多列数据合并为一列的功能,这一功能在日常工作也经常会用到,提供的这一VBA函数为MultiColumnsToOneColumn,具体源代码如下:
Option Explicit
'================================
' 多列数据合并为一列
' MultiColumnsToOneColumn
'
'================================
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
如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!
txttool.com 说一段 esp56物联 查询128 IP查询