仅供参考这是一些示例数据和最终使用的宏。我不是程序员,已经达到了“足够好”的地步。非常感谢R3uK领先的方式:
r1 | r2 | ccs
0001T | 0002T | 52
0003T | 0003T | 130
0005T | 0006T | 59
0007T | 0007T | 211
0008T | 0008T | 93
0009T | 0009T | 125
00100 | 00104 | 232
0010T | 0010T | 40
00120 | 00126 | 232
0012T | 0013T | 162
00140 | 00148 | 232
0014T | 0014T | 176
17999 | 17999 | 175
19000 | 19001 | 165
19020 | 19020 | 175
19030 | 19103 | 165
C2018 | C2018 | 243
C2019 | C2019 | 243
C2020 | C2020 | 243
C2021 | C2021 | 243
C2022 | C2022 | 243
C2023 | C2023 | 243
Public Function DeleteAndAddSheet(ByVal SheetName As String) As Worksheet
For Each aShe In Sheets
If aShe.Name <> SheetName Then
Else
Application.DisplayAlerts = False
aShe.Delete
Application.DisplayAlerts = True
Exit For
End If
Next aShe
Sheets.Add after:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Name = SheetName
Set DeleteAndAddSheet = ThisWorkbook.Worksheets(Worksheets.Count)
End Function
Sub Popluate_Ranges()
Dim WsS As Worksheet
Set WsS = Worksheets("Sheet1")
Dim WsR As Worksheet
Set WsR = DeleteAndAddSheet("Results")
Dim sc As Integer
Dim sr As Integer
Dim rc As Integer
Dim rr As Long
Dim rr2 As Long
Dim rc2 As Integer
Dim a As String
Dim b As String
Dim c As String
Dim v1 As String
Dim v2 As String
Dim d As String
Dim e As String
Dim f As String
Dim i As Integer
'break string into 3 parts for handling
rr = 1
rc = 1
sr = 2
sc = 1
For sr = 2 To WsS.Range("A2").End(xlDown).Row
For sc = 1 To 2
'write the first digit to first result column
WsR.Cells(rr, rc) = Left(Format(WsS.Cells(sr, sc), "00000"), 1)
rc = rc + 1
'write the middle 3 digits to the second result column
WsR.Cells(rr, rc) = Right(Left(Format(WsS.Cells(sr, sc), "00000"), 4), 3)
rc = rc + 1
'write the last digit to the third result column
WsR.Cells(rr, rc) = Right(Format(WsS.Cells(sr, sc), "00000"), 1)
rc = rc + 1
Next sc
'write ccs
WsR.Cells(rr, rc) = WsS.Cells(sr, sc)
rr = rr + 1
rc = 1
Next sr
WsR.Range("B:B").NumberFormat = "000"
WsR.Range("E:E").NumberFormat = "000"
rr = 1
rc = 1
rr2 = 1
rc2 = 8
For rr = 1 To WsR.Range("A1").End(xlDown).Row
'write/rejoin range start value
a = WsR.Cells(rr, rc).Value
b = WsR.Cells(rr, rc + 1).Value
c = WsR.Cells(rr, rc + 2).Value
WsR.Cells(rr2, rc2) = Format(a, "0") & Format(b, "000") & Format(c, "0")
WsR.Cells(rr2, rc2 + 1) = WsR.Cells(rr, 7).Value
rr2 = rr2 + 1
'check if at the end of the range
d = WsR.Cells(rr, rc + 3).Value
e = WsR.Cells(rr, rc + 4).Value
f = WsR.Cells(rr, rc + 5).Value
Do Until (a = d And b = e And c = f)
If IsNumeric(a) = False Then
i = a
i = i + 1
v1 = a
c = Right(i, 1)
v2 = a
If v1 = 9 And v2 = 0 Then
b = b + 1
End If
ElseIf IsNumeric(c) = False Then
b = b + 1
Else:
i = c
i = i + 1
v1 = c
c = Right(i, 1)
v2 = c
If v1 = 9 And v2 = 0 Then
b = b + 1
End If
End If
WsR.Cells(rr2, rc2) = Format(a, "0") & Format(b, "000") & Format(c, "0")
WsR.Cells(rr2, rc2 + 1) = WsR.Cells(rr, 7).Value
'WsR.Cells(rr2, rc2 + 2) = rr
rr2 = rr2 + 1
Loop
Next rr
WsR.Range("H:H").NumberFormat = "00000"
WsR.Activate
Range("A1:G1").EntireColumn.Delete
End Sub
告诉我们你试过的东西。 – 2015-04-03 15:56:19
我真的被难住了,这就是我问这个问题的原因。我正在考虑一个SQL游标(* gasp *),但R3uK在下面有一个很好的解决方案。我正在修改它以获得所需的特定结果 - 将结果拆分为两列,保持前导零,并处理不同的值(00305,A6047,0023C等)。将完成后发布解决方案,谢谢。 – user3285188 2015-04-03 16:34:46
如果你有解决方案,这是没问题的。确保接受你认为有用的答案。 – 2015-04-03 17:02:36