|
本帖最后由 老谭酸菜 于 2013-9-16 18:17 编辑
代码:
\[code=vb]Sub 数据整理()
Dim sz, sz1, sz3
Sheets(2).Cells.ClearContents
Sheets(1).Activate
n = [VI1].End(xlToLeft).Column
rng1 = Range(Cells(1, 2), Cells(1, n)) '号段区
rng2 = Range(Cells(2, 1), Cells([A65536].End(xlUp).Row, 1)) '区号
rng3 = Range(Cells(2, 2), Cells([A65536].End(xlUp).Row, n)) '尾号区
n = n - 1
m = 1
Sheets(2).Cells(m, 1) = Range("A1")
Sheets(2).Cells(m, 2) = "号段"
m = m + 1
For i1 = 1 To UBound(rng2) '逐区号循环
For i2 = 1 To UBound(rng1, 2) '逐号段循环
If Len(Trim(rng3(i1, i2))) = 0 Then GoTo out
If InStr(rng3(i1, i2), ",") > 0 Then
sz = Split(Replace(rng3(i1, i2), " ", "", 1, -1), ",")
Else
ReDim sz(0)
sz(0) = rng3(i1, i2)
End If
For i3 = 0 To UBound(sz)
If InStr(sz(i3), "-") > 0 Then
sz3 = Split(sz(i3), "-")
ReDim sz1(CInt(sz3(1)) - CInt(sz3(0)))
For i5 = CInt(sz3(0)) To CInt(sz3(1))
sz1(i5 - CInt(sz3(0))) = IIf(Len(CStr(i5)) = 3, CStr(i5), IIf(Len(CStr(i5)) = 2, "0" & CStr(i5), "00" & CStr(i5)))
Next i5
Else
ReDim sz1(0)
sz1(0) = sz(i3)
End If
For i4 = 0 To UBound(sz1)
str1 = rng1(1, i2) & sz1(i4)
Sheets(2).Cells(m, 1) = rng2(i1, 1)
Sheets(2).Cells(m, 2) = str1
m = m + 1
Next i4
Next i3
out:
Next i2
Next i1
Sheets(2).Activate
End Sub
[/code]
|
|