excel利用VBA将多行多列区域转为一列
时间:2022-09-12 03:03:56
有时需要在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,利用,VBA,将,多行,多列,区域,转为,一列
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Word中进行添加和删除水印的操作方法
2022-05-02 14:28:12
win10版本2004清理系统垃圾文件的操作方法
2023-08-21 17:17:23
![](https://img.aspxhome.com/file/2023/3/47063_0s.jpg)
ADDRESS函数
2023-08-19 22:15:12
![](https://img.aspxhome.com/file/2023/7/a142507_0s.png)
excel不能打印预览以及进行页面设置该怎办?
2022-12-12 00:17:06
![](https://img.aspxhome.com/file/2023/0/36080_0s.jpg)
Word中进行文字设置图片的环绕格式的操作技巧
2023-10-15 02:07:10
在word文档中如何添加一些公式?
2022-07-22 22:17:00
![](https://img.aspxhome.com/file/2023/2/25702_0s.jpg)
excel批量删除空行
2023-09-01 18:22:10
![](https://img.aspxhome.com/file/2023/3/a154343_0s.jpg)
excel 如何利用宏统计某种颜色格子里面数值的总和
2022-11-24 13:26:52
![](https://img.aspxhome.com/file/2023/7/a154597_0s.jpg)
win10电脑提示未检测到电池是什么原因?win10电脑提示未检测到电池解析方法
2023-08-03 17:53:36
![](https://img.aspxhome.com/file/2023/6/46716_0s.png)
如何美化Dock栏上的Office文件夹图标?
2023-07-29 17:31:57
![](https://img.aspxhome.com/file/2023/9/15249_0s.jpg)
word怎么添加公文分隔线?
2022-06-23 10:53:22
![](https://img.aspxhome.com/file/2023/1/30581_0s.png)
如何在微软Word里插入超链接
2022-08-09 14:34:08
关闭Office办公软件中自动拼写检查和自动语法检查图文教程
2023-01-22 10:59:41
![](https://img.aspxhome.com/file/2023/0/29190_0s.png)
Excel表格怎么制作柏拉图
2022-04-11 22:46:21
excel打开闪退如何解决
2022-11-28 05:52:12
怎样打印Word2013文档的修改时间
2023-12-11 23:44:48
![](https://img.aspxhome.com/file/2023/7/20897_0s.jpg)
Win10 20H2版本如何查看系统属性?
2023-11-14 21:03:42
![](https://img.aspxhome.com/file/2023/4/49234_0s.png)
Win10一开游戏就断WiFi怎么办?Win10一开游戏就断网是怎么回事?
2023-12-14 10:20:24
![](https://img.aspxhome.com/file/2023/5/51795_0s.jpg)
excel里常见的函数公式
2023-01-11 09:55:05
word 插入自动图文集的方法
2023-11-10 18:33:33
![](https://img.aspxhome.com/file/2023/6/33136_0s.jpg)