2015-04-03 37 views
0

有一个包含三列和〜9,400行的表格,列出了代码的字母数字范围的开始和结束值以及指定给范围的第三个值。希望将两列范围端点更改为明确列出(1:1)值的单列。你能想到一种方法来自动化范围内的明确列表值?给定起始点和结束点的字母数字值的范围

当前示例行:
| --- r1 ---- | --- r2 ---- | ccs |
| 0106T | 0110T | 7 |

所需输出:
| --code- | ccs |
| 0106T | 7 |
| 0107T | 7 |
| 0108T | 7 |
| 0109T | 7 |
| 0110T | 7 |

这是存储在一个MySQL数据库和Excel工作簿,但任何语言或庄园的操作都很好。

+2

告诉我们你试过的东西。 – 2015-04-03 15:56:19

+1

我真的被难住了,这就是我问这个问题的原因。我正在考虑一个SQL游标(* gasp *),但R3uK在下面有一个很好的解决方案。我正在修改它以获得所需的特定结果 - 将结果拆分为两列,保持前导零,并处理不同的值(00305,A6047,0023C等)。将完成后发布解决方案,谢谢。 – user3285188 2015-04-03 16:34:46

+0

如果你有解决方案,这是没问题的。确保接受你认为有用的答案。 – 2015-04-03 17:02:36

回答

0

如果只能用一个“T”,这将工作,是一个很好的基础,以完整的宏:

Sub Alpha() 
Dim WsS As Worksheet 
Set WsS = Worksheets("Source") 
Dim WsR As Worksheet 
Set WsR = DeleteAndAddSheet("Results") 

For i = 2 To WsS.Range("A" & Rows.Count).End(xlUp).Row 

    For k = Val(WsS.Cells(i, 1)) To Val(WsS.Cells(i, 2)) 
     WsR.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0) = k & "T " & WsS.Cells(i, 3) 
    Next k 

Next i 

End Sub 

有用的功能表:

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 
+0

伟大的地方开始,感谢您的帮助!完成后会发布最终解决方案。 – user3285188 2015-04-03 16:35:51

+0

很高兴我可以帮忙,我很匆忙,所以我没有完成,但为了使它灵活(关于这封信),你可以在'IsNumeric(mid(wss.cells(i, 1),MovingStart,1))''你会得到你的第一个字母的地址来使用'Right(wss.cells(i,1),Len(wss.Cells(i,1)) - MovingStart +或者 - 1)'获取非数字部分。对于以“0”开始,您可以简单地将其添加到文本中或使用Format()。 – R3uK 2015-04-03 16:45:25

0

综观提供的解决方案通过R3uK,Set DeleteAndAddSheet语句中的DeleteAndAddSheet函数看起来有错误。下标超出范围错误的语句错误。不确定预期的结果是否为图纸编号。你可以澄清一下吗? 谢谢。

+0

它不是直接创建表单的数字,因此可以在进一步编码中将其设置为对象。仍然奇怪的是,你有一个错误...你试图影响结果的数值?或者,也许可以将ThisWorkbook更改为ActiveWorkbook ... – R3uK 2015-04-03 16:51:52

+0

查看该函数行的结尾,它被声明为工作表,因此结果必须是工作表;) – R3uK 2015-04-03 16:53:24

0

仅供参考这是一些示例数据和最终使用的宏。我不是程序员,已经达到了“足够好”的地步。非常感谢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 
相关问题