Sub tqsj()
Dim arr, d, i%, j%, r%, s%, s0%, s1%, k, k1, brr, box
Set d = CreateObject("Scripting.Dictionary")
r = Range("a1048576").End(xlUp).Row
arr = Range("a3:a" & r)
s = 1: k = "": k1 = ""
While s < r - 1
If InStr(arr(s, 1), "中风险区(") > 0 Then
s0 = s '获取中高风险区的分界行号
ElseIf InStr(arr(s, 1), "低风险区(") > 0 Then
s1 = s '获取中低风险区的分界行号
End If
s = s + 1
Wend
For i = 3 To UBound(arr)
k2 = ""
If InStr(arr(i, 1), ")个") > 0 Then
If arr(i + 1, 1) = "" And arr(i - 1, 1) = "" Then k = Split(arr(i, 1), "(")(0) & "|" '获取省、直辖市名称
If arr(i + 1, 1) <> "" And arr(i - 1, 1) = "" Then k1 = Split(arr(i, 1), "(")(0) '获取地级市区名称
ElseIf arr(i - 1, 1) <> "" And arr(i, 1) <> "" Then
k2 = "|" & Split(arr(i, 1), " ")(0) '获取县区名称
End If
If arr(i, 1) <> "" And k2 <> "" Then
If i < s0 Then d(Split(arr(1, 1), "(")(0) & "|" & k & k1 & k2) = "" '高风险区数据加入字典
If i < s1 And i > s0 Then d(Split(arr(s0, 1), "(")(0) & "|" & k & k1 & k2) = "" '中风险区数据加入字典
If i > s1 Then d(Split(arr(s1, 1), "(")(0) & "|" & k & k1 & k2) = "" '低风险区数据加入字典
End If
Next i
ReDim brr(0 To d.Count, 0 To 3)
For i = 0 To d.Count - 1
box = Split(d.keys()(i), "|")
For j = 0 To UBound(box)
brr(i, j) = box(j)
Next j
Next i
[c2].Resize(UBound(brr), 4) = brr
Application.DisplayAlerts = False
For i = Range("c3").End(xlDown).Row To 2 Step -1
If Cells(i, "c") = Cells(i - 1, "c") Then Range(Cells(i, "c"), Cells(i - 1, "c")).Merge '合并单元格
Next i
Set d = Nothing
Application.DisplayAlerts = True
End Sub
|