Excel精英培训网

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

[已解决]请问怎样修改代码

[复制链接]
发表于 2015-1-28 22:13 | 显示全部楼层 |阅读模式
本帖最后由 dyzx 于 2015-2-1 09:44 编辑

请老师帮帮助,如果将表1转换成表2应怎样修改里面的代码,多谢指教,多谢
最佳答案
2015-1-31 21:56
dyzx 发表于 2015-1-30 08:35
hwc2ycy老师:非常多谢你的耐心指教,就是除了“**长、主任、书记”等外,其它就是“教师”职务,多谢[/b ...
  1. Sub 转换()
  2.     Dim r%, i%, m%, j%
  3.     Dim arr, brr(1 To 1000, 1 To 6)
  4.     Dim arrT1, arrT2
  5.     Dim str1$, str2$, str3$ ', str4$
  6.     Dim reg As Object, mh As Object
  7.     'Dim reg As RegExp
  8.     Dim strKm$, strNj$, strBj$, strZw$

  9.     str1 = "(初一|初二|初三)(.*?)(语文|数学|英语|政治|历史|音乐|化学|物理|地理|信息技术|生物|体育|美术|综合实践|写字课)"

  10.     '职务
  11.     str2 = "[^\s}]+?(长|任|记)"

  12.     '去多余的空格
  13.     str3 = "\s+"

  14.     Application.ScreenUpdating = False
  15.     Set reg = CreateObject("VBSCRIPT.REGEXP")  '引用正则表达式
  16.     reg.Global = True

  17.     m = 0
  18.     With Worksheets("表1")
  19.         r = .Cells(.Rows.Count, 1).End(xlUp).Row
  20.         arr = .Range("a3:l" & r)
  21.     End With

  22.     'brr: 序号 姓名 职务 任教科目 任教年级 任教班级
  23.     For j = 1 To UBound(arr, 2) Step 3
  24.         For i = 1 To UBound(arr)
  25.             If Len(arr(i, j + 1)) <> 0 Then
  26.                 strKm = "": strNj = "": strBj = "": strZw = ""
  27.                 reg.Pattern = str3
  28.                 arrT2 = Split(reg.Replace(arr(i, j + 2), " "))
  29.                 m = m + 1
  30.                 brr(m, 1) = m   '序号
  31.                 brr(m, 2) = arr(i, j + 1)   '姓名
  32.                 For Each arrT1 In arrT2
  33.                     With reg
  34.                         '职务
  35.                         .Pattern = str2
  36.                         If .Test(arrT1) Then
  37.                             Set mh = .Execute(arrT1)
  38.                             strZw = strZw & mh(0) & "/"
  39.                         Else
  40.                             '班级,科目
  41.                             .Pattern = str1
  42.                             If .Test(arrT1) Then
  43.                                 Set mh = .Execute(arrT1)
  44.                                 If InStr(1, strKm, mh(0).SubMatches(2), vbBinaryCompare) = 0 Then strKm = strKm & mh(0).SubMatches(2) & ","
  45.                                 'brr(m, 4) = brr(m, 4) & mh(0).SubMatches(2) & ","
  46.                                 If InStr(1, strNj, mh(0).SubMatches(0), vbBinaryCompare) = 0 Then strNj = strNj & mh(0).SubMatches(0) & ","
  47.                                 'brr(m, 5) = brr(m, 5) & mh(0).SubMatches(0) & ","
  48.                                 If mh(0).SubMatches(1) <> "级" Then
  49.                                     If InStr(1, strBj, mh(0).SubMatches(1), vbBinaryCompare) = 0 Then strBj = strBj & mh(0).SubMatches(1) & ","
  50.                                     'brr(m, 6) = brr(m, 6) & mh(0).SubMatches(1) & ","
  51.                                 Else
  52.                                     If InStr(1, strBj, "全级", vbBinaryCompare) = 0 Then strBj = strBj & "全级,"
  53.                                 End If
  54.                             End If
  55.                         End If
  56.                     End With
  57.                 Next
  58.                 If Len(strZw) Then
  59.                     brr(m, 3) = Left(strZw, Len(strZw) - 1)
  60.                 Else
  61.                     brr(m, 3) = "教师"
  62.                 End If
  63.                
  64.                 If Len(strKm) Then brr(m, 4) = Left(strKm, Len(strKm) - 1)
  65.                 If Len(strNj) Then brr(m, 5) = Left(strNj, Len(strNj) - 1)
  66.                 If Len(strBj) Then brr(m, 6) = Left(strBj, Len(strBj) - 1)

  67.             End If
  68.         Next
  69.     Next
  70.     With Worksheets("表2")
  71.         With .Range("a3").Resize(UBound(brr), UBound(brr, 2))
  72.             .Value = brr
  73.             .Borders.LineStyle = xlContinuous
  74.             .Font.Size = 10
  75.             .HorizontalAlignment = xlCenter
  76.             .VerticalAlignment = xlCenter
  77.         End With
  78.     End With
  79.     Application.ScreenUpdating = True
  80.     MsgBox "转换完成", vbInformation + vbOKOnly, "提示"
  81. End Sub
复制代码

数据转换.rar

19.89 KB, 下载次数: 28

发表于 2015-1-28 23:02 | 显示全部楼层
做个记号,规则应用还是比较复杂的,明天再抽时间看。
回复

使用道具 举报

 楼主| 发表于 2015-1-29 08:38 | 显示全部楼层
hwc2ycy 发表于 2015-1-28 23:02
做个记号,规则应用还是比较复杂的,明天再抽时间看。

hwc2ycy老师:先多谢你的指教,多谢
回复

使用道具 举报

发表于 2015-1-29 15:54 | 显示全部楼层
  1. Sub 转换()
  2.     Dim r%, i%, m%, j%
  3.     Dim arr, brr(1 To 1000, 1 To 6)
  4.     Dim arrT1, arrT2
  5.     Dim str1$, str2$, str3$ ', str4$
  6.     Dim reg As Object, mh As Object
  7.     'Dim reg As RegExp
  8.     Dim strKm$, strNj$, strBj$, strZw$

  9.     str1 = "(初一|初二|初三)(.*?)(语文|数学|英语|政治|历史|音乐|化学|物理|地理|信息技术|生物|体育|美术|综合实践|写字课)"

  10.     '职务
  11.     str2 = "[^\s}]+?(长|任|干部|记)"

  12.     '去多余的空格
  13.     str3 = "\s+"

  14.     Application.ScreenUpdating = False
  15.     Set reg = CreateObject("VBSCRIPT.REGEXP")  '引用正则表达式
  16.     reg.Global = True

  17.     m = 0
  18.     With Worksheets("表1")
  19.         r = .Cells(.Rows.Count, 1).End(xlUp).Row
  20.         arr = .Range("a3:l" & r)
  21.     End With

  22.     'brr: 序号 姓名 职务 任教科目 任教年级 任教班级
  23.     For j = 1 To UBound(arr, 2) Step 3
  24.         For i = 1 To UBound(arr)
  25.             If Len(arr(i, j + 1)) <> 0 Then
  26.                 strKm = "": strNj = "": strBj = "": strZw = ""
  27.                 reg.Pattern = str3
  28.                 arrT2 = Split(reg.Replace(arr(i, j + 2), " "))
  29.                 m = m + 1
  30.                 brr(m, 1) = m   '序号
  31.                 brr(m, 2) = arr(i, j + 1)   '姓名
  32.                 For Each arrT1 In arrT2
  33.                     With reg
  34.                         '职务
  35.                         .Pattern = str2
  36.                         If .Test(arrT1) Then
  37.                             Set mh = .Execute(arrT1)
  38.                             strZw = strZw & mh(0) & "/"
  39.                         Else
  40.                             '班级,科目
  41.                             .Pattern = str1
  42.                             If .Test(arrT1) Then
  43.                                 Set mh = .Execute(arrT1)
  44.                                 If InStr(1, strKm, mh(0).SubMatches(2), vbBinaryCompare) = 0 Then strKm = strKm & mh(0).SubMatches(2) & ","
  45.                                 'brr(m, 4) = brr(m, 4) & mh(0).SubMatches(2) & ","
  46.                                 If InStr(1, strNj, mh(0).SubMatches(0), vbBinaryCompare) = 0 Then strNj = strNj & mh(0).SubMatches(0) & ","
  47.                                 'brr(m, 5) = brr(m, 5) & mh(0).SubMatches(0) & ","
  48.                                 If mh(0).SubMatches(1) <> "级" Then
  49.                                     If InStr(1, strBj, mh(0).SubMatches(1), vbBinaryCompare) = 0 Then strBj = strBj & mh(0).SubMatches(1) & ","
  50.                                     'brr(m, 6) = brr(m, 6) & mh(0).SubMatches(1) & ","
  51.                                 Else
  52.                                     If InStr(1, strBj, "全级", vbBinaryCompare) = 0 Then strBj = strBj & "全级,"
  53.                                 End If
  54.                             End If
  55.                         End If
  56.                     End With
  57.                 Next
  58.                 If Len(strZw) Then brr(m, 3) = Left(strZw, Len(strZw) - 1)
  59.                 If Len(strKm) Then brr(m, 4) = Left(strKm, Len(strKm) - 1)
  60.                 If Len(strNj) Then brr(m, 5) = Left(strNj, Len(strNj) - 1)
  61.                 If Len(strBj) Then brr(m, 6) = Left(strBj, Len(strBj) - 1)

  62.             End If
  63.         Next
  64.     Next
  65.     With Worksheets("表2")
  66.         With .Range("a3").Resize(UBound(brr), UBound(brr, 2))
  67.             .Value = brr
  68.             .Borders.LineStyle = xlContinuous
  69.             .Font.Size = 10
  70.             .HorizontalAlignment = xlCenter
  71.             .VerticalAlignment = xlCenter
  72.         End With
  73.     End With
  74.     Application.ScreenUpdating = True
  75.     MsgBox "转换完成", vbInformation + vbOKOnly, "提示"
  76. End Sub
复制代码
回复

使用道具 举报

 楼主| 发表于 2015-1-29 17:16 | 显示全部楼层
hwc2ycy 发表于 2015-1-29 15:54

hwc2ycy老师:非常多谢你的帮助,但还有一个小小要求,就是职务一栏除了“**长”之外,其余就是“教师”,那代码应该怎样修改,多谢指教,多谢
回复

使用道具 举报

发表于 2015-1-29 21:50 | 显示全部楼层
dyzx 发表于 2015-1-29 17:16
hwc2ycy老师:非常多谢你的帮助,但还有一个小小要求,就是职务一栏除了“**长”之外,其余就 ...

意思就是职务就两种 XX长和老师,像主任,书记,也不用么

回复

使用道具 举报

 楼主| 发表于 2015-1-30 08:35 | 显示全部楼层
hwc2ycy 发表于 2015-1-29 21:50
意思就是职务就两种 XX长和老师,像主任,书记,也不用么

hwc2ycy老师:非常多谢你的耐心指教,就是除了“**长、主任、书记”等外,其它就是“教师”职务,多谢
回复

使用道具 举报

发表于 2015-1-31 21:56 | 显示全部楼层    本楼为最佳答案   
dyzx 发表于 2015-1-30 08:35
hwc2ycy老师:非常多谢你的耐心指教,就是除了“**长、主任、书记”等外,其它就是“教师”职务,多谢[/b ...
  1. Sub 转换()
  2.     Dim r%, i%, m%, j%
  3.     Dim arr, brr(1 To 1000, 1 To 6)
  4.     Dim arrT1, arrT2
  5.     Dim str1$, str2$, str3$ ', str4$
  6.     Dim reg As Object, mh As Object
  7.     'Dim reg As RegExp
  8.     Dim strKm$, strNj$, strBj$, strZw$

  9.     str1 = "(初一|初二|初三)(.*?)(语文|数学|英语|政治|历史|音乐|化学|物理|地理|信息技术|生物|体育|美术|综合实践|写字课)"

  10.     '职务
  11.     str2 = "[^\s}]+?(长|任|记)"

  12.     '去多余的空格
  13.     str3 = "\s+"

  14.     Application.ScreenUpdating = False
  15.     Set reg = CreateObject("VBSCRIPT.REGEXP")  '引用正则表达式
  16.     reg.Global = True

  17.     m = 0
  18.     With Worksheets("表1")
  19.         r = .Cells(.Rows.Count, 1).End(xlUp).Row
  20.         arr = .Range("a3:l" & r)
  21.     End With

  22.     'brr: 序号 姓名 职务 任教科目 任教年级 任教班级
  23.     For j = 1 To UBound(arr, 2) Step 3
  24.         For i = 1 To UBound(arr)
  25.             If Len(arr(i, j + 1)) <> 0 Then
  26.                 strKm = "": strNj = "": strBj = "": strZw = ""
  27.                 reg.Pattern = str3
  28.                 arrT2 = Split(reg.Replace(arr(i, j + 2), " "))
  29.                 m = m + 1
  30.                 brr(m, 1) = m   '序号
  31.                 brr(m, 2) = arr(i, j + 1)   '姓名
  32.                 For Each arrT1 In arrT2
  33.                     With reg
  34.                         '职务
  35.                         .Pattern = str2
  36.                         If .Test(arrT1) Then
  37.                             Set mh = .Execute(arrT1)
  38.                             strZw = strZw & mh(0) & "/"
  39.                         Else
  40.                             '班级,科目
  41.                             .Pattern = str1
  42.                             If .Test(arrT1) Then
  43.                                 Set mh = .Execute(arrT1)
  44.                                 If InStr(1, strKm, mh(0).SubMatches(2), vbBinaryCompare) = 0 Then strKm = strKm & mh(0).SubMatches(2) & ","
  45.                                 'brr(m, 4) = brr(m, 4) & mh(0).SubMatches(2) & ","
  46.                                 If InStr(1, strNj, mh(0).SubMatches(0), vbBinaryCompare) = 0 Then strNj = strNj & mh(0).SubMatches(0) & ","
  47.                                 'brr(m, 5) = brr(m, 5) & mh(0).SubMatches(0) & ","
  48.                                 If mh(0).SubMatches(1) <> "级" Then
  49.                                     If InStr(1, strBj, mh(0).SubMatches(1), vbBinaryCompare) = 0 Then strBj = strBj & mh(0).SubMatches(1) & ","
  50.                                     'brr(m, 6) = brr(m, 6) & mh(0).SubMatches(1) & ","
  51.                                 Else
  52.                                     If InStr(1, strBj, "全级", vbBinaryCompare) = 0 Then strBj = strBj & "全级,"
  53.                                 End If
  54.                             End If
  55.                         End If
  56.                     End With
  57.                 Next
  58.                 If Len(strZw) Then
  59.                     brr(m, 3) = Left(strZw, Len(strZw) - 1)
  60.                 Else
  61.                     brr(m, 3) = "教师"
  62.                 End If
  63.                
  64.                 If Len(strKm) Then brr(m, 4) = Left(strKm, Len(strKm) - 1)
  65.                 If Len(strNj) Then brr(m, 5) = Left(strNj, Len(strNj) - 1)
  66.                 If Len(strBj) Then brr(m, 6) = Left(strBj, Len(strBj) - 1)

  67.             End If
  68.         Next
  69.     Next
  70.     With Worksheets("表2")
  71.         With .Range("a3").Resize(UBound(brr), UBound(brr, 2))
  72.             .Value = brr
  73.             .Borders.LineStyle = xlContinuous
  74.             .Font.Size = 10
  75.             .HorizontalAlignment = xlCenter
  76.             .VerticalAlignment = xlCenter
  77.         End With
  78.     End With
  79.     Application.ScreenUpdating = True
  80.     MsgBox "转换完成", vbInformation + vbOKOnly, "提示"
  81. End Sub
复制代码

评分

参与人数 1 +3 收起 理由
dyzx + 3 很给力!

查看全部评分

回复

使用道具 举报

 楼主| 发表于 2015-2-1 09:47 | 显示全部楼层
hwc2ycy教师:非常多谢你的帮助,辛苦你了,多谢
抱拳.png
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-26 08:41 , Processed in 0.373466 second(s), 14 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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