|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Sub test0() ' ……
- Dim strPath As String, strFile As String, str_ As String
- Dim i As Long, j As Long, k As Long, x As Long, y As Long
- Dim ar, br, cr() As Long, dict As Object
-
- Set dict = CreateObject("Scripting.Dictionary")
-
- str_ = InputBox("请输入 车次", "输入提示:", "2-5,3,6-7,10,12")
- If Len(str_) Then
- ar = Split(str_, ",")
- For i = 0 To UBound(ar)
- br = Split(ar(i), "-")
- For j = 0 To UBound(br)
- If Not dict.Exists(Val(br(j))) Then dict.Add Val(br(j)), ""
- Next
- Next
- Else
- MsgBox "未指定车次!", 64: Exit Sub
- End If
-
- strPath = ThisWorkbook.Path & "\"
- strFile = strPath & "空白模板.xlsx"
- If Dir(strFile) = "" Then MsgBox "!", 64: Exit Sub
-
- DoApp False
-
- strPath = strPath & "Results" & "\"
- If Dir(strPath, vbDirectory) = "" Then MkDir strPath
-
- Dim results, data, Flag As Boolean
- Dim wkb As Workbook, wks As Worksheet, name_ As Name
- Dim cnt As Long, pos As Long
-
- With ActiveSheet
- data = .Range("A1", .Range("A1").CurrentRegion.Offset(1)).Value
- End With
- For j = 1 To UBound(data, 2)
- If Not dict.Exists(data(1, j)) Then dict.Add data(1, j), j
- Next
- x = dict("车次")
- data(UBound(data), x) = 10 ^ 7
-
- Set wkb = Workbooks.Open(strFile, 0)
- Set wks = wkb.Worksheets(1)
- results = wks.UsedRange.Resize(1000, 10).Value
- ActiveWindow.WindowState = xlMinimized
-
- For j = 1 To UBound(results, 2)
- If dict.Exists(results(6, j)) Then
- k = k + 1
- ReDim Preserve cr(1, 1 To k)
- cr(0, k) = j
- cr(1, k) = dict(results(6, j))
- End If
- Next
-
- For y = 2 To UBound(data) - 1
- If Val(data(y, x)) Then
- Flag = dict.Exists(Val(data(y, x)))
- If Flag Then
- cnt = 6
- pos = y
- For i = 2 To 3
- For j = 1 To 3 Step 2
- results(i, j + 1) = Trim(data(y, dict(Replace(results(i, j), ":", ""))))
- Next
- Next
- End If
- End If
- If Flag Then
- cnt = cnt + 1
- For j = LBound(cr, 2) To UBound(cr, 2)
- results(cnt, cr(0, j)) = data(y, cr(1, j))
- Next
- If Val(data(y + 1, x)) Then
- wks.Copy
- With ActiveWorkbook
- With .Worksheets(1)
- .Range("A1").Resize(cnt, UBound(results, 2)) = results
- .UsedRange.Offset(cnt).Clear
- End With
- For Each name_ In .Names
- name_.Delete
- Next
- .SaveAs strPath & Format(data(pos, x), "第0车"), 51
- .Close
- End With
- Flag = Not Flag
- End If
- End If
- Next
-
- Set dict = Nothing
- Set wks = Nothing
- wkb.Close False
- Set wkb = Nothing
- DoApp
- Beep
- End Sub
- Function DoApp(Optional b As Boolean = True)
- With Application
- .ScreenUpdating = b
- .DisplayAlerts = b
- .Calculation = -b * 30 - 4135
- End With
- End Function
复制代码 |
评分
-
1
查看全部评分
-
|