利用VBA将多行多列区域转为一列
时间:2022-07-06 15:19:53
有时需要在Excel中将某个多行多列的区域转换到一列中,下面的VBA代码可以实现这个目的。
1.按快捷键Alt+F11,打开VBA编辑器,在右侧代码窗口中粘贴下列代码:
Sub RangeToOneCol()
Dim TheRng, TempArr
Dim i As Integer, j As Integer, elemCount As Integer
On Error GoTo line1
Range("a:a").ClearContents
If Selection.Cells.Count = 1 Then
Range("a1") = Selection
Else
TheRng = Selection
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
Range("a1:a" & elemCount) = TempArr
End If
line1:
End Sub
上述代码可以将所选择的区域转换到A列中。
2.关闭VBA编辑器,返回Excel工作表界面。
3.如果A列包含数据,先在工作表的最左侧插入一列,以便放置转换后的数据。
4.选择需要转换的区域,按Alt+F8,打开“宏”对话框,选择上述代码中的宏名“RangeToOneCol”运行代码。
标签:代码,区域,转换,选择,Excel函数
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
怎样在Word中制作信笺?Word制作竖式的古代信笺的方法
2023-01-17 22:04:21
![](https://img.aspxhome.com/file/2023/0/31110_0s.png)
excel表格排序的教程
2022-03-22 07:39:54
word如何设计个人简历封面
2023-06-12 08:47:08
![](https://img.aspxhome.com/file/2023/2/21022_0s.jpg)
利用word制作一个简单的公司简介
2022-02-03 02:35:22
![](https://img.aspxhome.com/file/2023/0/26740_0s.jpg)
word2016怎么制作毛笔书法字帖?word2016字帖制作教程
2022-01-22 09:22:36
![](https://img.aspxhome.com/file/2023/0/22930_0s.jpg)
Excel定位填充的小技巧,excel定位填充
2022-09-24 00:25:49
Word页码太难搞,对着页码思考人生
2023-03-03 14:05:38
![](https://img.aspxhome.com/file/2023/7/32557_0s.jpg)
如何把xlsx文件转换成xls的图文方法步骤
2022-06-11 03:18:15
![](https://img.aspxhome.com/file/2023/0/43080_0s.jpg)
Word折线图制法
2022-06-28 08:19:26
iOS 14 小技巧: 如何在 Safari 浏览器页面中查找关键字?
2023-10-19 11:30:25
![](https://img.aspxhome.com/file/2023/5/46305_0s.png)
win10闹钟关机后还有没?win10闹钟关机后还会不会响?
2023-11-09 05:43:48
![](https://img.aspxhome.com/file/2023/7/47497_0s.jpg)
FREQUENCY函数
2022-03-03 15:35:31
![](https://img.aspxhome.com/file/2023/2/a142492_0s.png)
word如何设置背景色
2022-01-19 05:03:11
excel 显示所有隐藏工作表的方法实现教程
2023-04-19 19:05:53
![](https://img.aspxhome.com/file/2023/3/a155073_0s.jpg)
word中导入excel的教程
2022-04-20 03:42:02
word2016字体重叠看不清楚怎么解决
2023-11-22 14:48:24
![](https://img.aspxhome.com/file/2023/5/17745_0s.jpg)
Win10键盘睡眠唤醒后无法使用怎么办?
2023-11-21 02:36:23
![](https://img.aspxhome.com/file/2023/2/49782_0s.jpg)
如何在Excel中通过改变单元格的值来动态显示图片
2022-03-26 02:30:16
![](https://img.aspxhome.com/file/2023/4/a156644_0s.jpg)
word中简繁体转换的技巧
2022-07-11 09:08:01
Excel中要经常输入重复的文字,无需复制粘贴,可以使用下拉列表
2022-01-23 18:24:00
![](https://img.aspxhome.com/file/2023/5/a140545_0s.jpg)