实现分列的两段excel vba 分列代码
时间:2023-04-12 00:50:38
excel vba 分列多用于一般常规的分列操作完成不了的情况。
Excel内置的分列,仅用于有规律的数据进行分列。比如下面的截图,这样的数据源,分列就可以考虑使用excel vba 分列完成。
A列数据源,要将汉字和数字分列后的效果如B:D列。
下面是两段excel vba 分列的代码,案例和答案来自论坛版主。
第一段excel vba 分列的代码:
Sub vba分列()
Dim oJs As Object, rng As Range
Set oJs = CreateObject("ScriptControl"): oJs.Language = "JScript"
oJs.eval "function gets(str){return str.replace(/(\d+)/,’ $1 ‘)}"
For Each rng In Range("A2", [A65536].End(3))
rng(1, 2).Resize(1, 3) = Split(oJs.codeobject.gets(rng.Value), " ")
Next
End Sub
第二段excel vba 分列的代码:
Sub vba分列()
Dim arr, i%, brr(), sma As Object
arr = Range("a2:a" & Cells(Rows.Count, 1).End(3).Row)
ReDim brr(1 To UBound(arr), 1 To 3)
With CreateObject("vbscript.regexp")
.Global = True
.Pattern = "([^\d]+)(\d+)(.+)"
For i = 1 To UBound(arr)
Set sma = .Execute(arr(i, 1))(0).submatches
brr(i, 1) = sma(0)
brr(i, 2) = sma(1)
brr(i, 3) = sma(2)
Next
End With
Range("b2", Cells(Rows.Count, Columns.Count)).ClearComments
Range("b2").Resize(UBound(brr), UBound(brr, 2)).NumberFormat = "@"
Range("b2").Resize(UBound(brr), UBound(brr, 2)) = brr
Set sma = Nothing
End Sub
代码使用方法,在excel中,按下ALT+F11,打开VBE编辑器,单击插入——模块,复制上面任意一段代码,按F5键运行即可完成分列。
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Win8程序不兼容怎么查看?兼容中心是如何确定程序的兼容性
MimoLive for mac使用教程,将媒体文件添加到mimoLive?
![](https://img.aspxhome.com/file/2023/4/a212634_0s.jpeg)
QQ群匿名消息怎么查?QQ群匿名消息的破解方法
![](https://img.aspxhome.com/file/2023/8/a339454_0s.jpg)
Win10系统自定义锁屏界面的步骤
![](https://img.aspxhome.com/file/2023/26/a226797_0s.jpg)
笔记本wifi连上又断开再连又断是为什么?
![](https://img.aspxhome.com/file/2023/2/53322_0s.jpg)
word文档如何全部替换内容?
![](https://img.aspxhome.com/file/2023/7/29147_0s.jpg)
Win7无法关闭密码保护共享的原因及解决方案
![](https://img.aspxhome.com/file/2023/28/a247359_0s.jpg)
Win10快速预览版19559推送:无重大新功能 修复绿屏死机Bug
![](https://img.aspxhome.com/file/2023/28/a241011_0s.jpg)
Win10如何移除锁屏界面网络图标?
![](https://img.aspxhome.com/file/2023/26/a226047_0s.jpg)
Win7如何给电脑设置一个登陆开机密码?
![](https://img.aspxhome.com/file/2023/28/a246398_0s.jpg)
WPS PPT放映时分屏显示的设置方法
![](https://img.aspxhome.com/file/2023/10/a351064_0s.png)
使用Word如何划横线?
![](https://img.aspxhome.com/file/2023/3/31353_0s.jpg)
如何把“电源”按钮添加到Win10右键菜单
![](https://img.aspxhome.com/file/2023/30/a262621_0s.png)
「Ai实例教程」制作Q版渐变文字效果
![](https://img.aspxhome.com/file/2023/8/a211778_0s.jpeg)
ppt怎么制作文字从右移动的动画?
![](https://img.aspxhome.com/file/2023/10/a346192_0s.gif)
几何画板如何绘制固定比值的矩形
![](https://img.aspxhome.com/file/2023/3/a300071_0s.jpg)
XP系统中制作动态屏幕保护的方法
![](https://img.aspxhome.com/file/2023/28/a242545_0s.jpg)
wps文字怎么给论文插入页码
Windows8系统Defender与安全软件冲突如何禁用
![](https://img.aspxhome.com/file/2023/28/a250899_0s.jpg)