多列数据区域转换成一列VBA代码
来源:网络整理
发布时间:2019-03-05 00:07:00
查看次数:
内容提要:VBA模块,实现了选择需要转换的数据区域和选择存放区域起始单元格。
Sub rangetoonecol2()
Dim TheRng, TempArr, RNG As Range
Dim i As Integer, j As Integer, elemCount As Integer
On Error Resume Next
Set RNG = Application.InputBox("请选择源区域", "区域转一列", , , , , , 8)
If RNG.Cells.Count = 1 Then
MsgBox "所选择区域单元格个数应该大于1"
Exit Sub
Else
TheRng = RNG
elemCount = UBound(TheRng, 1) * UBound(TheRng, 2)
ReDim TempArr(1 To elemCount, 1 To 1)
For i = 1 To UBound(TheRng, 1)
For j = 1 To UBound(TheRng, 2)
TempArr((i - 1) * UBound(TheRng, 2) + j, 1) = TheRng(i, j)
Next
Next
Set RNG = Application.InputBox("请选择存放区域起始单元格", "区域转一列", , , , , , 8)
RNG(1).Resize(elemCount, 1) = TempArr
End If
End Sub