|
代码如下。。。。
Function ss(rng)
Dim RegEx, b, bb
Set RegEx = CreateObject("VBScript.RegExp") 'Create0bject("VBSCRIPT.REGEXP") 'RegEx为建立正则表达式
RegEx.Global = True '设置全局可用
RegEx.Pattern = "\D"
b = RegEx.Replace(rng, "|") '把匹配样式的字符用|替换
ss = Replace(b, "|", "") '数字
Set RegEx = Nothing
End Function
Sub test()
Dim wb As Workbook, sht As Worksheet
Set wb = ThisWorkbook
Set sht = wb.Sheets("sheet1")
With sht
Arr = .[a1].CurrentRegion
Set d = CreateObject("Scripting.Dictionary")
Ar = [{"历","政","地","物","化","生"}]
For i = 2 To UBound(Arr)
d(Arr(i, 1)) = i
Next
For i = 1 To UBound(Ar)
d(Ar(i)) = i + 1
Next
ReDim Brr(1 To UBound(Arr), 1 To UBound(Ar) + 1)
For i = 2 To UBound(Arr)
s = Split(Arr(i, 2), ".")(1)
If InStr(s, ",") = 0 Then
sss = Left(s, 3)
ssss = --ss(s)
For j = 1 To 3
If d.exists(Mid(s, j, 1)) Then
Brr(d(Arr(i, 1)), d(Mid(s, j, 1))) = ssss
End If
Next
Else
s = Split(Split(Arr(i, 2), ".")(1), ",")
For j = 0 To UBound(s)
sss = Left(s(j), 3)
ssss = --ss(s(j))
For k = 1 To 3
If d.exists(Mid(s(j), k, 1)) Then
Brr(d(Arr(i, 1)), d(Mid(s(j), k, 1))) = Brr(d(Arr(i, 1)), d(Mid(s(j), k, 1))) + ssss
End If
Next
Next
End If
Next
.[m1].CurrentRegion.Clear
.[m1].Resize(UBound(Brr), UBound(Brr, 2)) = Brr
.[n1].Resize(, UBound(Ar)) = Ar
ReDim br(1 To UBound(Arr))
For i = 1 To UBound(Arr)
br(i) = Arr(i, 1) & "班"
Next
.[m1].Resize(UBound(Arr)) = Application.Transpose(br)
.[m1] = "学科"
.[m1].CurrentRegion.HorizontalAlignment = xlCenter
.[m1].CurrentRegion.Borders.LineStyle = 1
.[m1].CurrentRegion.Columns.ColumnWidth = 8.38
End With
Set d = Nothing
Beep
End Sub |
|