Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 1786|回复: 4

[已解决]请老师帮忙对数据进行拆分,谢谢苏子龙老师!

[复制链接]
发表于 2017-4-16 10:40 | 显示全部楼层 |阅读模式
本帖最后由 lhj323323 于 2017-4-16 22:21 编辑

老师:

我想对指定列的数据,按固定顺序的关键词进行拆分,并导入到同一工作表的其它列里,详细需求,请看附件,先谢谢了
请用VBA来实现。
新建.rar (12.51 KB, 下载次数: 5)
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2017-4-16 11:50 | 显示全部楼层    本楼为最佳答案   
  1. Sub tt()
  2. Dim arr, arr2, i%, y%
  3. Dim reg, mh
  4. Sheet2.Activate
  5. Application.ScreenUpdating = False
  6. arr = Range("a2:d" & Range("a65536").End(xlUp).Row)
  7. ReDim arr2(1 To UBound(arr), 1 To 6)
  8. Set reg = CreateObject("vbscript.regexp")
  9. For i = 1 To UBound(arr)
  10.     For y = 1 To 3
  11.         arr2(i, y) = arr(i, y)
  12.         If arr(i, 3) <> "" Then
  13.             reg.Pattern = Mid("送转派", y, 1) & "(\d+\.?\d*)"
  14.             If reg.test(arr(i, 4)) Then
  15.                 Set mh = reg.Execute(arr(i, 4))
  16.                 arr2(i, y + 3) = mh(0).submatches(0)
  17.             End If
  18.         End If
  19.     Next
  20. Next
  21. Range("f2:l65536").ClearContents
  22. Range("g2").Resize(UBound(arr), 6) = arr2
  23. Range("g:g").NumberFormatLocal = "000000"
  24. Application.ScreenUpdating = True
  25. End Sub
复制代码

新建.rar

545.61 KB, 下载次数: 8

评分

参与人数 1 +2 收起 理由
lhj323323 + 2 谢谢苏子龙老师!

查看全部评分

回复

使用道具 举报

发表于 2017-6-13 21:17 | 显示全部楼层
Sub 分组()
Dim regex As New RegExp
Dim arr
Dim m As IMatch2
t = Timer
arr = Range("a2:d1000")
ReDim brr(1 To UBound(arr), 1 To 6)
With regex
      .Global = True
      .Pattern = "(?送|转|派))(\d+(\.\d+)?)"
      For i = 1 To UBound(arr)
          If .Test(arr(i, 4)) Then
          Set ma = .Execute(arr(i, 4))
              x = x + 1
              For j = 1 To 3
                  brr(x, j) = arr(i, j)
              Next j
              For Each m In ma
                  Select Case m.SubMatches(0)
                         Case "送"
                            brr(x, 4) = m.SubMatches(1)
                         Case "转"
                            brr(x, 5) = m.SubMatches(1)
                         Case "派"
                            brr(x, 6) = m.SubMatches(1)
                  End Select
              Next m
          End If
      Next i
End With
Range("g2:l1000").ClearContents
Range("g2").Resize(x, 6) = brr
MsgBox Format(Timer - t, "0.000秒")
End Sub
回复

使用道具 举报

发表于 2017-6-13 21:19 | 显示全部楼层
Sub 分组()
Dim regex As New RegExp
Dim arr
Dim m As IMatch2
t = Timer
arr = Range("a2:d1000")
ReDim brr(1 To UBound(arr), 1 To 6)
With regex
      .Global = True
      .Pattern = "(?送|转|派))(\d+(\.\d+)?)"
      For i = 1 To UBound(arr)
          If .Test(arr(i, 4)) Then
          Set ma = .Execute(arr(i, 4))
              x = x + 1
              For j = 1 To 3
                  brr(x, j) = arr(i, j)
              Next j
              For Each m In ma
                  Select Case m.SubMatches(0)
                         Case "送"
                            brr(x, 4) = m.SubMatches(1)
                         Case "转"
                            brr(x, 5) = m.SubMatches(1)
                         Case "派"
                            brr(x, 6) = m.SubMatches(1)
                  End Select
              Next m
          End If
      Next i
End With
Range("g2:l1000").ClearContents
Range("g2").Resize(x, 6) = brr
MsgBox Format(Timer - t, "0.000秒")
End Sub
回复

使用道具 举报

发表于 2017-6-13 21:21 | 显示全部楼层
通过测试速度上好像快了点
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-26 15:29 , Processed in 0.369351 second(s), 14 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表