|
'只是增加了一个选择源数据文件夹的弹出框,这个目标文件夹名称不一定是“分发库”
Option Explicit
Sub test4()
Dim arr, pth, filename(), dic(1), i, t, p, f
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show Then pth = .SelectedItems(1) Else Exit Sub
End With
t = Split(pth, "\")
pth = Left(pth, Len(pth) - Len(t(UBound(t))))
If Not getfilename(filename, pth & t(UBound(t)), ".pdf") Then MsgBox "!!": Exit Sub
For i = 0 To UBound(dic)
Set dic(i) = CreateObject("scripting.dictionary")
Next
arr = [a1].CurrentRegion.Offset(1).Resize(, 6)
For i = 1 To UBound(arr, 1) - 1
If Len(arr(i, 1)) > 0 And Len(arr(i, 2)) > 0 Then dic(0)(LCase(arr(i, 1))) = arr(i, 2) & "\"
If Len(arr(i, 5)) > 0 And Len(arr(i, 6)) > 0 Then dic(1)(LCase(arr(i, 5))) = arr(i, 6)
Next
On Error GoTo errmsg
For i = 1 To UBound(filename)
t = Split(filename(i), "\"): f = t(UBound(t)): t = LCase(Split(f, "-")(0))
If dic(0).exists(t) Then
p = dic(0)(t): t = LCase(Left(f, 1))
t = IIf(dic(1).exists(t), dic(1)(t), "其他") & "\"
If Len(Dir(pth & t, vbDirectory)) = 0 Then MkDir pth & t
p = pth & t & p
If Len(Dir(p, vbDirectory)) = 0 Then MkDir p
FileCopy filename(i), p & f
Else
MsgBox "无法定位药品名称" & vbNewLine & filename(i): Exit Sub
End If
Next
Exit Sub
errmsg:
MsgBox filename(i) & vbNewLine & "无法写入文件,文件是否已经打开或磁盘已满?"
End Sub
Function getfilename(filename, pth, mark) As Boolean
Dim f, n
If Right(pth, 1) <> "\" Then pth = pth & "\"
f = Dir(pth & "*.*")
Do While Len(f) > 0
If LCase(Right(f, Len(mark))) = LCase(mark) Then
n = n + 1: ReDim Preserve filename(1 To n)
filename(n) = pth & f
End If
f = Dir
Loop
If n > 0 Then getfilename = True
End Function |
评分
-
2
查看全部评分
-
|